0

I'm trying to write a function that reads a 2D array of integers where the user is the one who enters the size of the array (it shouldn't be defined before ) . I tried this but it's not working. I appreciate your help!

enter image description here line 6 is the declaration of the function this is my code:

#include <iostream>

using namespace std;

void Fill_table(int mat[][], int s) {
    for (int i = 0; i<s; i++) {
        for (int j = 0; j<s; j++) {
            cout << "mat[" << i << "][" << j << "]:" << endl;
            cin >> mat[i][j];
        }
    }
}

int main()
{
    int n;

    cout << "Enter the size:  ";
    cin >> n;

    int a[n][n];
    Fill_table(a, n);
    return 0;
}
5
  • I tried this but it's not working. Please elaborate. Commented Jan 23, 2020 at 18:44
  • Does this answer your question? Passing 2-D array as argument Commented Jan 23, 2020 at 18:47
  • In the future, it's much better to paste the error as text instead of an image. There's a lot missing. But, your problem should be solved by the above comment. Commented Jan 23, 2020 at 18:51
  • Because your size is not known at compile time -- look at this: stackoverflow.com/questions/936687/… Commented Jan 23, 2020 at 18:52
  • Another alternative is to use a simple matrix class. Commented Jan 23, 2020 at 18:54

2 Answers 2

1

Just to give you a simple alternative. Instead of declaring a 2D array with [][], you could use [n*n] and then subscript with i*s + j. The code would be

#include <iostream>

using namespace std;

void Fill_table(int* mat, int s) {
    for (int i = 0; i<s; i++) {
        for (int j = 0; j<s; j++) {
            cout << "mat[" << i << "][" << j << "]:" << endl;
            cin >> mat[i*s+j];
        }
    }
}

int main()
{
    int n;

    cout << "Enter the size:  ";
    cin >> n;

    int a[n * n];
    Fill_table(a, n);
    return 0;
}

1D arrays can be passed as a pointer to a function. You also need to pass the size (as you did). For 2D arrays, you need to know the size at compile time.

Sign up to request clarification or add additional context in comments.

1 Comment

Caveats: int a[n * n]; is a non-Standard Variable Length Array and may not be supported by your compiler. If it is supported, you need to restrict the values of n the user can provide since n*n gets big fast. An n of as little as 500 could exhaust the available Automatic storage on a desktop PC.
1

You cannot set the size of a multidimensional array at runtime. The error

error: declaration of ‘mat’ as multidimensional array must have bounds for all dimensions except the first void Fill_table(int mat[][], int s)

tells you exactly that.

Consider using C++ data structure instead, in this case vector<vector<int>>, you have a somewhat simple how-to guide in Creating a Matrix using 2D vector in C++ – Vector of Vectors.

1 Comment

Note that what's being called a 2D vector is actually a vector of vectors and can be a surprisingly slow solution due to extra pointer chasing and lack of contiguity between the rows. It's not so bad with a large matrix, but with a small matrix, the inability to take full advantage of caching can be murderous. Here's an alternative that uses one vector and a bit of indexing math.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.