Given an MxN integer matrix, write a program to - Find all zeros and replace them with the sum of their neighbor
elements.
- After replacing the zeros, set all other elements in the corresp row and column with zeros (excluding the elements which were previously zeros).
Note: Consider the upper, lower, right and left elements as
neighboring elements.
M, N = input('Enter the size of matrix M N: ').split()
M, N = int(M), int(N)
matrix = []
for x in range(M):
matrix.append(list(map(int, input("Input the elements of the matrix: ").split())))
for x in range(M):
for y in range(N):
if not((x == y) or (x == N - y - 1)):
matrix[x][y] = 0
for element in matrix:
print(*element)
Comments
Leave a comment