C++: Display elements above and below the main diagonal of a matrix

Matrices are always an interesting part while learning C++. While they are interesting, they are very vital in the programming world. Simply, we are referring to the two-dimensional arrays as matrices.

Today, we take a look at a C++ program to print the elements above and below the main diagonal of a matrix. This one's pretty easy once you get to know the main diagonal of a matrix. Before I give you the hint, there's a major condition that must be applicable in order to run the program -- it should be a square matrix. Why? Because these only have diagonals. You can check it yourself. Either, you can use an if condition to check whether the given matrix is applicable. Here, we considered the matrix to be a squared one for the time being. Thus, both indices are same. Now, to the hint -- the main diagonal is the diagonal having the same row and column number or where i=j where i is the row number and j is the column number.

There it is! You already got the logic, right? We need to use two for loops to print the elements above and below the main diagonal respectively  So, the elements coming above the main diagonal would have the value of j greater than i and for the elements below the main diagonal, it's the vice-versa, i.e, j is less than i. So, we put an if condition inside the for loop to check if the condition is satisfied and print the respective elements thereby. Now, let's get into the program. I gave each in seperate functions for the sake of understanding the structure of the program. There is no need to add functions in this one as it wouldn't very much simplify the program.

/*Program to print the elements above and below the main diagonal of a square matrix.*/
#include<iostream.h>
#include<conio.h>
//Function for reading matrix.
void readm(int a[][10],int m)
{
 for(int i=0;i<m;i++)
 {
  for(int j=0;j<m;j++)
  {
   cout<<"\nEnter element ["<<i<<"]["<<j<<"]: ";
   cin>>a[i][j];
  }
 }
}
//Function to print matrix.
void printm(int a[][10],int m)
{
 for(int i=0;i<m;i++)
 {
  cout<<"\n";
  for(int j=0;j<m;j++)
   cout<<a[i][j]<<" ";
 }
}
//Function to print the elements above and below the main diagonal.
void upper(int a[][10],int m)
{
 cout<<"\nThe upper triangle is:\n";
 for(int i=0;i<m;i++)
 {
  cout<<"\n";
  for(int j=0;j<=i;j++)
   cout<<"  ";
  for(j=0;j<m;j++)
   if(j>i)
    cout<<a[i][j]<<" ";
 }
 cout<<"\nThe lower triangle is:\n";
 for(i=0;i<m;i++)
 {
  cout<<"\n";
  for(int j=0;j<m;j++)
   if(j<i)
    cout<<a[i][j]<<" ";
 }
}
//Main function.
void main()
{
 clrscr();
 int a[10][10],m;
 cout<<"\nEnter size of square matrix: ";
 cin>>m;
 readm(a,m);
 cout<<"\nPrinting.";
 printm(a,m);
 upper(a,m);
 getch();
}

The way it should appear is upto you. You can do as I did to get a clear image of those elements just like they were a part of the matrix or any way according to your wish. And if you wish to print the main diagonal along with the matrix, simply change j>i to j>=i and j<i to j<=i.

Now, was that simple? If you have any queries, feel free to post a comment here. You can also submit your request for a program at the forums page and I will try to solve it out. Hope you all loved the program. :)

Advertisements