Matrices and grids · 9 / 15 Exercise Medium

Diagonals of a matrix

Objective

In a square matrix, the main diagonal runs from top left to bottom right (m[i][i]) and the secondary one from top right to bottom left (m[i][n-1-i]).

Instructions

  • The first line has the size n of the square matrix; then come n rows with n integers.
  • Show Main: x and Secondary: y with the sum of each diagonal.
Input
3
1 2 3
4 5 6
7 8 9
Output
Main: 15
Secondary: 15

Open the editor in full screen: the statement on the left and your code, the output and the tests on the right.

Start exercise