How to print all square submatrices of square matrix in C?

ata.niazov picture ata.niazov · Nov 8, 2015 · Viewed 10.7k times · Source

Please, help me to find and print all square submatrices of square matrix from big to small square matrices in C programming language

I wrote code that works wrong:

int main() {
    int mtrx_size = 8;
    int mat[8][8] = {
        { 1, 2, 3, 4, 5, 6, 7, 8},
        { 9,10,11,12,13,14,15,16},
        {17,18,19,20,21,22,23,24},
        {25,26,27,28,29,30,31,32},
        {33,34,35,36,37,38,39,40},
        {41,42,43,44,45,46,47,48},
        {49,50,51,52,53,54,55,56},
        {57,58,59,60,61,62,63,64}
    };

    int i,j;
    int sub_mtrx_size;

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

    }
    return 0;

Here I need to find all 8x8, 7x7, 6x6, 5x5, 4x4, 3x3 and 2x2 submatrices.

Answer

Tom Karzes picture Tom Karzes · Nov 8, 2015

Your code was just printing a single sub-matrix for each size, positioned in the upper-left corner of the matrix. You need to add i and j offsets to get the sub-matrices at all positions:

#include <stdio.h>

int main() {
    int mtrx_size = 8;
    int mat[8][8] = {
        { 1, 2, 3, 4, 5, 6, 7, 8},
        { 9,10,11,12,13,14,15,16},
        {17,18,19,20,21,22,23,24},
        {25,26,27,28,29,30,31,32},
        {33,34,35,36,37,38,39,40},
        {41,42,43,44,45,46,47,48},
        {49,50,51,52,53,54,55,56},
        {57,58,59,60,61,62,63,64}
    };

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

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

    return 0;
}