Sending Two Arrays to 1 Function

Need a little help with this question :

1
2
3
4
5
Write a program that asks user to input two matrix of the same size. Then prints menu to enable the user to choose an operation:
           1: Addition.
           2: Subtraction.
           3: Transpose.
After choosing the operation, the program calls a function to perform the required operation, and prints the original matrices in a tabular form, the requested operation and the result.


I wrote Everything but the problem Is how can I send 2 arrays to a function ._. ??
I know how to send an array to a function , like this :
function(array1 , 5 , 8)
First time replier :)

From what I know, using Functions with arrays, need to have subscripts ([]) in the... arguments.

1
2
3
4
5
6
7
8
9
10
11
function(int []); // Prototype; 1 Array
function(int [], int []); // Prototype; 2 Arrays

int main (void)
{
     int array1 [5], array2 [5];

     function (array1, array2); // using the prototype with two arrays

     return 0;
}


Get the idea?
Last edited on
Just as you call with 1 array call it with 2 arrays:
int[N][M] function(array1 , array2, 5 , 8);

Arrays are passed by reference (in c++ by pointers to be more specific) so every change in your array you make inside the function is applied to the original matrices. You don't have to worry about that though since you don't have to change any array in your case.

Just make sure you know the dimensions at compile time. Also you know the dimension of your return array (it's the same as the others even in your transpose case, but reversed i.e. the memory allocation is the same NxM).

If you have a choice consider using std::vector instead of arrays anyway.
Topic archived. No new replies allowed.