Let be a given two-dimensional array with zero-based indexing. The element belongs to:
the main diagonal if i = j;
the secondary diagonal if i + j = n - 1;
Example
Let’s consider the given example.
The sum of the integers on the main diagonal is 1 + 6 + 5 + 3 = 15.
The sum of the integers on the secondary diagonal is 8 + 2 + 7 + 4 = 21.
Algorithm implementation
Read the input data. The matrix elements are processed on the fly, without creating a two-dimensional array beforehand.
scanf("%d",&n);
a = b = 0;
for(i = 0; i < n; i++)
for(j = 0; j < n; j++)
{
scanf("%d",&val);In the variable , compute the sum of the elements on the main diagonal.
In the variable , compute the sum of the elements on the secondary diagonal.
if (i == j) a += val;
if (i + j == n - 1) b += val;
}Print the answer.
printf("%d %d\n",a,b);Algorithm implementation — two-dimensional array
Declare a two-dimensional array.
int m[510][510];Read the input data.
scanf("%d", &n);
for (i = 0; i < n; i++)
for (j = 0; j < n; j++)
scanf("%d", &m[i][j]);In the variable , compute the sum of the elements on the main diagonal.
In the variable , compute the sum of the elements on the secondary diagonal.
a = b = 0;Iterate through all the elements of the two-dimensional array.
for (i = 0; i < n; i++)
for (j = 0; j < n; j++)
{The element is on the main diagonal if i = j;
if (i == j) a += m[i][j];The element is on the secondary diagonal if i + j = n - 1;
if (i + j == n - 1) b += m[i][j];
}Print the answer.
printf("%d %d\n", a, b);Java implementation
import java.util.*;
public class Main
{
public static void main(String[] args)
{
Scanner con = new Scanner(System.in);
int n = con.nextInt();
int m[][] = new int[n][n];
for(int i = 0; i < n; i++)
for(int j = 0; j < n; j++)
m[i][j] = con.nextInt();
int a = 0, b = 0;
for(int i = 0; i < n; i++)
for(int j = 0; j < n; j++)
{
if (i == j) a += m[i][j];
if (i + j == n - 1) b += m[i][j];
}
System.out.println(a + " " + b);
con.close();
}
}Java implementation — on the fly
import java.util.*;
public class Main
{
public static void main(String[] args)
{
Scanner con = new Scanner(System.in);
int n = con.nextInt();
int a = 0, b = 0;
for(int i = 0; i < n; i++)
for(int j = 0; j < n; j++)
{
int val = con.nextInt();
if (i == j) a += val;
if (i + j == n - 1) b += val;
}
System.out.println(a + " " + b);
con.close();
}
}Python implementation
Read the input data.
n = int(input())
m = [list(map(int, input().split())) for _ in range(n)]In the variable , compute the sum of the elements on the main diagonal.
In the variable , compute the sum of the elements on the secondary diagonal.
a = b = 0Iterate through all the elements of the two-dimensional array.
for i in range(n):
for j in range(n):The element is on the main diagonal if i = j;
if i == j: a += m[i][j]The element is on the secondary diagonal if i + j = n - 1;
if i + j == n - 1: b += m[i][j]Print the answer.
print(a, b)Python implementation — list comprehension
Read the input data.
n = int(input())
m = [list(map(int, input().split())) for _ in range(n)]In the variable , compute the sum of the elements on the main diagonal.
In the variable , compute the sum of the elements on the secondary diagonal.
a = sum(m[i][i] for i in range(n))
b = sum(m[i][n - 1 - i] for i in range(n))Print the answer.
print(a, b)