Code errors

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

10 + 4 = 14

I can't figure out how to add the elements of the arrays and display it

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
  #include <iostream>
  #include <array>
  using namespace std;
  const size_t rows{10};
  const size_t columns{2);
  void printArray(const array<array<int, columns>, rows>&);

  int main()
{
 array<array,int, columns>, rows> arrayOne{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20};
array<array,int, columns>, rows> arrayTwo{21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40};
array<array<int, columns> rows> arraySum[20];

cout << "The values of array one are: " << endl;
printArray(arrayOne);

cout << "The values of array two are: " << endl;
printArray(arrayTwo);

arraySum[i] = arrayOne[i] + arrayTwo[i];

cout << " Sum of array one and two is: ";
printArray(arraySum);
}
}
 
The answer is in your question

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


The arrays are all accessed by index so this is how you will populate the values in your third array. You will need a loop to cover all of your index values.

As for passing the values into a function you will need to make sure that you pass the arrays by reference so that they will be updated in main. See the difference in this tutorial http://www.cplusplus.com/doc/tutorial/functions/

Thanks. I'm trying to understand it, but it seems to be going over my head. I'm assuming my loop would need to be something similar to

int count;

for(count =0; count < 20; count++) {
cout << arrayOne[count];

As for passing the arrays by reference I'm not quite sure I get it. I think I'm hung up and wanting to make sure my output for the arraySum is also in columns and rows.
Essentially, you will want two loops, thie first to populate your third array with the second to print it out.

The first loop will use this
arraySum[i] = arrayOne[i] + arrayTwo[i];


The second for displaying the output will be similar to the one here

for(count =0; count < 20; count++) {
cout << arrayOne[count]; //<-- this will only print the values from array one
//try something like this
//cout << FirstArrayVal << plus << secondArrayVal << eqaul << sumArrayVal << newline MM


Pass by reference essentially means that you are not passing a copy of the object, but the object itself. A brief example illustrating this point:

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
#include <iostream>
void passByVal(int x, int y)
{
    x=5;
    y = 10;
   std::cout << "passByVal: x = " << x << " y = " << y << "\n";
}

void passByRef(int& x, int& y)
{
   x = 5;
  y = 10;
  std::cout << "passByRef: x = " << x << " y = " << y << "\n";
}

int main()
{
    int x = 0;
    int y = 0;

   //show values of x and y before operations
   std::cout << "Main: x = " << x << " y = " << y << "\n";

   //notice the value of main after this
   passByVal(x,y);
   std::cout << "Main: x = " << x << " y = " << y << "\n";

   //notice the value after this
   passByRef(x,y);
   std::cout << "Main: x = " << x << " y = " << y << "\n";
}


Here is the output f the above code.
Main: x = 0 y = 0
passByVal: x = 5 y = 10
Main: x = 0 y = 0
passByRef: x = 5 y = 10
Main: x = 5 y = 10
Last edited on
Thank you so much. That cleared it up for me.
Topic archived. No new replies allowed.