How to add values of a for loop?

I want to add each of the values that are calculated by the end of each for loop.

For example:

for(int i = 0; i < 10; i++)
{
int x;
}

I want to be able to add all of the x values calculated from the for loop together. Does anyone know how to do that?
Last edited on
?? + ?? = ??

Current x is undefined. You are going to want to have a sum variable outside of the for loop and set it equal to zero. Then just say something like sum += x; inside of your for loop though first you want to do something with x or you will have undefined behavior.


1
2
3
4
5
6
int sum = 0;

for( int i = 0; i < 10; ++i )
    sum += i;

std::cout << "sum: " << sum << std::endl;

http://ideone.com/JJxmRl

Though technically speaking if you are trying to do what I just did you would be better off using summation*.

*http://en.wikipedia.org/wiki/Summation


Thanks, I actually haven't learned that operator yet. My teacher is not very good, he just tells us to program something and doesn't teach us how. We spend most of our time looking on the internet learning how to program.
It is a compound operator it would be the same as

sum = sum + i;

pretty much just adds the right side to the current value ( left side ).

http://www.cplusplus.com/doc/tutorial/operators/ --towards the middle

*edit well not really middle about a quarter of way down.

Compound assignment (+=, -=, *=, /=, %=, >>=, <<=, &=, ^=, |=)
Compound assignment operators modify the current value of a variable by performing an operation on it. They are equivalent to assigning the result of an operation to the first operand:

expression equivalent to...
y += x; y = y + x;
x -= 5; x = x - 5;
x /= y; x = x / y;
price *= units + 1; price = price * (units+1);

and the same for all other compound assignment operators. For example:
1
2
3
4
5
6
7
8
9
10
11
// compound assignment operators
#include <iostream>
using namespace std;

int main ()
{
  int a, b=3;
  a = b;
  a+=2;             // equivalent to a=a+2
  cout << a;
}
5

Last edited on
I thought I was finished, but it turns out to be more complicated:

I am writing a program that records user inputed polynomials and then allows them to input the x values for the polynomial.

I am stuck on the part where I am supposed to add the (ax^(b)) values together. Where a is the coefficient and b is the exponent.

The user inputs the amount of terms they want earlier on. I omitted the rest of the code to keep it simple.

1
2
3
4
5
6
7
8
9
10

	float x, coef[terms], powe[terms], term_val=0;

	for (int count = 0; count < terms; count++)
			{
				float term_val = coef[count] * pow( x , powe[count]) + term_val;

				cout << term_val;

			}


The for loop spits out each of the ax^(b), but I can't figure out how to add them together.

Last edited on
1
2
3
4
5
6
int sum = 0;

//for loop start
float term_val = coef[count] * pow( x , powe[count]) + term_val;
sum += term_val; //sum = sum + term_val;
//end for loop 



*edit
One more thing to mention you kind of have two variables both named term_val. Technically they are in two different scopes so it should be fine but I would suggest just removing the float on line 6 and use the old tem_val variable.
Last edited on
Thanks, you're awesome. Now I will go play around with the operator until I am more familiar with it. Thanks again.
Topic archived. No new replies allowed.