C++ and Python Code to Find The Sum of Elements Above and Below The Main Diagonal of a Matrix

Posted in

C++ and Python Code to Find The Sum of Elements Above and Below The Main Diagonal of a Matrix

Vinay Khatri
Last updated on June 10, 2022

    Here in this article, we will code to find out the sum of non-diagonal elements and separate them with elements above the diagonal and elements below the diagonal of a matrix. To keep things simple, we will consider only one diagonal, which is the left to right one.

    Sum of Elements Above and Below the Main Diagonal of a Matrix

    For example, if we have the following matrix: 3  4  5 6  7  9 5  4  6 Then the sum of the above diagonal elements of the matrix will be 4+5+9 =18, and the sum of the below diagonal elements of the same will be 6+5+4 = 15.

    To implement the program that can do so, we need to use a nested for loop i.e. a for loop inside a for loop. Hence, with this logic, you can implement the program in any programming language.

    We will start by first asking the user to enter elements of an array. Once we get these, we will use the nested for loop to calculate the sum of the elements below and above the diagonal. Finally, we display the same.

    C++

    #include<iostream.h>
    
    #include< conio.h>
    
    #include<stdio.h>
    
    #include<math.h>
    
    void main()
    
    {
    
    clrscr();
    
    int arr[4][4],ad=0, bd=0;
    
    cout<<"Enter the elements in the array:"<<endl;
    
    for(int i=0; i<3;i++)
    
    {
    
      for(int j=0; j<3;j++)
    
        {cin>>arr[i][j];
    
         if(j>i)
    
           ad+=arr[i][j];
    
         else
    
           if(i>j)
    
          bd+=arr[i][j];
    
    }
    
    }
    
    cout<<"The sum of above diagonal elements is: "<<ad<<endl;
    
    cout<<"The sum of below diagonal elements is: "<<bd;
    
    getch();
    
    }

    Output:

    Enter the elements in the array:
    
    3
    
    4
    
    5
    
    6
    
    7
    
    9
    
    5
    
    4
    
    6
    
    
    
    
    The sum of above diagonal elements is: 18
    
    The sum of below diagonal elements is: 15

    Python

    arr=[[0,0,0],[0,0,0],[0,0,0]]
    
    ad=0
    
    bd=0
    
    print("Enter the elements in the array:")
    
    for i in range(3):
    
        for j in range(3):
    
            arr[i][j] = int(input())
    
         if j>i:
    
             ad+=arr[i][j]
    
         else:
    
             if i>j:
    
                 bd+=arr[i][j]
    
    print("The sum of above diagonal elements is:",ad)
    
    print("The sum of below diagonal elements is:",bd)

    Output:

    Enter the elements in the array:
    
    3
    
    4
    
    5
    
    6
    
    7
    
    9
    
    5
    
    4
    
    6
    
    The sum of above diagonal elements is: 18
    
    The sum of below diagonal elements is: 15

    Conclusion

    With the abovementioned code, you will be able to find the sum of elements above and below the diagonal of a matrix in C++ and Python programming languages. In the above code, we have predefined the size of the matrix. You can, however, come up with a program that asks the user to enter the size of the matrix too. Also, the code above only considers the left to right diagonal, however, you can consider the other one or both.

    People are also reading:

    Tags:
    matrix

    Leave a Comment on this Post

    0 Comments