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;
}
Ðаn
  • 10,934
  • 11
  • 59
  • 95
uzad.adblm
  • 19
  • 6
  • ***I tried this but it's not working.*** Please elaborate. – R Sahu Jan 23 '20 at 18:44
  • Does this answer your question? [Passing 2-D array as argument](https://stackoverflow.com/questions/12990568/passing-2-d-array-as-argument) – NutCracker Jan 23 '20 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. – Lou Franco Jan 23 '20 at 18:51
  • Because your size is not known at compile time -- look at this: https://stackoverflow.com/questions/936687/how-do-i-declare-a-2d-array-in-c-using-new – Lou Franco Jan 23 '20 at 18:52
  • Another alternative is to use [a simple matrix class.](https://stackoverflow.com/a/2076668/4581301) – user4581301 Jan 23 '20 at 18:54

2 Answers2

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.

anastaciu
  • 23,467
  • 7
  • 28
  • 53
  • 1
    Note that what's being called a 2D `vector` is actually a `vector` of `vector`s 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.](https://stackoverflow.com/a/2076668/4581301) – user4581301 Jan 23 '20 at 21:03
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.

Lou Franco
  • 87,846
  • 14
  • 132
  • 192
  • 1
    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. – user4581301 Jan 23 '20 at 21:02