Skip to content

Instantly share code, notes, and snippets.

@sujaykundu777
Created August 6, 2017 18:25
Show Gist options
  • Save sujaykundu777/783c9ddc141f81f58fbff191480fa787 to your computer and use it in GitHub Desktop.
Save sujaykundu777/783c9ddc141f81f58fbff191480fa787 to your computer and use it in GitHub Desktop.
Passing 2d Matrix as a Function Parameter in C++
#include <iostream>
using namespace std;
void getData(int mat[][10],int row, int col);
void display(int mat[][10],int row, int col);
int main()
{
int row, col;
int mat[10][10];
cout<<"Enter the dimensions of the matrix";
cin>>row>>col;
//Get the input for the matrix
getData(mat,row,col);
//Display the matrix
display(mat,row,col);
return 0;
}
//Function to get the input of the dimensions from the user
//Its not required to input the i for the matrix as a parameter
void getData(int mat[][10],int row,int col){
int i , j;
for(i=0;i<row;i++){
for(j=0;j<col;j++){
cout<<"Enter the elements of the matrix for :"<< i+1 << ":" << j+1 << ":";
cin>>mat[i][j];
}
}
}
void display(int mat[][10],int row,int col)
{
int i,j;
cout<<"Output matrix : "<<endl;
for(i=0;i<row;i++){
for(j=0;j<col;j++){
cout<<mat[i][j]<<" ";
if(j == col-1)
cout << endl << endl;
cout<<endl;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment