I am trying to better understand two dimensional arrays with functions

I created this program in hopes to better understand 2d arrays by doing simple operations. I am just not getting it. All I am trying to do is add to values from 2 different arrays, letting the user pick the row to use. Any tips?

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
#include <cstdlib>
#include <iostream>

using namespace std;

int addArray(int [][2], int[][2], int, int);
int main(int argc, char** argv) {
 int array1 [2][2]={{1,2},
                      {3,4}};
    
    int array2[2][2]={{5,6},
                     {7,8}};
    int position1, n;
    int position2, a;
    
    cout<<"Enter a position";cin>>position1;
    cout<<"Enter a position";cin>>position2;
    cout<<addArray(array1[n][2],array2[a][2], position1, position2);
    
            
    return 0;
}
int addArray(int one[][2], int two[][2], int n, int a)
{
    int add;
    add=one[n][2]+two[a][2];
return add;
}

dood ur output will always be null/0

first position of array is always 0 , you have 2x2 array.....

add=one[n][2]+two[a][2];

you are adding two NULLs. u can do [0][1] to [1][1] . can't use 2 at all bud
Yes of course. That was dumb of me. I stupidly scratched out this code to show a simple example I might learn from. The question remains the same without that silly mistake.

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
#include <cstdlib>
#include <iostream>

using namespace std;

int addArray(int [][2], int[][2], int, int);
int main(int argc, char** argv) {
 int array1 [3][3]={{1,2,5},
                      {3,4,5}};
    
    int array2[3][3]={{5,6,6},
                     {7,8,6}};
    int position1, n;
    int position2, a;
    
    cout<<"Enter a position";cin>>position1;
    cout<<"Enter a position";cin>>position2;
    cout<<addArray(array1[n][2],array2[a][2], position1, position2);
    
            
    return 0;
}
int addArray(int one[][2], int two[][2], int n, int a)
{
    int add;
    add=one[n][2]+two[a][2];
return add;
}


Maybe someone can just point me in the direction of a simple example of a 2d arrays in used in function. It seems that the examples I have seen easily jump over my limited abilities. I need an idiots example 3x3 at most.
Last edited on
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
#include <cstdlib>
#include <iostream>

using namespace std;

int addArray(int array1[][2], int array2[][2]);

int main(int argc, char** argv) {

 int array1[2][2]={{1,2},{3,4}};

 int array2[2][2]={{5,6},{7,8}};


 cout<<addArray(array1,array2);

    return 0;
}
int addArray(int array1[][2], int array2[][2])
{
  int sum;

  sum = array1[0][0] + array2[1][1];
  return sum;
}



dont worry about user input, just add locations from two different arrays first, after that it is cakewalk...
Last edited on
I wish it was a cakewalk for me too. But alas...

I understand fully what is happening in your example. What i don't understand is the syntax that allows me to control a function.

i.e. what if I want to add a specific row
what if I want to add a specific column

can I say

int addArray(int array1[i][2], int array2[n][2])
What do you mean by "add a specific row"?

int addArray(int array1[][2], int array2[][2]); prototype/interface ,saying hey i have a function which takes 2 parameterz, which are arrays.
u need to tell the compiler how big they are. u do not manipulate the array at this point of the program

1
2
3
4
5
6
7
int addArray(int array1[][2], int array2[][2]) // size of arrays i am sending for manipulation
{
  int sum;

  sum = array1[0][0] + array2[1][1];
  return sum;
}


definition u manipulate ur code here.

1
2
3
4
5
6
7
int main(){

int array1[2][2]={{1,2},{3,4}}; // create my arrays
int array2[2][2]={{5,6},{7,8}};

addArray(array1,array2); // calling my function, pass/ telling it where it can find my arrays in ram.
}


Call function/ pass arrays

by default the name of the array is the address where the array can be found. I dont need to tell the call the size, it already knows because i told it using the prototype. it ONLY needs teh address now, which is array1 array2
Last edited on
the method u were using with position 1 and 2 , and more arguements for ur fucntion will work fine
Thanks so much. The pieces are falling together.
Topic archived. No new replies allowed.