This is my code to find the sum of all the elements of all columns in a given matrix:
row, col = map(int, input().split())
mat1 = [list(map(int, input().split())) for i in range(row)]
result = 0
j = 0
for i in range(row):
result += mat1[i][j]
print(result)
I am able to get the answer for the first column but I am unable to do it for other columns. Where should I increment j to +1 to get the result for other columns?
This is the input:
2 2
5 -1
19 8
This is the output:
24
7
I got 24 as the answer. How should I get 7 now?
EDIT:
Can I insert the code in a function? After I got the first column's answer, I can increment j outside the loop and then call the function? I think it is known as recursion. I don't know I am new to programming