0

I am trying to create MxN matrix using 2D-arrays in C++.

The createMatrix() function asks for user input for matrix items and the printMatrix() function has to print the matrix.

But the printing task is not working (I can't access the array created, I don't understand why)

I receive the error :

matrix.cpp:35:20: error: invalid types ‘int[int]’ for array subscript
    cout << matrix[i][j];

The code I'm working with is:

#include "iostream"
using namespace std;

// user input matrix
int createMatrix(int m, int n){
    int arr[m][n];
    for(int i = 0; i < m; i++){
        for(int j = 0; j < n; j++){
            cout << "A[" << i << "][" << j << "] : ";
            cin >> arr[i][j];
        }
        cout << endl;
    }
    return arr[m][n];
}

/*
void printMatrix(int matrix[][2], int m, int n){
    for(int i = 0; i < m; i++){
        for(int j = 0; j < n; j++){
            cout << matrix[i][j];
        }
    }
}
*/

int main(){ 
    int m = 2, n = 2; // m = rows, n = columns
    int matrix = createMatrix(m,n);

    //  printMatrix(matrix, m, n); // not working as sub-routine too, main target to make it work with sub-routine

    // to print matrix
    for(int i = 0; i < m; i++){
        for(int j = 0; j < n; j++){
            cout << matrix[i][j];
        }
    }

    return 0;
}
2
  • Your function signature is not correct to return a two-dimensional array. Please refer to stackoverflow.com/questions/8617683/… for more information. Commented Aug 25, 2015 at 12:28
  • you return int not int** in your function. also I could not understand how 'int arr[m][n];' line is compiled. you should not be able to use variables in array creation. Commented Aug 25, 2015 at 12:30

3 Answers 3

5

matrix is an int not an int[][]. Since it is an int there is no subscript operator and that is why you are getting the error you are getting. You are also using veriable length arrays which is not standard C++. I would suggest you change your code to use a std::vector like

std::vector<std::vector<int>> createMatrix(int m, int n)
{
    std::vector<std::vector<int>> arr(m, std::vector<int>(n));
    for(int i = 0; i < m; i++){
        for(int j = 0; j < n; j++){
            cout << "A[" << i << "][" << j << "] : ";
            cin >> arr[i][j];
        }
        cout << endl;
    }
    return arr;
}

And then main() would be:

int main(){ 
    int m = 2, n = 2; // m = rows, n = columns
    std::vector<std::vector<int>> matrix = createMatrix(m,n);

    //  printMatrix(matrix, m, n); // not working as sub-routine too, main target to make it work with sub-routine

    // to print matrix
    for(int i = 0; i < m; i++){
        for(int j = 0; j < n; j++){
            cout << matrix[i][j];
        }
    }

    return 0;
}
Sign up to request clarification or add additional context in comments.

16 Comments

@Michail What? using a vector might add a couple integers vs an array but the vector manages the memory for you, is just as fast and IMHO much easier to pass around and manipulate than a naked array.
@Michail Yes, lazy is a very good trait for a programmer. If you want complexity for the sake of it, go program in C.
@SidVishnoi you are getting a segfault as there is no return statement. instead of vector < vector <int> > printMatrix just make it void printMatrix
@SidVishnoi BTW if I were writing the print function and I have a new compiler I would use this: coliru.stacked-crooked.com/a/1cf1cdfd91912c0b
@SidVishnoi No problem with learning the basics. I am just showing you what is possible and how the language is being used now.
|
0

Your matrix is not array. it is int.

You need to work with the pointers.

1 Comment

any other way? because it works fine when i use them without sub-routines (create and print) (I'm new to C++)
0

Yes, createMatrix works but you won't be able to do anything with what it created. Because:

  1. arr[n][m] is local (and out of boundaries by the way). It is not a matrix as you probably thought but an item of arr at position [n][m].
  2. It is not well defined to declare array of fixed sizes with vary sizes that depend on function input.

You need to pass to createMatrix array from the main() as pointer (like you did in printMatrix) and createMatrix should work with it and not something local.

Now regarding your original question:

But the printing task is not working (I can't access the array created, I don't understand why)

matrix was defined as int, not as array.

int matrix = createMatrix(m,n);

2 Comments

can i use it like int matrix[m][n] = createMatrix(m,n); ?
@Sid Vishnoi Two things. One is - define your matrix in the main as int matrix[2][2] (unless m and n are macros), using variable to declare marixs size is not by the standard. Two is - No, your current createMatrix returns an item (not an array and not a matrix but a integer of arr at position [n][m]). Declare void createMatrix(int matrix[][2], int rows_num) and implement it accordingly.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.