Check Whether a Matrix is Diagonal or not in C++

Hello Learners,

This session is based on basic matrix-related problems in C++. We usually solve it mathematically. But, here in different computer languages, we call it a 2-D array i.e. 2-Dimensional array. Every 2-D array has a number of rows and columns which can be the same or different.

C++ Program to Check Whether a given Matrix is Diagonal or not

Let us know some basic theories about 2-D arrays to solve this particular problem.
So, first among all is Square Matrix. This question needs a concept of Square as well as Diagonal Matrices. Every Diagonal Matrix is definitely Square Matrix but the opposite/inverse of this is not possible. HOW? let’s get familiar with that.

Square Matrix: Square Matrix is a 2-D array that has an equal number of rows and columns. This means, if there are 3 rows then there must be 3 columns as well. For example
1 2 3
4 5 6
7 8 9
It is a Square Matrix.

Diagonal Matrix: Diagonal Matrix is also a 2-D array that has an equal number of rows and columns but in addition to that, there is a condition. The condition is that this matrix should have non-zero elements only in the diagonal running from the upper left to the lower right. Other than that, the rest of the elements must be zero. Identity Matrix is one of its examples. Another example is:
6 0 0
0 4 0
0 0 9

Let us understand this by C++ source codes:

using namespace std;

int main()
  int m,i,j,z=0;
  cout<<"Enter size of square matrix: ";
  int arr[m][m];
  cout<<"Enter elements of your matrix: ";
            if(i!=j && arr[i][j]==0)
    cout<<"\nIt is a Diagonal Matrix";
    cout<<"\nIt is Not a Diagonal Matrix";
  return 0;

Here, we are using both i and j integer variables to control rows and columns of a given matrix respectively. Both of them are used in for loop. Also, m is an integer variable used to determine the size of a square matrix. Integer z is initialized to zero and is used to check the diagonality conditions.
In this question, the index of i must be equal to j if there are non-zero elements and elements should be equal to zero otherwise. So, if the above condition is true then the value of z will be increamented by 1 each time.

At last, z should hold a value such that the number of non-zero elements gets matched to that particular size of the square matrix. For example: if there is a 3*3 matrix then the number of zeroed elements must be 6 and in the case of 2*2 it’s 4. That’s why (m*m-m) is there as last condition. It perfectly fits into that condition.

Now, let’s have a look on the output screen:

Enter size of square matrix: 3
Enter elements of your matrix: 4 0 0
                               0 7 0
                               0 0 1
It is a Diagonal Matrix.

In this way, you could check that your matrix is diagonal or not. Hope it was easy enough to understand. Feel free to ask your queries in comment section.

Isha Rani Prasad
Codespeedy Tech Pvt. Ltd.

Leave a Reply

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