0

I have a function to multiply arrays. The function is multiply:

void multiply(int array1[][], array2[][]);

But it doesn't work.

for example I have to arrays

int array1[4][10]
int array2[10][6]
multiply(arra1[4][10], array2[10][6])

But it doesn't works.

I need call de function with any size.

later I need to call the function with:

int array3[5][5]
int array4[5][5]
multiply(arra1[5][5], array2[5][5])

How do I get the arrays in the function?

multidimensional array must have bounds for all dimensions except the first

And i need variable rows and variabloe columns

Eric Postpischil
  • 195,579
  • 13
  • 168
  • 312
Code Geas Coder
  • 1,839
  • 4
  • 23
  • 29
  • 1
    You tagged this with C and C++ but ask about C++ in the title. You should ask about only one per question, because the answers for this question will differ from C and C++. The C tag should be deleted from this question, and, if you are interested in a C answer, you should create a separate question. – Eric Postpischil Jun 29 '13 at 23:46
  • possible duplicate of http://stackoverflow.com/questions/1719051/array-of-pointers-as-function-parameter?rq=1 or http://stackoverflow.com/questions/4767190/how-to-pass-2-dimension-arrays-to-function-in-c?rq=1 or lots of others. – kfsone Jun 29 '13 at 23:59

4 Answers4

0

You seem to use the multiply function like its definition would be like this: void multiply(int number1, int number2); Perhaps you should post the whole definition of multiply function

aks
  • 109
  • 8
0

C-style array is not really array but a set of hacks with pointer math. You can't return such array from a function at all, and passing in is really just passing a pointer under the hood.

As pointer works fine for simple arrays and with the same syntax we can forget that difference. In that case the array externt in the parameter is completely ignored.

With 2D array it will not work the same anymore, as for the pointer math to work you must pass a proper "slice". So all but the first array extents are mandatory. You can leave the first empty or use with any number as before.

Certainly to make the function actually useful you must pass in the first extent too as a separate parameter unless the function can somehow figure it out from the data.

void multiply(const int in_array1[][10], const int in_array2[][10], int rows, int out_array[][10]);

could be a good function to multiply arrays with 10 columns and variable rows.

If you need all-variable, just pass in (int* arr, int rows, int cols) and calculate the cell position yourself.

Balog Pal
  • 16,195
  • 2
  • 23
  • 37
0

Use std::vector to model arrays unless you have a specific reason for using an array. Example:

vector< vector<int> > myArray1(3, vector<int>(2,0));
vector< vector<int> > myArray2(3, vector<int>(2,0));

// ... 

vector< vector<int> > multiply(vector< vector<int> > &myArray1, 
                               vector< vector<int> > &myArray2) {

    vector< vector<int> > result(myArray1.length(), myArray2[0].length());

    for(int i = 0; i < myArray1.length(); i++){
        for(int j = 0; j < myArray1[i].length(); j++){
            // ...
        }
    }

    return result;
}
Rayron Victor
  • 2,398
  • 1
  • 25
  • 25
0

You can accomplish this with a template definition for multiply().

template <unsigned L, unsigned M, unsigned N>
void multiply (int (&array1)[L][M], int (&array2)[M][N]) {
    //...
}

int main () {
    int array1[4][10];
    int array2[10][6];
    int array3[2][2];
    multiply(array1, array2);  // ok, inner dimensions match
    multiply(array1, array3);  // fail, inner dimensions don't match
}

The dimensions of the arrays will be determined by type deduction from the template function call.

jxh
  • 69,070
  • 8
  • 110
  • 193