Sum of two arrays

I am reworking a previous post

http://www.cplusplus.com/forum/beginner/183495/

and have added the braces as suggested in the post but I am still receiving an error saying - void sum(int[],int[],int[],int[]) : cannot convert argument 4 from 'const int' to in[]

thanks for any help!


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
33
    #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 code, as posted, compiles and runs fine.

If you do change the type of fourth argument from int to int[], then calling the function with arraySize (a const int) is obviously an error, but the fourth argument should not be an array.
Last edited on
I copied your code exactly and it is working on my end? Try coping it and pasting it here: http://cpp.sh/


Array 1  +   Array 2  =   Array 3
      1  +        11  =        12
      2  +        12  =        14
      3  +        13  =        16
      4  +        14  =        18
      5  +        15  =        20
      6  +        16  =        22
      7  +        17  =        24
      8  +        18  =        26
      9  +        19  =        28
     10  +        20  =        30
     11  +        21  =        32
     12  +        22  =        34
     13  +        23  =        36
     14  +        24  =        38
     15  +        25  =        40
     16  +        26  =        42
     17  +        27  =        44
     18  +        28  =        46
     18  +        29  =        47
     20  +        30  =        50


My guess is there is something wrong with your setup/environment.
Last edited on
Topic archived. No new replies allowed.