A matrix is considered sparse when it has more zero values than non-zero values (and dense when it has more non-zero values). In this recipe, we will learn how to find out whether the specified matrix is sparse.

# Finding whether a matrix is sparse

# How to do it…

- First, specify the order of the matrix. Then, you will be prompted to enter the elements in the matrix. Let's assume that you specified the order of the matrix as 4 x 4. After entering the elements in the matrix, it might appear like this:

- Once the elements of the matrix are entered, count the number of zeros in it. A counter for this purpose is initialized to
**0**. Using nested loops, each of the matrix elements is scanned and, upon finding any zero elements, the value of the counter is incremented by 1.

- Thereafter, the following formula is used for establishing whether the matrix is sparse.

*If counter > [(the number of rows x the number of columns)/2] = Sparse Matrix*

- Depending on the result of the preceding formula, one of the following messages will be displayed on the screen as follows:

The given matrix is a sparse matrix

or

The given matrix is not a sparse matrix

The `sparsematrix.c` program for establishing whether the matrix is sparse is as follows:

#include <stdio.h>

#define max 100

/*A sparse matrix has more zero elements than nonzero elements */

void main ()

{

static int arr[max][max];

int i,j,r,c;

int ctr=0;

printf("How many rows and columns are in this matrix? ");

scanf("%d %d", &r, &c);

printf("Enter the elements in the matrix :\n");

for(i=0;i<r;i++)

{

for(j=0;j<c;j++)

{

scanf("%d",&arr[i][j]);

if (arr[i][j]==0)

++ctr;

}

}

if (ctr>((r*c)/2))

printf ("The given matrix is a sparse matrix. \n");

else

printf ("The given matrix is not a sparse matrix.\n");

printf ("There are %d number of zeros in the matrix.\n",ctr);

}

Now, let's go behind the scenes to understand the code better.

# How it works...

Because we don't want to fix the size of the matrix, we will define a macro called `max` of value 100. A matrix, or a two-dimensional array called **arr**, is defined of the order max x max. You will be prompted to enter the order of the matrix, for which you can again enter any value up to 100.

Let's assume that you’ve specified the order of the matrix as 4 x 4. You will be prompted to enter elements in the matrix. The values entered in the matrix will be in row-major order. After entering the elements, the matrix **arr** should look like *F**igure 1.22,* as follows:

A counter called `ctr` is created and is initialized to `0`. Using nested loops, each element of matrix `arr` is checked and the value of `ctr` is incremented if any element is found to be 0. Thereafter, using the `if else` statement, we will check whether the count of zero values is more than non-zero values. If the count of zero values is more than non-zero values, then the message will be displayed on the screen as follows:

The given matrix is a sparse matrix

However, failing that, the message will be displayed on the screen as follows:

The given matrix is not a sparse matrix

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

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

Let's run the generated executable file, `sparsematrix.exe`, to see the output of the program:

D:\CBook>./sparsematrixHow many rows and columns are in this matrix? 4 4Enter the elements in the matrix :0 1 0 05 0 0 90 0 3 02 0 4 0The given matrix is a sparse matrix.There are 10 zeros in the matrix.

Okay. Let's run the program again to see the output when the count of non-zero values is higher:

D:\CBook>./sparsematrixHow many rows and columns are in this matrix? 4 4Enter the elements in the matrix:1 0 3 40 0 2 98 6 5 10 7 0 4The given matrix is not a sparse matrix.There are 5 zeros in the matrix.

Voilà! We've successfully identified a sparse and a non-sparse matrix.

# 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:

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:

- Checks that if the index location of the row and column is the same, in other words, if the row number is 0 and the column number, too, is 0, then at that index location, [0][0], the matrix element must be
`1`. Similarly, if the row number is 1 and the column number, too, is 1, that is, at the [1][1] index location, the matrix element must be`1`. - Verify that the matrix element is
`0`at all the other index locations.

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>./identitymatrixHow many rows and columns are in this matrix ? 3 4An identity matrix is a square matrixBecause 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>./identitymatrixHow many rows and columns are in this matrix ? 3 3Enter elements in the matrix :1 0 11 1 00 0 1The entered matrix is1 0 11 1 00 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>./identitymatrixHow many rows and columns are in this matrix ? 3 3Enter elements in the matrix :1 0 00 1 00 0 1The entered matrix is1 0 00 1 00 0 1Matrix is an identity matrix

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