C++ Practice Problem – Sum of Elements on Both Diagonals of a Matrix

Time Limit: 2s Memory Limit: 192MB Problem DescriptionCalculate the sum of the elements on both diagonals of a matrixInput FormatNumber of rows N of the matrixand an N*N integer matrix a[N][N] (N<=10)Output FormatThe sum of the elements on both diagonals of the input matrixSample Input31 2 3 4 5 6 7 8 9Sample Output25Code

#include <iostream>using namespace std;
// Function: Calculate the sum of the elements on both diagonals
int calculateDiagonalSum(int matrix[10][10], int N) {    int sum = 0;    for (int i = 0; i < N; i++) {        sum += matrix[i][i];         // Main diagonal        sum += matrix[i][N-1-i];     // Secondary diagonal    }
    // If N is odd, the center element is counted twice, need to subtract it
    if (N % 2 == 1) {        sum -= matrix[N/2][N/2];    }
    return sum;}
int main() {    int N;    cin >> N;    int matrix[10][10];
    // Read the matrix    for (int i = 0; i < N; i++) {        for (int j = 0; j < N; j++) {            cin >> matrix[i][j];        }    }
    // Call function to calculate the sum of the diagonals    int result = calculateDiagonalSum(matrix, N);    cout << result << endl;
    return 0;}

Output ResultC++ Practice Problem - Sum of Elements on Both Diagonals of a Matrix

Problem Analysis

The problem requires calculating the sum of the elements on both diagonals of an N×N matrix. The two diagonals are:

  1. Main diagonal: from the top left to the bottom right (row index equals column index)

  2. Secondary diagonal: from the top right to the bottom left (row index + column index = N-1)

Note: When N is odd, the center element will be counted twice, so it needs to be adjusted.

Methodology

  1. Read Input: First read the number of rows N, then read the N×N matrix.

  2. Calculate Diagonal Sum: Use a function to calculate the sum of both diagonals.

  3. Adjust for Duplicates: If N is odd, subtract the center element once.

  4. Output Result: Print the sum of the elements on both diagonals.

Code Explanation

  1. Function Definition: The <span>calculateDiagonalSum</span> function takes a 10×10 matrix and the size N, returning the sum of the elements on both diagonals.

  2. Calculate Diagonal Sum:

  • Main diagonal:<span>matrix[i][i]</span>

  • Secondary diagonal:<span>matrix[i][N-1-i]</span>

  • Adjust for Duplicates: When N is odd, the center element is counted twice, so it needs to be subtracted once.

  • Main Function: Read input, call the calculation function, and output the result.

    • Calculation Process:

      • Main diagonal: 1 + 5 + 9 = 15

      • Secondary diagonal: 3 + 5 + 7 = 15

      • Total: 15 + 15 = 30

      • Adjust: 30 – 5 = 25

    • Output: 25

    C++ Practice Problem - Sum of Elements on Both Diagonals of a Matrix

    C++ Basic Tutorial Collection

    C++ Practice Problem - Sum of Elements on Both Diagonals of a MatrixC++ Basic Resources

    1. C++ Output

    2. C++ Variables

    3. C++ Input

    4. C++ Expressions

    5. IF Statements

    6. IF Applications

    7. WHILE Loops

    8. FOR Loops

    9. Arrays

    10. One-dimensional Arrays

    11. Two-dimensional Arrays

    12. C++ Functions

    13. C++ File Operations – Writing Files

    If you find it useful, please click on me

    C++ Practice Problem - Sum of Elements on Both Diagonals of a MatrixC++ Practice Problem - Sum of Elements on Both Diagonals of a MatrixC++ Practice Problem - Sum of Elements on Both Diagonals of a MatrixC++ Practice Problem - Sum of Elements on Both Diagonals of a Matrix

    Leave a Comment