0

I want to write a function addArrays which will, as parameters, take two 2D arrays of type int and of dimensions 3x4, and it's job is to add individual elements of each index from the given arrays and display it in the console.

In main(), I created two 2D arrays arrA and arrB of appropriate sizes with intitialized members and check the functionality of the created function.

My code so far:

#include <iostream>
using namespace std;

void addArrays(int x[3][4], int y[3][4]);

int main()
{
    int arrA[3][4] = { {7, 8, 13, 22}, {56, 4, 78, 3}, {22, 13, 46, 5} };
    int arrB[3][4] = { {32, 47, 56, 14}, {33, 100, 19, 64}, {4, 18, 157, 84} };
}

void addArrays(int x[3][4], int y[3][4])
{
}

Honestly, I know how to work with 1D arrays, but not displaying the sum of all individual elements. I know I have to use a for loop, but again, I'm confused as to how to pass in a 2D array and use it.

anastaciu
  • 23,467
  • 7
  • 28
  • 53
Wixxybyte
  • 27
  • 4
  • You should use loops. Do you know how to print elements of one 3x4 array? – MikeCAT Feb 03 '21 at 14:32
  • Honestly, I don't. I know how to work with 1D arrays, but not displaying the sum of all individual elements. I know I have to use a for loop, but again, I'm confused as to how to pass in a 2D array into it. – Wixxybyte Feb 03 '21 at 14:36

2 Answers2

2

You mention you know how to work with 1D arrays, it's the same for 2D arrays, only with one more dimension.

In a 1D array you use arrA[0] to access the first element of the array, in a 2D array you use arrA[0][0] to access the first element in the first line of the array, arrA[0][1] to access the second element in the first line of the array. To access the first element in the second line you would use arrA[1][0] and so on, you get the idea.

So to loop through all the elements in the array you can use nested loops like so:

void addArrays(int x[3][4], int y[3][4])                                        
{
    for( int i = 0; i < 3; i++){    // make sure to use the correct dimensions
        for(int j = 0; j < 4; j++){ // 3 lines and 4 columns
           // x[i][j] accesses elements in array x
           // y[i][j] accesses elements in array y
        }
    }
}

I think you'll manage to do the math yourself. After that you just need to send data to the standard output, i.e. to print data in the console. For that, as you may know, you use std::cout.

Side notes:

  • In the function void addArrays(int x[3][4], int y[3][4]){...} you may omit the first dimension of the array i.e. int x[][4] or int (*x)[4] instead of int x[3][4], since the argument will decay to a pointer to array.

  • Since it seems that you are not to change the values of the passed arrays, using const is recommend. You would have void addArrays(const int (*x)[4], const int (*y)[4]);

  • As you are using C++, you can take advantage of the possibility of using references, something like void addArrays(const int (&x)[3][4], const int (&y)[3][4]){/*same code*/}, the benefit being you must pass a correct object to the function otherwise the program will fail to compile whereas in the previous versions if you pass, for example, NULL, i.e. addArrays(arrA, NULL); the program will compile fine but will result in undefined behavior when you run it. References are safer and you should use them when possible.

  • It's more or less consensual among more experienced C++ programmers that the usage of using namespace std; is not a good practice, you can read more about it, and find alternatives following the link.

anastaciu
  • 23,467
  • 7
  • 28
  • 53
0

I will start this for you and try to give you an idea of the general structure, but since you have not shown your attempt at the problem I won't fill things in for you.

The basic idea here when looping through 2D arrays (of size MxN) is that you can really just think about it in terms of looping through M arrays of length N.

void loopThroughArray(int arr[M][N])
{
  // Loop over M arrays
  for (int m = 0; m < M; ++m) {
    // For each m'th array, loop over its N contents
    for (int n = 0; n < N; ++n) {
      // Doing work
      arr[m][n] = 1234;
    }
  }
}
Jacob Faib
  • 1,062
  • 7
  • 22
  • If I understood correctly however, there have to be 2 parameters in one function. Like `(int x[3][4], int y[3][4])` – Wixxybyte Feb 03 '21 at 14:50
  • How would I loop through already set rows and columns? – Wixxybyte Feb 03 '21 at 14:52
  • Right, I am not giving you the code to solve your exact problem here. I am giving you a code snippet to help you understand how to go about solving your problem. Please read [this post](https://meta.stackoverflow.com/questions/334822/how-do-i-ask-and-answer-homework-questions) about asking homework problems. – Jacob Faib Feb 03 '21 at 14:52
  • That's okay. Thank you. – Wixxybyte Feb 03 '21 at 14:54
  • In your case since you know your arrays are both 3x4, you can hard-code `M` and `N` as 3 and 4 respectively in the loops. Otherwise you would have to either pass the array sizes in as additional parameters to the function (i.e. `void addArrays(int arr1[][], int arr2[][], int M, int N)` or as template parameters. – Jacob Faib Feb 03 '21 at 14:55
  • 1
    @JacobFaib no, you must specify all but the first dimension. This can work: `void addArrays(int arr1[][N], int arr2[][N],int M)` – 463035818_is_not_an_ai Feb 03 '21 at 17:03