Given a 2D array, Sum the elements of the array using Python

 Given a 2D array, Sum the elements of the array using Python

In this blog post we are taking two integers as input on one line separated by space representing rows and columns of the matrix. Following lines after that will be elements of the matrix with each element separated by space and then print the sum of all elements of 2D array using Python.

 

# Given a 2D array, Sum the elements of the array using Python
r, c = list(map(int, input().split()))
mat = []
result = 0
for i in range(r):
arr = list(map(int, input().split()))[:c]
mat.append(arr)
for ii in mat:
result += sum(ii)
print(result)




 

No comments

Powered by Blogger.