0

I'm having a beginners doubt.

How do I pass an user inputted 2d array/vector to a function?

Since, user will be inputting the number of rows and columns, we will ask for the input.

int n, m;
int main(){
    cin >> n >> m;
    return 0;
}

Once inputted we will ask for the values of each cell.

int n, m;
int main(){
    cin >> n >> m;
      
    char ary[n][m];
    for(int i=0; i<n; i++){
        for(int j=0; j<m; j++){
             cin >> ary[i][j];
        }
    }
    return 0;
}

Once we inputted the whole matrix, we would like to print it through a function.

int n, m;

void fun(char ary[n][m]){
     for(int i=0; i<n; i++){
         for(int j=0; j<m; j++){
             cout << ary[i][j] << " ";
         }cout << endl;
     }
}

int main(){
    cin >> n >> m;
         
    char ary[n][m];
    for(int i=0; i<n; i++){
        for(int j=0; j<m; j++){
             cin >> ary[i][j];
        }
    }
         
    fun(ary);
    return 0;
}

Why doesn't this code work? I thought maybe I can use vectors, but I am quite clueless about it as well. Please help me out.

Thank you.

Here's the log after running above code: 2D array passed as param: error:

Karen Baghdasaryan
  • 2,407
  • 6
  • 24
  • 2
    `char ary[n][m];` is not standard C++. Array sizes must be compile time constants. You should take a look at `std::vector` – 463035818_is_not_an_ai Aug 09 '22 at 11:37
  • If you want to pass a matrix, then pass a matrix. in other words, put the array in a class/struct and pass that around. Easier syntax and better semantics. For arrays you can also consider to use std::array or std::vector they behave much more like classes, can be passed around as (const) reference and returned from functions (much harder to do right using "C" style arrays) – Pepijn Kramer Aug 09 '22 at 11:38
  • @463035818_is_not_a_number what does array sizes must be compile time constants mean? Can you help me out with how to do it with vector? –  अंशुल Aug 09 '22 at 11:40
  • 1
    Whichever C++ textbook or web site showed you this kind of an example, of declaring an array: throw away that book, or don't visit that web site again. You are not being taught proper C++. See a good C++ textbook for a complete discussion of how to use `std::vector` instead of arrays, like this. It is not realistic to expect to be able to learn C++ by asking one question at a time. The only way to learn C++ is with a good textbook. – Sam Varshavchik Aug 09 '22 at 11:40
  • In C, with VLA, it would be `void fun(int n, int m, char ary[n][m])`. For C++, you might use `std::vector>` instead. – Jarod42 Aug 09 '22 at 11:42
  • You'll are mentioning to use vector, I understand it's a better way but as mentioned, I'm clueless about how to use 2d vector. –  अंशुल Aug 09 '22 at 11:49
  • Well, then, this is exactly what C++ textbook's are for: to offer plenty of clues about core C++ fundamentals. Unfortunately, Stackoverflow doesn't really work very well as a textbook replacement, C++ is just too complicated to be able to learn it by asking just one question at a time. – Sam Varshavchik Aug 09 '22 at 12:09

1 Answers1

0

The std::vector has a constructor, where you can specify the size. Please see here, constructor number 3.

With that, you can write

std::vector<std::vector<int>> vec( n , std::vector<int> (m, 0)); 

Then you have a 2d vector.

You will then pass this as reference to you function.

void someFunction(std::vector<std::vector<int>>& vecPara) {
    // Do something with vecPara[i][j]
}

You were using so called VLAs (Variable length arrays). These are not part of the C++ language.

If you want to define a C-Style array in C++, then the size-value must be a constant compile time value, like for exymple "3". It cannot be the value of a variable, because that could change at runtime.

A M
  • 14,694
  • 5
  • 19
  • 44