Sum of two arrays printed in a third array by passing the values to a function

Hello,
I have an assignment to make. I am not sure if I am on the right track and I receive two errors from the compiler. I am also not sure if I should put the "cout" part in the sum() or in main()
Thank you in advance for your advices.
Here is the assignment:
Create three arrays of 20 integers each. Fill in two arrays with data, leaving the third blank. From main, pass these three arrays into a function. Inside the function, add the value of each array element and store it in the third.

third_array[i] = second_array[i] + first_array[i]

Back in main, print out the value of every element in the third/sum array. Excellent output on this assignment might show something like:

Array 1 + Array 2 = Array 3

5 + 3 = 8

etc.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
  #include <iostream>
#include <iomanip>

using namespace std;

void sum(int [], int [], int [], int);

int main()
{
	const int arraySize = 20;
	int first_array[arraySize] = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,18,20};
	int second_array[arraySize] = {11,12,13,14,15,16,17,18,19,20,
								21,22,23,24,25,26,27,28,29,30};
	int third_array[arraySize] = {};
	
	cout << setw(7) << "Array 1" << setw(3) << "+" << setw(10)
	<< "Array 2" << setw(3) << "=" << setw(10) << "Array 3" << endl;

	
	sum( first_array, second_array, third_array, arraySize);
	
}

void sum(int a[], int b[], int c[], int sizeArray)
{
	for (int i = 0; i < sizeArray; ++i)
	c[i]= b[i] + a[i];
	
	cout << setw(7) << a[i] << setw(3) << "+" << setw(10)
	<< b[i] << setw(3) << "=" << setw(10) << c[i] << endl;
}

The two errors are: 29: name lookup for 'i' changed for new ANSI 'for' scoping
and 26: using obsolete binding at 'i'
You forgot braces starting on line 27 and ending on line 30; in other words, when you have something like an if, while, or for statement without brackets, only the next line will be looped over. You have to add braces around a larger statement if you want the for loop to include more than one statement. Otherwise, your code should work fine.
Thank you, Ispil!
It worked.
Topic archived. No new replies allowed.