Please help!!!!! Simple for loop

Hello forum,

I would first to say I appreciate any help, yes this is HW but I am not asking for it to be done for me, I'm a rookie and hit a snag.

So I want to write a program that calculates and compares 4 separate sums. Then compares them and prints a silly message.

this is it below, my problem is that the 1st sum calculates to 225 and then that is the value of int for the 2nd value, and so on..

the objective of the program,according to the professor, is to have the sums all be equal for the first 3 and the 4th one be purposefully wrong.

again, any help is appreciated I have been playing with this for 10 hrs. I know it is probably very simple, vaguely remember the professor speaking of this in class but got stuck..

#include <iostream>
using namespace std;
int main()
{
int n;
int sum1 = 0;
for (n = 1; n <= 5; n++){
sum1 = sum1 + (n * n * n);
}
cout << sum1 << endl;

int sum2 = (n * n *((n + 1)*(n + 1)) / 4);
cout << sum2 << endl;

int sum3 = 0;
for (n = 1; n <= 5; n++){
sum3 = ((n + 1)*(n + 1));
}
cout << sum3 << endl;

int sum4 = (n *((n + 1)*(2 * n + 1)) / (4 + 2));
cout << sum4 << endl;

if (sum1 == sum2)
cout << "sum1 and sum2 are equal.";
else
cout << "sum1 and sum2 are not equal."<<endl;
if (sum2 == sum3)
cout << "sum2 and sum3 are equal.";
else
cout << "sum2 and sum3 are not equal."<<endl;
if (sum3 == sum4)
cout << "sum3 and sum4 are equal.";
else
cout << "sum3 and sum4 are not equal."<<endl;
system ("PAUSE");
return 0;
}
It's pretty difficult to suggest anything, unless we know which parts were supplied by the professor, and which parts are your own contribution. For example, this looks like the sums of series done by both adding all the terms and by the use of a formula. Were the formulae supplied, or did you find then by yourself?

Oh and your code would be easier to read if enclosed in code tags and properly indented. (use the "<>" button).
Last edited on
I can tell you that this one is wrong:

1
2
3
4
int sum3 = 0;
for (n = 1; n <= 5; n++){
sum3 = ((n + 1)*(n + 1));
}


n=1 sum3 = (1+1)(1+1)=4
n=2 sum3 = (2+1)(2+1)=9
n=3 sum3 = (3+1)(3+1)=16
n=4 sum3 = (4+1)(4+1)=25
n=5 sum3 = (5+1)(5+1)=36

You can try this:

1
2
3
4
int sum3 = 0;
for (n = 1; n <= 5; n++){
sum3 = ((n*2)*(n*2)) +(n*n*n)
}


Its the same as the first one, but i think it works.
n=5 = (5*2)(5*2) + (5*5*5) = 10*10 + 125 = 225
One more comment. Think about the value of n after the completion of a "for" loop. (print out the value if you are not sure). Does n have the value that you want it to have for the subsequent calculations?
RE: Chervil:
its all my contribution, the professor handed out a paper that had 4 equations and that we should print which a re equal and which are not for any value of n.

And that is the trouble I am having.

I figured out on paper that the sum in the first one for n=1 thru 5 which is what the loop runs for, would be 225. That's easy, now go to the next sum expression, that sum is going to start at n=6, how do I make it start back at n = 1 so that it too can be equal to 225???

My question has to do with keeping that variable constant at n =1 after to loop for the second look to use it.

RE: pogrady
that formula was given, it works I think I just did it wrong and now I see it. Thanks for pointing that out to me.
its supposed to be (1+2+3+4+5)2 which in this case would work (15) squared is 225.
thanks to all
Hi, thanks for your reply. I guess I wasn't totally clear on what was the original question - if something is supposed to be deliberately "wrong" it might be hard for an onlooker to separate that from those things which were accidentally wrong. :)

Anyway, I notice you have the value "5" appearing a couple of times in your code. I would recommend you make this a constant, something like this: const int count = 5; and place this at the start of your main function.

Then you can use this count value instead of "5" in your for loop. And also, you can reset n to this value at those points in the program where it is required (Just put n=0; or n=1; or n= count; whichever you feel is appropriate).

The advantage of doing this is you can later try changing this to a different figure, in order to thoroughly test the code (if it works for just one value it could be a fluke).

Thank you chervil, it took very long, but hey I learned it and will probably not forget it for 50 years.
I had to declare another variable and make n constant as you stated.
#include <iostream>
using namespace std;
int main()
{
int n = 7;
int m;
int sum1 = 0;
for(m = 1; m <= n; m++){
sum1 = sum1 + (m * m * m);
}
cout << sum1 << endl;
int sum2 = (n * n *((n + 1) * (n + 1))/ 4);
cout << sum2 << endl;
int sum3 = 0;
for(m = 1; m <= n; m++){
sum3 = (sum3 + m);
}
sum3 = sum3 * sum3;
cout << sum3 << endl;
int sum4 = (n * ((n + 1) * (2 * n + 1))/(4 + 2));
cout << sum4 << endl;

if (sum1 == sum2)
cout << "sum1 and sum2 are equal." << endl;
else
cout << "sum1 and sum2 are not equal." << endl;

if (sum2 == sum3)
cout << "sum2 and sum3 are equal." << endl;
else
cout << "sum2 and sum3 are not equal." << endl;

if (sum3 == sum4)
cout << "sum3 and sum4 are equal." << endl;
else
cout << "sum3 and sum4 are not equal." << endl;
system ("PAUSE");
return 0;
}
I'm really glad you worked through it and got it working properly.
It gets easier, trust me.

Topic archived. No new replies allowed.