I am trying to make a function that prints a 2d array. Here is my code:
void print_matrix(float*** mat, int dim01, int dim02){
for(int i=0; i<dim01; i++){
for(int j=0; j<dim02; j++){
cout<<*mat[i][j]<<" ";
}
cout<<endl;
}
}
I also tried an alternative code as this threw an error.
[1] 2999 Segmentation Fault
The second function is :
void print_matrix(float** mat, int dim01, int dim02){
for(int i=0; i<dim01; i++){
for(int j=0; j<dim02; j++){
cout<<mat[i][j]<<" ";
}
cout<<endl;
}
}
This also threw a segmentation fault with 3617 as instead of 2999.
Just to make things clear. In the first function I passed array's address but in 2nd I just passed the array.
What is the error I am making here. I know that segmentation fault is because of memory management error but I can't find it!
the whole code goes like this :
#include<iostream>
using namespace std;
//some useful functions
float** cofactor(float**, int, int, int);
void input_matrix(float**, int, int);
void print_matrix(float**, int, int);
//main
int main(){
int size;
int a,b;
float** arr01; float** arr02;
cout<<"Size of matrix : ";
cin>>size;
input_matrix(arr01,size,size);
cout<<endl<<"Input Successful..."<<endl;
/*
cout<<"Enter the element to find the cofactor [i,j] : ";
cin>>a>>b;
cofactor(arr01,size,a,b);
*/
print_matrix(arr01,size,size);
return 0;
}
//definitions
void input_matrix(float** mat, int dim01, int dim02){
mat = new float*[dim01];
cout<<"Enter the matrix : "<<endl;
for(int i=0; i<dim01; i++){
mat[i]=new float[dim02];
for(int j=0; j<dim02; j++){
cin>>mat[i][j];
}
}
}
void print_matrix(float** mat, int dim01, int dim02){
for(int i=0; i<dim01; i++){
for(int j=0; j<dim02; j++){
cout<<mat[i][j]<<" ";
}
cout<<endl;
}
}
Thanks in advance!