-4

The following code finds all square sub matrix of a square matrix:

#include <stdio.h>    
int main() {
int mtrx_size = 3;
int mat[3][3] = {
    { 1, 2, 3},
    { 9,10,11},
    {17,18,19},
};

//I took 3*3 matrix for example though It works for any square matrix

int i, j, k, l, m;
int sub_mtrx_size;

for(sub_mtrx_size = mtrx_size; sub_mtrx_size > 1 ; sub_mtrx_size--) {
    m = mtrx_size - sub_mtrx_size + 1;
    for (k = 0; k <m; k++) {
        for (l = 0; l <m; l++) {
            for (i = 0; i < sub_mtrx_size; i++) {
                for (j = 0; j < sub_mtrx_size; j++) {
                    printf("%3d ", mat[i+k][j+k]);
                }
                printf("\n");
            }
            printf("\n");
        }
    }
}

return 0;

}

But I want to find all of the sub matrix of a matrix.

2013
  • 7
  • 1
  • 1
  • 6
  • 1
    The code you posted was taken from [this question](http://stackoverflow.com/questions/33595117/how-to-print-all-square-submatrices-of-square-matrix-in-c). And you made no attempt to solve this problem yourself. You're much more likely to get help here if you try to do the work yourself and post any problems you run across, rather than asking others to do your work for you. – interjay Apr 09 '16 at 16:41
  • So, all your words in the above comment are taken from Oxford Dictionary? :) Thanks for the above link... – 2013 Apr 09 '16 at 18:17

1 Answers1

0

I've tried this way to get all my sub matrix:

#include <stdio.h>

int main() {
int mtrx_size = 3;
 int mtrx_size1=3;
int mat[3][3] = {
    { 1, 2,3},
    { 9,10,4},
    { 5,6,7},
};

//Here I took example for 3*3 matrix though it should work for others

int i, j, ioff, joff, off_cnt,off_cnt1;
int sub_mtrx_size,sub_mtrx_size1;

for(sub_mtrx_size = mtrx_size; sub_mtrx_size > 0 ; sub_mtrx_size--)
  for(sub_mtrx_size1 = mtrx_size1; sub_mtrx_size1 > 0 ; sub_mtrx_size1--){
   off_cnt = mtrx_size - sub_mtrx_size + 1;
    off_cnt1 = mtrx_size1 - sub_mtrx_size1 + 1;
     for (ioff = 0; ioff < off_cnt; ioff++) {
       for (joff = 0; joff < off_cnt1; joff++) {
          for (i = 0; i < sub_mtrx_size; i++) {
            for (j = 0; j < sub_mtrx_size1; j++) {
                printf("%3d ", mat[i+ioff][j+joff]);
                }
                printf("\n");
            }
            printf("\n");
        }
    }
}

return 0;
}
2013
  • 7
  • 1
  • 1
  • 6