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

Click For Summary

Discussion Overview

The discussion revolves around a C++ programming issue related to calculating the sum of a specific series (1/3 + 1/6 + 1/9 + ... + 1/(3*n)) for a given integer n. Participants are addressing a homework problem that requires the use of loops and involves debugging the provided code.

Discussion Character

  • Homework-related
  • Technical explanation
  • Conceptual clarification

Main Points Raised

  • The original code fails to produce the correct sum due to integer division in the expression 1/iK, which results in zero.
  • One participant suggests modifying the division to 1./iK to ensure floating-point division occurs.
  • Another participant emphasizes the importance of proper type casting in calculations to avoid similar issues in the future.
  • Code formatting is discussed, with a recommendation to use [code] tags for better readability in forum posts.

Areas of Agreement / Disagreement

Participants generally agree on the cause of the issue related to integer division and the need for proper type casting, but there is no explicit consensus on additional coding practices beyond those mentioned.

Contextual Notes

The discussion highlights the potential pitfalls of type mismatches in programming and the importance of understanding how data types affect calculations.

Elbobo
Messages
145
Reaction score
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


Series = \frac{1}{3} \sum^{n}_{k=1} \frac{1}{k}

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;

count << "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)
{
count << "Invalid value for n. Exiting..." << endl;
return 0;
}
double dSum=0.0;
for(iK=1; iK <= iN; iK++)
{
dSum += double(1/iK);
}
count << "The value of that summation is " << (1/3)*dSum << "." << endl;
return 1;
}
 
Physics news on Phys.org
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).
 
Oh! Thank you so much!
 
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.
 
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]
 

Similar threads

  • · Replies 3 ·
Replies
3
Views
1K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 7 ·
Replies
7
Views
2K
  • · Replies 24 ·
Replies
24
Views
2K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 23 ·
Replies
23
Views
3K
  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 13 ·
Replies
13
Views
2K
  • · Replies 3 ·
Replies
3
Views
2K