C Program to Check Matrix is a Sparse Matrix

How to write a C Program to check Matrix is a Sparse Matrix or Not with example. Any matrix is called a Sparse Matrix in C if it contains a large number of zeros. The mathematical formula behind this C Sparse Matrix is: T >= (m * n )/2, where T is the total number of zeros.

C Program to Check Matrix is a Sparse Matrix Example

This program for the sparse matrix in c allows the user to enter the number of rows and columns of a Matrix. Next, we are going to check whether the given matrix is sparse or not using For Loop.

/* C Program to check Matrix is a Sparse Matrix or Not */
 
#include<stdio.h>
 
int main()
{
 	int i, j, rows, columns, a[10][10], Total = 0;
  
 	printf("\n Please Enter Number of rows and columns  :  ");
 	scanf("%d %d", &i, &j);
 
 	printf("\n Please Enter the Matrix Elements \n");
 	for(rows = 0; rows < i; rows++)
  	{
   		for(columns = 0;columns < j;columns++)
    	{
      		scanf("%d", &a[rows][columns]);
    	}
  	}
     
 	for(rows = 0; rows < i; rows++)
  	{
   		for(columns = 0; columns < j; columns++)
    	{
    		if(a[rows][columns] == 0)
    		{
    			Total++;    		
			}
   	 	}
  	}
  	if(Total > (rows * columns)/2)
  	{
  		printf("\n The Matrix that you entered is a Sparse Matrix ");
	}
	else
	{
		printf("\n The Matrix that you entered is Not a Sparse Matrix ");
	}
  	
 	return 0;
}
C Program to check Matrix is a Sparse Matrix or Not 1

In this sparse matrix in c Program, We declared single Two dimensional arrays Multiplication of size of 10 * 10.

Below C Programming statements asks the User to enter the Matrix size (Number of rows and columns. For instance 2 Rows, 2 Columns = a[2][2] )

printf("\n Please Enter Number of rows and columns  :  ");
scanf("%d %d", &i, &j);

Next, we used for loop to iterate every cell present in a[2][2] matrix. Conditions inside for loop ((rows < i) and (columns < j)) will ensure the compiler, not to exceed the matrix limit. Otherwise, the matrix will overflow

scanf statement inside the for loop will store the user entered values in every individual array element such as a[0][0], a[0][1], …..

for(rows = 0; rows < i; rows++).
{
  for(columns = 0; columns < j; columns++)
   {
     scanf("%d", &a[rows][columns]);
   }
}

Next, we are trying to check whether each and every element in a matrix is equal to transposed matrix or not. If True then it is a sparse matrix

for(rows = 0; rows < i; rows++)
{
	for(columns = 0; columns < j; columns++)
    	{
    		if(a[rows][columns] == 0)
    		{
    			Total++;    		
		}
 	}
}

User inserted values in this C sparse matrix example are: a[3][3] = {{10, 20, 0}, { 0, 0, 0},{0, 0, 70}}

Row First Iteration: for(rows = 0; rows < 3; 0++)
The condition (0 < 3) is True.

Column First Iteration: for(columns = 0; 0 < 3; 0++)
The condition (columns < 3) is True.

if(a[rows][columns] == 0) => if(a[0][0] == 0) – This condition is False. So, it will exit from If statement

Column Second Iteration: for(columns = 1; 1 < 3; 1++)
The condition (columns < 3) is True.

if(a[0][1] == 0) – This condition is False.

Column Third Iteration: for(columns = 2; 2 < 3; 1++)
The condition (columns < 3) is True.

if(a[0][2] == 0) – This condition is True. So, Total will increment to 1.

Do the same for the remaining iterations of this sparse matrix program.

Next, we are using If Statement to check whether Total Number of Zeros is greater than or equal to (rows * columns) /2

Let me try the C sparse matrix program with other values

Please Enter Number of rows and columns  :  3 3

 Please Enter the Matrix Elements 
10 20 30
0 0 0
25 59 0

 The Matrix that you entered is Not a Sparse Matrix

Comments are closed.