# C++ Program to Compute the Sum of Diagonals of a Matrix

14/06/2024

To compute the sum of the diagonals of a matrix, we need to consider both the primary (main) diagonal and the secondary (anti) diagonal. The primary diagonal consists of elements where the row index is equal to the column index. The secondary diagonal consists of elements where the sum of the row index and column index is equal to the size of the matrix minus one.

Problem Statement

Given a square matrix, compute the sum of its primary and secondary diagonals.

Approach

1. Primary Diagonal: Sum elements where the row index equals the column index.
2. Secondary Diagonal: Sum elements where the sum of the row and column indices equals the size of the matrix minus one.
3. Avoid Double Counting: If the matrix size is odd, the central element will be counted twice. Adjust the sum to subtract the central element once in this case.

Example

For a 3×3 matrix:

1 2 3

4 5 6

7 8 9

Primary diagonal sum: 1+5+9=15

Secondary diagonal sum: 3+5+7=15

Total sum of diagonals: 15+15−5=25 (Subtract the central element 5 once because it’s counted twice).

C++ Code Implementation

Here is the C++ program to compute the sum of the diagonals of a matrix:

#include <iostream>

#include <vector>

using namespace std;

// Function to compute the sum of the diagonals of a matrix

int sumOfDiagonals(const vector<vector<int>>& matrix) {

int n = matrix.size();

int primaryDiagonalSum = 0;

int secondaryDiagonalSum = 0;

for (int i = 0; i < n; ++i) {

primaryDiagonalSum += matrix[i][i];

secondaryDiagonalSum += matrix[i][n – 1 – i];

}

int totalDiagonalSum = primaryDiagonalSum + secondaryDiagonalSum;

// If the matrix has an odd size, subtract the central element which is counted twice

if (n % 2 == 1) {

totalDiagonalSum -= matrix[n / 2][n / 2];

}

}

// Function to print a matrix

void printMatrix(const vector<vector<int>>& matrix) {

for (const auto& row : matrix) {

for (const auto& elem : row) {

cout << elem << ” “;

}

cout << endl;

}

}

int main() {

// Initializing a 3×3 matrix

vector<vector<int>> matrix = {

{1, 2, 3},

{4, 5, 6},

{7, 8, 9}

};

cout << “Matrix:” << endl;

printMatrix(matrix);

// Computing the sum of the diagonals

int diagonalSum = sumOfDiagonals(matrix);

cout << “Sum of the diagonals: ” << diagonalSum << endl;

return 0;

}

Explanation

1. Function sumOfDiagonals:

• Parameters: Takes a 2D vector (matrix).
• Primary Diagonal Sum: Sums elements where the row index equals the column index.
• Secondary Diagonal Sum: Sums elements where the sum of the row index and column index equals n−1n – 1n−1.
• Total Sum Calculation: Sums the primary and secondary diagonal sums.
• Adjustment for Odd-sized Matrix: Subtracts the central element once if the matrix size is odd, to avoid double counting.
• Return Value: Returns the total diagonal sum.
1. Function printMatrix:

Prints the matrix in a formatted way for better visualization.

1. Main Function:
• Initializes a sample 3×3 matrix.
• Prints the matrix.
• Calls the sumOfDiagonals function to compute the sum of the diagonals.
• Outputs the result of the diagonal sum.