0

Can anyone help me? I want to pass firstMatrix, secondMatrix, and finalMatrix. So far I have this. I could make it a global variable but I still wouldnt know the actual size

int matrixSummation(int firstMatrix[][matrixSize],int secondMatrix[][matrixSize], int finalMatrix[][matrixSize], int matrixSize){
for(int row=0; row< matrixSize; row++){
    for(int col=0; col< matrixSize; col++){
        finalMatrix[row][col]=firstMatrix[row][col]+secondMatrix[row][col];
    }
}
}
  int main(int argc, char** argv) {


int matrixSize;

cout << "Enter size of your matrices: " <<endl;
cin >> matrixSize;

int firstMatrix[matrixSize][matrixSize];
int secondMatrix[matrixSize][matrixSize];
int finalMatrix[matrixSize][matrixSize];

cout <<"Enter numbers for the 1st matrix: "<<endl;
for(int row = 0; row < matrixSize; row++){
    for(int col=0; col< matrixSize; col++){
        cin >> firstMatrix[row][col];
    }
}

cout <<"Enter your numbers for the 2nd matrix: "<<endl;
for(int row = 0; row < matrixSize; row++){
    for(int col=0; col< matrixSize; col++){
        cin >> secondMatrix[row][col];
    }
}
matrixSummation(firstMatrix,secondMatrix,finalMatrix,matrixSize);
}
Yu Hao
  • 119,891
  • 44
  • 235
  • 294
user66167
  • 19
  • 1
  • 2
  • What is the error that you are getting? – brokenfoot Mar 03 '14 at 02:16
  • @brokenfoot use of undeclared identifier 'matrixSize', candidate function not viable: no known conversion from 'int [matrixSize][matrixSize]' to 'int *' for 1st argument – user66167 Mar 03 '14 at 02:21
  • OK, make `matrixSize` a global. – brokenfoot Mar 03 '14 at 02:24
  • Duplicate of [passing 2D array to function](http://stackoverflow.com/questions/8767166/passing-2d-array-to-function) (C++). The accepted answer there explains your options. – bazzargh Mar 03 '14 at 02:27
  • @bazzargh : mm..it doesn't seem to talk about the case when a 2D array with unknows dimesions has to be passed. – brokenfoot Mar 03 '14 at 02:39
  • @brokenfoot This question wants to leave matrixSize as a parameter (not unknown, but parameterisable), which he can't do with the style of declaration he used; I'm referring the questioner to the other answer because they show you different ways of passing the matrix, the 3rd one lets you pass the size as a parameter. – bazzargh Mar 03 '14 at 02:50
  • @bazzargh: Alright, that makes sense :) – brokenfoot Mar 03 '14 at 02:50

5 Answers5

1

You can use a function template:

template<std::size_t A, std::size_t B>
int matrixSummation(int (&firstMatrix)[A][B], int (&secondMatrix)[A][B], int (&finalMatrix)[A][B])

so that inside your function you can access the number of rows with A and the number of columns with B, without passing sizes around.

Shoe
  • 74,840
  • 36
  • 166
  • 272
1

This can't be done. At compile time, the function

int matrixSummation(int firstMatrix[][matrixSize],int secondMatrix[][matrixSize], int finalMatrix[][matrixSize], int matrixSize){

should know the matrix dimesions (atleast the columns).

See here: Passing array with unknown size to function


You can make a 1D array represent your 2d array:

instead of

array[ROW][COL];

use

array[ROW*COL];

And when accessing:

a[r][c]; //r<5,c<5

Use:

a[r*ROW+c];
Community
  • 1
  • 1
brokenfoot
  • 11,083
  • 10
  • 59
  • 80
1

I have been working on dynamic array size for my image file as well, in the end i just pass the array using int**.

#include <iostream>

using namespace std;

void printMatrix(int** val, int row, int col)
{
    int new_val[row][col];
    int i, j;
    for(i = 0; i < row; i++)
    {
        for(j = 0; j < col; j++)
        {
            new_val[i][j] = val[i][j];
            cout<<new_val[i][j]<<" ";
        }cout<<endl;
    }
    cout<<"test!"<<endl;
}

int main ()
{
    int i, j, k;
    int row, col;
    row = 3;
    col = 5;
    int** array = new int*[row];
    for(int i = 0; i < row; ++i)
    {
        array[i] = new int[col];
    }
    for (i = 0; i < row; i++)
    {
        for (j = 0; j < col; j++)
        {
            array[i][j] = k;
            k++;
            cout << array[i][j]<< " ";
        }
        cout<<endl;
    }
    cout<<endl;
    printMatrix(array, row, col);

    return 0;

}
randomGirl
  • 21
  • 7
1

The root of the problem is that this is not allowed in Standard C++:

cin >> matrixSize;
int firstMatrix[matrixSize][matrixSize];

Some compilers let this pass, so far; however you then run into difficulties when trying to use firstMatrix. For example the problem that you have had trying to pass it to a function.

In fact the reason this is not allowed in Standard C++ is that so far nobody has been able to come up with a proposal that does not run into difficulties sooner or later.

The best course of action would be to declare your arrays differently, in a way that is compatible with Standard C++.

In Standard C++, C-style arrays must have their dimension known at compile-time. Therefore you must use a different container.


The version that is the simplest code to write would look like:

vector< vector<int> > firstMatrix(matrixSize, vector<int>(matrixSize));

which creates a 2-D array with each dimension being matrixSize and then you can access it in the same way you would access a C-style array.

Importantly, you can pass firstMatrix to a function exactly the same way you would pass any other variable, and its sizes can be accessed via member functions of the vector, e.g. firstMatrix.size() is the number of rows, and firstMatrix[0].size() is the number of columns.


That version is a bit less efficient at run-time due to the fact that each row is stored in a separate allocation. To have a contiguous storage you can write:

vector<int> firstMatrix(matrixSize * matrixSize);

Then you simulate the rows and columns within that allocation, for example the previous code's firstMatrix[2][3] would in this solution be firstMatrix[2 * matrixSize + 3].

M.M
  • 138,810
  • 21
  • 208
  • 365
0

The big thing here is initializing a dynamically allocated 2-D array. I recommend using "vectors," but I show a pointer approach. There is no need to pass a set value for the size to the function.

int[][] matrixSummation(int firstMatrix[][],int secondMatrix[][]){
    if(firstMatrix.length != secondMatrix.length 
          || firstMatrix[].length !=secondMatrix[].length)
          return NULL;

    int **finalMatrix;
    finalMatrix= new int*[firstMatrix.length]; 

    for (int i = 0; i < 10; ++i) {
        finalMatrix[i] = new int[firstMatrix.length]; 
        //make sure you initialize each int[]__ with proper length.
    }

    for(int row=0; row< matrixSize; row++){
        for(int col=0; col< matrixSize; col++){
            finalMatrix[row][col]=firstMatrix[row][col]+secondMatrix[row][col];
        }
    }
    return finalMatrix;
}
progrenhard
  • 2,333
  • 2
  • 14
  • 14