Can this code be modified so that i can find the secondary diagonal elements ? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Can this code be modified so that i can find the secondary diagonal elements ?

m=[[1,2,3],[4,5,6],[1,2,3]] sum=0 for i in range (3): for j in range (3): if i==j: sum=sum+m[i][j] print(sum)

29th Nov 2018, 12:35 AM
Mara
1 Answer
+ 1
For a 3×3 matrix, the secondary diagonal is formed by these (i, j) pairs: (0, 3) (1, 2) (2, 1) (3, 0) Do you see the relation? As i increases by 1, j decreases by 1. So maybe their sum is fixed? 😉 Let me know if that doesn't make sense.
29th Nov 2018, 3:31 AM
Kishalaya Saha
Kishalaya Saha - avatar