Total sum of arrays

I have two user defined arrays and am trying to sum all of their elements together. Here is the function I must adhere to.
int sum_of_all (int data1 [size], int data2 [size], int values)
the two arrays values are defined by the user in this:
1
2
3
4
5
6
7
8
9
10
void input (int data [size], int values)
{ 	
	
	for (int index=1; index<=values; index++)
	{
		cout << "Enter value "<< index << ": ";
		cin >> data[index];
	}
cout << endl;
}


but I cannot figure out how the int sum_of_all function is intended to work.
here is my attempt:
1
2
3
4
5
6
7
8
9
10
11
12
13
int sum_of_all (int data1 [size], int data2 [size], int values)
{
	int index;
	int size;
	int sum=0;

	cout << "The total sum is: ";
	for (index=1; index<=values; index++)
	{
		sum=data1[index]+data2[index];
	}
	cout << sum;
}

needless to say, this does not work as sum changes each time stepped through the loop.
Last edited on
Remember that arrays in C/C++ start at zero and stop at size - 1.

needless to say, this does not work as sum changes each time stepped through the loop.

Are you aware of some of the other operators? http://www.cplusplus.com/doc/tutorial/operators/ Perhaps you need to use a different operator for your sum calculation?
Last edited on
Array indecies start at zero, so your for loop condition should look like this:
for ( index = 0; index <values; ++index )

Your thinking is on track. Each iteration of the loop needs to add to the sum, rather that simply assigning the result. Check out the += operator:
http://www.cplusplus.com/doc/tutorial/operators/#compound

As a side, remove line 4 from your code.

Topic archived. No new replies allowed.