# Thread: 2D Array Help

1. ## 2D Array Help

how can i get an 2 dimentional array to be called and the contents of the second array to be passed into the first array and then show the user the contents of array before and after the function call?

2. >the contents of the second array to be passed into the first array
Code:
`void func(int array[][3])`
Or alternately:
Code:
`void func(int array[3][3])`
If you have a second array, just add the second array to the function parameter list.

3. yeah i have that but was wondering if anyone could give me a few more hints of what is needed.

4. You need to explain better what you need, and maybe post some code showing what you're trying to do. Your first post isn't clear. If you use a vector instead, you can copy by doing:
Code:
`vector<int> b = a;`

5. what is needed is
1. 2d int array of the same size
2. function called AddArray
3. contents of the second array to be added to the first array element by element
4. the caller main() should output the contents of the arrays before and after the function call.

Hope this helps a bit more.

ill post some code when i have got a bit more done, just asked so i could get a few pointers as i go along.

6. So in main you've got the following (where size is a constant):
Code:
```   int a[size][size];
int b[size][size];

//Arrays filled with values

//Output the contents of the arrays (use nested for-loops)

//Output the contents of the arrays (use nested for-loops)```
Then the declaration for AddArray would look something like:
Code:
```void AddArray(int a[size][size], int b[size][size])
{
//Code to add the arrays
}```
Or you can leave out the first dimension:
Code:
`void AddArray(int a[][size], int b[][size])`

7. then do i just need to get a loop to go through all the characters and then output them into the first array?

8. >then do i just need to get a loop to go through all the characters and then output them into the first array?
>3. contents of the second array to be added to the first array element by element
So you want to add each element of the second array to the corresponding element of the first array.

Take something simple like printing a 2d array. How do you do that? Well normally two nested for-loops. Start by writing the code to do that. Once you have code to print a 2d array, compile and run the code to make sure it prints the array. Once you have that part working simply take that code, copy it to your AddArray function, and change it to add the arrays, instead of printing them.

9. just an example of the loop i will need to do is this right (StrOut from another program)

Code:
```	  int i = 0;
while (a[i] != '\0')
{
StrOut[i] = a[i];
i ++;
}

StrOut[i] = ' ';
i++;

int j = 0;
while (b[j] != '\0')
{
StrOut[i] = b[j];
j ++;
i++;
i+=StrOut[i] = '\0';
}
cout <<StrOut <<endl;```

10. Code:
```for (int i=0; i<rows; ++i) {
for (int j=0; j<cols; ++j) {
printf("%i", A[rows][cols]); //or %f if you use doubles
}
printf("\n"); //end of row, go to next line.
}```
NB: capital letters are standard notation for matrices in maths, doesn't really matter in code (im assuming thats your goal, a matrix). rows and cols are the dimensions of the array. i have done a matrix ADT for both C and C++. i used pointer arith and a single-d array but this will work too.

11. > printf("%i", A[rows][cols]); //or %f if you use doubles
I think you meant i and j.

12. i got this loop. guess it does the same thing?

Code:
```int arr[3][3];
for (int x=0; x<3; x++)
for (int y=0; y<3; y++)
arr[x][y] = 0;```

13. >i got this loop. guess it does the same thing?
That's exactly what you want. It will allow you to access each element of the 2d array.

14. once i have got that loop implemented how do i show what was in the array before and then after it had been copied?

15. whoops, yes i did.
Code:
`printf("%i", A[i][j]);`
thanks for the pick-up

Popular pages Recent additions