Write a Python function called sumNxN with three parameters. The first parameter is a nested list (matrix) representing a matrix of size N x N. The second parameter is the integer N, and the third is a list of N zeros.
Modify the list of zeros so that each entry is the sum of the corresponding column. There is no return.
Note: You may assume the sizes provided are all correct.
Solution:
The code is written in python3.
Code of the function sumNxN:
def sumNxN(matrix,N,L):
for i in range(0,N):
sum = 0
for j in range(0,N):
#Sum of a column
sum += matrix[j][i]
L[i] = sum
Code with implementation:
def sumNxN(matrix,N,L):
for i in range(0,N):
sum = 0
for j in range(0,N):
#Sum of a column
sum += matrix[j][i]
L[i] = sum
#for testing
matrix=[[1,2,3,4],
[0,2,3,4],
[4,3,2,1],
[3,3,3,3]]
N=4
L=[0,0,0,0]
sumNxN(matrix,N,L)
print(L)
Screenshot of Code and output :
Get Answers For Free
Most questions answered within 1 hours.