c program for given an n x n square matrix, find sum of all sub-squares of size k x k

#PROGRAMOLOGY is a student interest site providing best possible source codes in
different programming languages ….
CURRENTLY we are working on C PROGRAMMING in addition we are thinking about JAVA AND SCRATCH as well
//DO SUGGEST PLEASE..
GRAB AUTHOR ON
https://www.fiverr.com/am10shiekh
//kindly work on inverted commas in program if you are facing any error to get bug free result
//HAPPY PROGRAMMING


// Size of given matrix
#define n 5

// A simple function to find sum of all sub-squares of size k x k
// in a given square matrix of size n x n
void printSumSimple(int mat[][n], int k)
{
   // k must be smaller than or equal to n
   if (k > n) return;

   // row number of first cell in current sub-square of size k x k
   for (int i=0; i<n-k+1; i++)
   {
      // column of first cell in current sub-square of size k x k
      for (int j=0; j<n-k+1; j++)
      {
          // Calculate and print sum of current sub-square
          int sum = 0;
          for (int p=i; p<k+i; p++)
             for (int q=j; q<k+j; q++)
                 sum += mat[p][q];
           cout << sum << "  ";
      }

      // Line separator for sub-squares starting with next row
      cout << endl;
   }
}

// Driver program to test above function
int main()
{
    int mat[n][n] = {{1, 1, 1, 1, 1},
                     {2, 2, 2, 2, 2},
                     {3, 3, 3, 3, 3},
                     {4, 4, 4, 4, 4},
                     {5, 5, 5, 5, 5},
                    };
    int k = 3;
    printSumSimple(mat, k);
    return 0;
}

One thought on “c program for given an n x n square matrix, find sum of all sub-squares of size k x k

Leave a comment