Answer to Question #263282 in Python for ram

Question #263282

You are given a square matrix A of dimensions NxN and an integer X which means is an element of A, write a program to find P by performing the following table


1
Expert's answer
2021-11-09T17:45:56-0500
# Firstly we assign here the size of the matrix
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"""
def printSumSimple(mat, k):
 
    # our k must be or equal or smaller then our n
    if (k > n):
        return
 
    # row number of first cell in current
    # sub-square of size k x k
    for i in range(n - k + 1):
     
        # column of first cell in current
        # sub-square of size k x k
        for j in range(n - k + 1):
             
            # Calculate and print sum of
            # current sub-square
            sum = 0
            for p in range(i, k + i):
                for q in range(j, k + j):
                    sum += mat[p][q]
            print(sum, end = " ")
     
        """Line separator for sub-squares
        starting with next row"""
        print()
 


if __name__ == "__main__":
 
    mat = [[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]]
    k = 3
    printSumSimple(mat, k)
 

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS