Sum of the Diagonal of a Matrix: flowchart, algorithm and program
This program reads a 3×3 matrix into a 2D array and prints the sum of its main diagonal. The main diagonal is made of the elements where the row number and column number are equal: a[0][0], a[1][1] and a[2][2].
▶ Watch the dry run step by step →
Algorithm
- Step 1: Start.
- Step 2: a[3][3], i, j, sum = 0.
- Step 3: Print "Enter 9 elements".
- Step 4: Set i = 0.
- Step 5: If i < 3, go to step 6; otherwise go to step 11.
- Step 6: Set j = 0.
- Step 7: If j < 3, go to step 8; otherwise go to step 10.
- Step 8: Read a[i][j].
- Step 9: Increase j by 1. Go to step 7.
- Step 10: Increase i by 1. Go to step 5.
- Step 11: Set i = 0.
- Step 12: If i < 3, go to step 13; otherwise go to step 15.
- Step 13: Set sum = sum + a[i][i].
- Step 14: Increase i by 1. Go to step 12.
- Step 15: Print sum.
- Step 16: Stop.
Pseudocode
BEGIN
a[3][3], i, j, sum ← 0
PRINT "Enter 9 elements"
i ← 0
WHILE i < 3 DO
j ← 0
WHILE j < 3 DO
READ a[i][j]
j++
END WHILE
i++
END WHILE
i ← 0
WHILE i < 3 DO
sum ← sum + a[i][i]
i++
END WHILE
PRINT sum
END
Program in C, Python and Java
Sum of the Diagonal of a Matrix program in C
#include <stdio.h>
int main() {
int a[3][3], i, j, sum = 0;
printf("Enter 9 elements:\n");
for (i = 0; i < 3; i++) {
for (j = 0; j < 3; j++) {
scanf("%d", &a[i][j]);
}
}
for (i = 0; i < 3; i++) {
sum = sum + a[i][i];
}
printf("Sum of main diagonal = %d\n", sum);
return 0;
}Sum of the Diagonal of a Matrix program in Python
a = []
print("Enter 9 elements:")
for i in range(3):
row = []
for j in range(3):
row.append(int(input()))
a.append(row)
total = 0
for i in range(3):
total = total + a[i][i]
print("Sum of main diagonal =", total)Sum of the Diagonal of a Matrix program in Java
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int[][] a = new int[3][3];
int sum = 0;
System.out.println("Enter 9 elements:");
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
a[i][j] = sc.nextInt();
}
}
for (int i = 0; i < 3; i++) {
sum = sum + a[i][i];
}
System.out.println("Sum of main diagonal = " + sum);
}
}
Sample input: 1 2 3 4 5 6 7 8 9
Output: Sum of main diagonal = 15
How it works
- The nested loops with i for rows and j for columns read all 9 values into a[i][j] using scanf.
- sum starts at 0 so the first addition is not affected by any leftover value.
- A single loop runs i from 0 to 2 and adds a[i][i] to sum. Because the row and column index are the same, only diagonal elements are picked.
- For the input 1 2 3 4 5 6 7 8 9 the diagonal elements are 1, 5 and 9, so sum becomes 0+1=1, then 1+5=6, then 6+9=15.
- Finally the program prints sum, which is 15 for this sample.
Dry run (trace table)
| Step | Line | Box | sum | i | j | Output |
|---|---|---|---|---|---|---|
| 1 | 3 | Start | ||||
| 2 | 4 | a[3][3], i, j, sum = 0 | 0 | |||
| 3 | 5 | Print "Enter 9 elements" | 0 | Enter 9 elements: | ||
| 4 | 6 | i = 0 | 0 | 0 | ||
| 5 | 6 | i < 3? | 0 | 0 | ||
| 6 | 7 | j = 0 | 0 | 0 | 0 | |
| 7 | 7 | j < 3? | 0 | 0 | 0 | |
| 8 | 8 | Read a[i][j] | 0 | 0 | 0 | |
| 9 | 7 | j++ | 0 | 0 | 1 | |
| 10 | 7 | j < 3? | 0 | 0 | 1 | |
| 11 | 8 | Read a[i][j] | 0 | 0 | 1 | |
| 12 | 7 | j++ | 0 | 0 | 2 | |
| 13 | 7 | j < 3? | 0 | 0 | 2 | |
| 14 | 8 | Read a[i][j] | 0 | 0 | 2 | |
| 15 | 7 | j++ | 0 | 0 | 3 | |
| 16 | 7 | j < 3? | 0 | 0 | 3 | |
| 17 | 6 | i++ | 0 | 1 | 3 | |
| 18 | 6 | i < 3? | 0 | 1 | 3 | |
| 19 | 7 | j = 0 | 0 | 1 | 0 | |
| 20 | 7 | j < 3? | 0 | 1 | 0 |
Time and space complexity
Time: O(n²) · Space: O(n²). The nested loops read all n×n elements (n = 3 here), and the matrix takes n×n storage; the diagonal sum itself is O(n).
Common mistakes
- Using a second loop over j to add a[i][j] for every element, which gives the sum of the whole matrix (45), not the diagonal.
- Forgetting to initialise sum = 0, so garbage value is added to the result.
- Looping i up to 3 with i <= 3, which reads outside the array because valid indexes are 0 to 2.
Viva questions
Q1. Why is sum initialised to 0 in line 4?
Because sum is an accumulator and 0 is the identity for addition. If I leave it uninitialised, it holds a garbage value and the final answer will be wrong.
Q2. Why do we need two nested loops to read the matrix but only one loop to add the diagonal?
Reading needs every element, so we need row i and column j, which means two loops. The diagonal elements have row index equal to column index, so a single index i is enough to reach a[i][i].
Q3. What does a[i][i] mean, and which elements does it pick for a 3x3 matrix?
It is the element whose row number equals its column number. It picks a[0][0], a[1][1] and a[2][2], which is the main diagonal from top-left to bottom-right.
Q4. Why is there an & in scanf("%d", &a[i][j]) and what does it do?
scanf needs the address of the memory location where it should store the value. &a[i][j] gives the address of that element, so the input is stored in the matrix.
Q5. If the input is 1 2 3 / 4 5 6 / 7 8 9, what is the output? What if all nine elements are 0 or negative numbers?
The output is 1 + 5 + 9 = 15. For all zeros the sum is 0, and with negative numbers the sum is simply negative, because the code does nothing special about signs.
Q6. What is the time complexity of this program, and which part dominates?
Reading the matrix takes O(n^2) for an n x n matrix, and adding the diagonal takes O(n). So the total is O(n^2), dominated by the input loops. Here n is fixed at 3, so it is a constant amount of work.
Q7. What would you change to print the sum of the other diagonal (the anti-diagonal)?
Use a[i][2-i] in the sum loop instead of a[i][i]. For a general size n, it would be a[i][n-1-i], which gives a[0][2], a[1][1] and a[2][0].
Q8. How would you modify the program to work for an n x n matrix entered by the user?
Read n first, then use n in place of 3 in all the loops. Since the array size must be known, I would declare a[n][n] after reading n as a variable-length array, or use a large fixed size like a[100][100] and use only the first n rows and columns.
Frequently asked questions
How do you find the sum of the main diagonal of a matrix?
Add every element a[i][i], where the row index equals the column index. A single loop from 0 to n-1 is enough, so no inner loop is needed.
How do you get the other (anti) diagonal sum?
For an n×n matrix, add a[i][n-1-i]. For a 3×3 matrix that is a[0][2], a[1][1] and a[2][0].
Does this work for matrices that are not square?
The main diagonal sum is normally defined for square matrices. For a non-square matrix you would loop only up to the smaller of the row and column counts.