Hello, I want to know how do I print all submatrices of a 2d array ?
I have taken a input 2d array from the user. Now how to proceed further ?
Doubt in my code
hello @yashsharma4304
fix top left corner of submatrix and bottom right corner of submatrix and then print everthing in that submatrix.
for(int x1=0;x1<m; x1++) {
for(int y1=0;y1<n;y1++) { // (x1,y1) corrdinates of top left corner
for(int x2=x1;x2<m;x2++){
for(int y2=y1;y2<n;y2++){
// (x2,y2) are corrdinates of bottom right corner
// now simply print all values of this submatrix
for(int i=x1;i<=x2;i++){
for(int j=y1;j<=y2;j++){
cout<<mat[i][j]<<" ";
}
cout<<"\n";
}
}
}
}
1 Like