There's more...

How about finding an identity matrix, in other words, finding out whether the matrix entered by the user is an identity matrix or not. Let me tell you—a matrix is said to be an identity matrix if it is a square matrix and all the elements of the principal diagonal are ones and all other elements are zeros. An identity matrix of the order 3 x 3 may appear as follows:

Figure 1.23

In the preceding diagram, you can see that the principal diagonal elements of the matrix are 1's and the rest of them are 0's. The index or subscript location of the principal diagonal elements will be arr[0][0], arr[1][1], and arr[2][2], so the following procedure is followed to find out whether the matrix is an identity matrix or not:

If both the preceding conditions are met, then the matrix is an identity matrix, or else it is not.

The identitymatrix.c program to establish whether the entered matrix is an identity matrix or not is given as follows:

    #include <stdio.h>
#define max 100
/* All the elements of the principal diagonal of the Identity matrix are ones and rest all are zero elements */
void main ()
{
static int arr[max][max];
int i,j,r,c, bool;
printf("How many rows and columns are in this matrix ? ");
scanf("%d %d", &r, &c);
if (r !=c)
{
printf("An identity matrix is a square matrix\n");
printf("Because this matrix is not a square matrix, so it is not an
identity matrix\n");
}
else
{
printf("Enter elements in the matrix :\n");
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
scanf("%d",&arr[i][j]);
}
}
printf("\nThe entered matrix is \n");
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
printf("%d\t",arr[i][j]);
}
printf("\n");
}
bool=1;
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
if(i==j)
{
if(arr[i][j] !=1)
{
bool=0;
break;
}
}
else
{
if(arr[i][j] !=0)
{
bool=0;
break;
}
}
}
}
if(bool)
printf("\nMatrix is an identity matrix\n");
else
printf("\nMatrix is not an identity matrix\n");
}
}

Let's use GCC to compile the identitymatrix.c program as follows:

D:\CBook>gcc identitymatrix.c -o identitymatrix

No error is generated. This means the program is compiled perfectly and an executable file is generated. Let's run the generated executable file. First, we will enter a non-square matrix:

D:\CBook>./identitymatrix
How many rows and columns are in this matrix ? 3 4
An identity matrix is a square matrix
Because this matrix is not a square matrix, so it is not an identity matrix

Now, let's run the program again; this time, we will enter a square matrix

D:\CBook>./identitymatrix 
How many rows and columns are in this matrix ? 3 3
Enter elements in the matrix :
1 0 1
1 1 0
0 0 1

The entered matrix is
1 0 1
1 1 0
0 0 1

Matrix is not an identity matrix

Because a non-diagonal element in the preceding matrix is 1, it is not an identity matrix. Let's run the program again:

D:\CBook>./identitymatrix 
How many rows and columns are in this matrix ? 3 3
Enter elements in the matrix :
1 0 0
0 1 0
0 0 1
The entered matrix is
1 0 0
0 1 0
0 0 1
Matrix is an identity matrix

Now, let's move on to the next recipe!