How to add all the diagonal elements of a matrix in C++

In this tutorial, we will learn how to add all diagonal elements of a matrix in C++ with Algorithm.

In order to add all diagonal elements of a matrix, it is important to make sure that the size/length of the row and column must be same. This is because if they are not the same, then we will not get the appropriate diagonal. which will lead to a false answer. appropriate diagonal means:

consider two matrices of size 3*3 

                                 7  8  5            
                                 6  4  2                 
                                 1  2  3  

we can see that the above matrix of size 3*3 has appropriate diagonal as if we 
start from one corner, we will end up another corner.Therefore, this matrix has 
appropriate diagonal.
---------------------------------------------------------------------------------
consider two matrices of size 3*4
                                1  2  4  7 
                                2  1  7  5 
                                7  4  1  5

we can see that the above matrix of size 3*4 has inappropriate diagonal as if we
start form one corner, we are not ending up with another corner. Therefore, this 
matrix has inappropriate diagonal. 

Add all the diagonal elements of a matrix

let’s take an example to understand how the addition of all diagonal elements looks like.

Example:

consider a matrix of size 5*5

Input matrix: 
               1  5  7  4  6
               2  4  5  6  8
               8  7  5  4  1
               7  4  1  0  5
               2  6  4  7  8  

The addition of all diagonal elements should be: 
1 + 4 + 5 + 0 + 8 + 2 + 4 + 6 + 6 = 36

Note: The green bold elements in the matrix are the diagonal elements in the above examples.

Algorithm

  1. Declare and initialize a matrix of size m*n in the main function. (m = size of row, n = size column)
  2. declare and initialize a variable with zero say “sum = 0
  3. check whether ‘n‘ is equal to ‘m‘. Then proceed for next steps.
  4. Declare two loops, one form i = 0 to ‘n’ and another inside the first loop which will go from j = 0 to ‘m’.
  5. Inside the second loop check whether i = j or “i + j = n – 1″. if yes, then for every true arr[i][j], add arr[i][j] to “sum”.
  6. print the value of sum.

Note: we can use either ‘n’ or ‘m’ to check the condition for loop or inside the loop. because the value of ‘n’ and ‘m’ is equal, which is already checked in step 3.

Program to add all diagonal elements of a matrix in C++

#include <cstdlib>
#include <iostream>
#define size 100
using namespace std;

int main(int argc, char** argv) {

    int arr[size][size], n, m, sum = 0;
    
    cout<<"Enter the length of row: ";
    cin>>n;
    cout<<"Enter the length of column: ";
    cin>>m;
    
    //checking for equality of m and n
    if(n != m){
        cout<<"Length of rows and columns must be equal.";
        exit(0);
    }
    
    //Taking input in matrix 'arr' 
    cout<<"\nEnter the elements of matrix: \n";
    for(int i = 0;i < n; i++){
        for(int j = 0;j < m;j++){
            cin>>arr[i][j];
        }
    }
    
    //Finding sum of all diagonal elements
    cout<<"\nSum of All diagonal elements is: ";
    for(int i = 0; i < n; i++){
        for(int j = 0; j < m; j++){
            if(i == j || i + j == n - 1){
                sum = sum + arr[i][j];
            }
        }
    }
    
    //Printing the sum of all diagonal elements of entered matrix
    cout<<endl<<sum;
    return 0;
}

Output: 

Enter the length of row: 5
Enter the length of column: 5

Enter the elements of matrix:
1 5 7 4 6
2 4 5 6 8
8 7 5 4 1
7 4 1 0 5
2 6 4 7 8

Sum of All diagonal elements is:
36

Time complexity: O(n^2)

Note: In the program, the maximum size of the matrix is restricted to 100 only as the matrix size is initialized with “matrix [size] [size] ” and here the size = 100 from “#define size 100”

You may also learn:

  1. Find sum of each row and column of a matrix in C++
  2. Print the corner elements of the matrix using C++

Leave a Reply

Your email address will not be published. Required fields are marked *