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?
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?
>the contents of the second array to be passed into the first array
Or alternately:Code:void func(int array[][3])
If you have a second array, just add the second array to the function parameter list.Code:void func(int array[3][3])
yeah i have that but was wondering if anyone could give me a few more hints of what is needed.
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;
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.
So in main you've got the following (where size is a constant):
Then the declaration for AddArray would look something like:Code:int a[size][size]; int b[size][size]; //Arrays filled with values //Output the contents of the arrays (use nested for-loops) AddArray(a, b); //Output the contents of the arrays (use nested for-loops)
Or you can leave out the first dimension:Code:void AddArray(int a[size][size], int b[size][size]) { //Code to add the arrays }
Code:void AddArray(int a[][size], int b[][size])
then do i just need to get a loop to go through all the characters and then output them into the first array?
>then do i just need to get a loop to go through all the characters and then output them into the first array?
Your description says:
>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.
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;
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.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. }
> printf("%i", A[rows][cols]); //or %f if you use doubles
I think you meant i and j.
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;
>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.
once i have got that loop implemented how do i show what was in the array before and then after it had been copied?
whoops, yes i did.
thanks for the pick-upCode:printf("%i", A[i][j]);