question about summing

i got a realyl stupid question, i know i shoudln't ask this but somehow i just forgot how to code anymore.


i want to sum the array from [1,0] to [1,7]

e.g
given the array
1,2,3,4,5,6,2

i want to write a function Sum,
in which the input is

sum([1,0],[1,x]) <-- in this example x = 7
and it will display 1+2+3+4+5+6+2 = 23
You can just do something simple like this with loops which keeps going through the array (adding each element to the Sum) until it hits whatever limit you want to set.

1
2
3
4
5
6
7
8
9
10
	int Array[] = {1,2,3,4,5,6,2};
	int Limit = 7; 
	int Sum = 0;

	for(int i = 0; i < Limit; i++)
	{
		Sum += Array[i];
	}

	cout << "The sum of the numbers is " << Sum << endl; 
thanks alot, but i woudl want to code it in while loop. how i do that?
1
2
3
4
5
6
7
8
9
10
11
12
int Array[] = {1,2,3,4,5,6,2};
int Counter = 0; 
int Limit = 7; 
int Sum = 0;

while(Counter < Limit)  // Keeps looping until Counter == Limit
{
	Sum += Array[Counter];
	Counter++;  // Adds 1 to Counter 
}

cout << "The sum of the numbers is " << Sum << endl; 
Topic archived. No new replies allowed.