pass 2d array to a function in c++
10,714
Problems in your code:
Problem 1
void input(int matrix[][],int num_h){
is not valid C++. In a multi-dimensional array, all but the first dimension must be constants. A valid declaration would be:
// Define a constant at the start of the file.
const int MATRIX_SIZE 200;
void input(int matrix[][MATRIX_SIZE],int num_h){
Problem 2
int matrix[num_h][num_h];
is not valid C++. VLA are not supported in C++.
Suggested Solution
Use std::vector<std::vector<int>>
to capture the 2D array.
Change
void input(int matrix[][],int num_h){
to
void input(std::vector<std::vector<int>>& matrix){
// You can get the size by calling matrix.size()
// There is no need to pass num_h as an argument.
Change the calling code to:
int main(){
Graph g;
int num_h;
cout<<"Enter the number of houses: ";
cin>>num_h;
// Construct a 2D array of size num_h x num_h using std::vector
std::vector<std::vector<int>> matrix(num_h, std::vector<int>(num_h));
g.input(matrix);
return 0;
}
Author by
Pranjal
Updated on June 04, 2022Comments
-
Pranjal almost 2 years
I am trying to pass a 2d array to a function in c++. The problem is that its dimension is not universal constant. I take the dimension as an input from the user and then try to pass the array. Here is what i am doind:
/* * boy.cpp * * Created on: 05-Oct-2014 * Author: pranjal */ #include<iostream> #include<cstdlib> using namespace std; class Queue{ private: int array[1000]; int front=0,rear=0; public: void enqueue(int data){ if(front!=(rear+1)%1000){ array[rear++]=data; } } int dequeue(){ return array[front++]; } bool isEmpty(){ if(front==rear) return true; else return false; } }; class Graph{ public: void input(int matrix[][],int num_h){ //this is where I am passing the matrix int distance; char ans; for(int i=0;i<num_h;i++){ for(int j=0;j<num_h;j++) matrix[i][j]=0; } for(int i=0;i<num_h;i++){ for(int j=i+1;j<num_h;j++){ cout<<"Is there route between houses "<<i<<" and "<<j<<": "; cin>>ans; if(ans=='y'){ cout<<"Enter the distance: "; cin>>distance; matrix[i][j]=matrix[j][i]=distance; } } } cout<<"The matrix is: \n"; for(int i=0;i<num_h;i++){ cout<<"\n"; for(int j=0;j<num_h;j++) cout<<matrix[i][j]<<"\t"; } } }; int main(){ Graph g; int num_h; cout<<"Enter the number of houses: "; cin>>num_h; int matrix[num_h][num_h]; g.input(matrix,num_h); //this is where I get an error saying // Invalid arguments ' Candidates are: void input(int (*)[], // int) ' return 0; }
Help much appreciated. Thank you.
-
Pranjal over 9 yearsBut then it would become a 1d array. I want to use 2d array
-
Pranjal over 9 yearsThat was the best solution. Thank you very much
-
Pranjal over 9 yearsCan you please help me with one more thing? How should I return that matrix from the input function ?
-
R Sahu over 9 years@Pranjal, you don't need to. If you pass it by reference, as I have suggested in my answer, the changes you make in
input
will be visible inmain
. -
Pranjal over 9 yearsGreat! I just tried it and it really works. Thanks for that. But I still wanted to know how to return that array. Maybe helpful for me in future
-
R Sahu over 9 years@Pranjal, you can change the return type to
std::vector<std::vector<int>>&
and usereturn matrix;
in the function.