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.