C++: Simple summation program won't work correctly

Borek mentioned, if you are dealing with integers that are easily cast as doubles, you should use the dot method that he mentioned, as it is faster and more efficient than the casting approach.
  • #1
Elbobo
145
0

Homework Statement


Write a C++ program to determine and print the sum of the series (1/3 + 1/6 + 1/9 + ... + 1/(3*n)) for a given value of n. In case you don't see the patter, the series can also be written as 1/(3*1) + 1/(3*2) +1/(3*3)+ ... + 1/(3*n). The value of n should be given interactively through the terminal. (You won't get credit if you do not use loops)


Homework Equations


[itex]Series = \frac{1}{3} \sum^{n}_{k=1} \frac{1}{k}[/itex]

The Attempt at a Solution


//The following code gives me a final answer of 0. I can't understand why my FOR loop doesn't work. Please help!

#include <iostream>
#include <cmath>

using namespace std;

int main( void )
{
int iN = 0;
int iK = 1;

cout << "Input a positive integer value for n for the summation of 1/(3k), from k=1 to k=n." << endl;
cin >> iN;

if(iN <= 0)
{
cout << "Invalid value for n. Exiting..." << endl;
return 0;
}
double dSum=0.0;
for(iK=1; iK <= iN; iK++)
{
dSum += double(1/iK);
}
cout << "The value of that summation is " << (1/3)*dSum << "." << endl;
return 1;
}
 
Physics news on Phys.org
  • #2
1/iK is a result of dividing two integers, so it is integer as well - and it is always zero. Try 1./iK (dot added to force conversion on the expression to floating point).
 
  • #3
Oh! Thank you so much!
 
  • #4
Elbobo said:
In addition to what Borek said, when you include code in your post, put a [ code] tag at the beginning and a [ /code] tag at the end (without the extra space that I used). This preserves your indentation and makes your code easier to read. I have done this with your code.
Code:
#include <iostream>
#include <cmath>

using namespace std;

int main( void )
{
	int iN = 0;
	int iK = 1;

	cout << "Input a positive integer value for n for the summation of 1/(3k), from k=1 to k=n." << endl;
	cin >> iN;

	if(iN <= 0)
	{
		cout << "Invalid value for n. Exiting..." << endl;
		return 0;
	}
	double dSum=0.0;
	for(iK=1; iK <= iN; iK++)
	{
		dSum += double(1/iK);
	}
	cout << "The value of that summation is " << (1/3)*dSum << "." << endl;
	return 1;
}

As Borek points out, the division you're doing is integer division. Although you are casting the result to double in the line
Code:
dSum += double(1/iK);
it's too late. The value calculated is 0, and all the cast does is convert 0 as an integer to 0.0 as a double.
 
  • #5
and I would add further that the issue of using differently typed variables can lead to MANY problems, not just the one exemplified in your specific case, so it's always best to specifically cast your variables into whatever the end result of any calculation is supposed to be [assuming that they are not already of that type]
 

Related to C++: Simple summation program won't work correctly

1. Why is my C++ summation program not working correctly?

There could be several reasons why your summation program is not working correctly. Some common reasons include syntax errors, logic errors, and incorrect use of data types. It is important to carefully review your code and make sure it follows proper syntax and logic.

2. How do I fix my C++ summation program?

The best way to fix your summation program is to carefully review your code and identify any syntax or logic errors. You may also need to check your use of data types and make sure they are appropriate for the task at hand. Debugging tools and techniques can also be helpful in identifying and fixing errors.

3. What is the purpose of a summation program in C++?

A summation program in C++ is used to add together a series of numbers and calculate their total sum. This can be useful in a variety of applications, such as accounting, data analysis, and scientific calculations.

4. How can I improve the performance of my C++ summation program?

To improve the performance of your summation program, you can use more efficient algorithms and data structures, reduce the number of unnecessary calculations, and optimize your code for the specific task at hand. Additionally, using parallel processing techniques or optimizing for the hardware can also improve performance.

5. Are there any resources or tutorials for creating a C++ summation program?

Yes, there are many online resources and tutorials available for creating a C++ summation program. Some popular resources include C++ programming textbooks, online tutorials and courses, and community forums where you can ask for help and advice. It may also be helpful to practice with simple exercises and gradually build up to more complex programs.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
3
Views
773
  • Engineering and Comp Sci Homework Help
Replies
2
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
24
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
7
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
8
Views
883
  • Engineering and Comp Sci Homework Help
Replies
3
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
23
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
8
Views
1K
  • Programming and Computer Science
Replies
12
Views
1K
Back
Top