# Write a C program to check Sparse Matrix

By | 18.11.2016

## Check Sparse matrix

Write a C Program to Check a Matrix is Sparse Matrix or Not. Here’s simple Program to Check a Matrix is Sparse Matrix or Not in C Programming Language.

## What is Matrix ?

Matrix representation is a method used by a computer language to store matrices of more than one dimension in memory. C uses “Row Major”, which stores all the elements for a given row contiguously in memory.

Two-dimensional Arrays : :

The simplest form of multidimensional array is the two-dimensional array. A two-dimensional array is, in essence, a list of one-dimensional arrays. An m × n (read as m by n) order matrix is a set of numbers arranged in m rows and n columns.

To declare a two-dimensional integer array of size [x][y], you would write something as follows −

• type arrayName [ x ][ y ];

Where type can be any valid C data type and arrayName will be a valid C identifier.

Below is the source code for C Program to Check a Matrix is Sparse Matrix or Not which is successfully compiled and run on Windows System to produce desired output as shown below :

### SOURCE CODE : :

```/*  C Program to Check a Matrix is Sparse Matrix or Not  */

#include <stdio.h>

int main()
{
int A,i,j,m,n;
int row, col, total = 0;

printf("Enter no. of rows :: ");
scanf("%d", &m);
printf("\nEnter no. of cols :: ");
scanf("%d",&n);
printf("\nEnter values to the matrix :: \n");
for (i = 0; i < m; i++)
{
for (j = 0; j < n; j++)
{
printf("\nEnter a[%d][%d] value :: ",i,j);
scanf("%d", &A[i][j]);
}
}

/*
* Counts total number of zero elements in the matrix
*/
for(row=0; row<m; row++)
{
for(col=0; col<n; col++)
{
/*
* If the current element is zero
*/
if(A[row][col] == 0)
{
total++;
}
}
}

if(total >= (row * col)/2)
{
printf("\nThe given matrix is a Sparse matrix.");
}
else
{
printf("\nThe given matrix is not Sparse matrix.");
}

return 0;
}```

### Output : :

```/*  C Program to Check a Matrix is Sparse Matrix or Not  */

Enter no. of rows :: 3

Enter no. of cols :: 3

Enter values to the matrix ::

Enter a value :: 1

Enter a value :: 0

Enter a value :: 1

Enter a value :: 1

Enter a value :: 0

Enter a value :: 0

Enter a value :: 0

Enter a value :: 1

Enter a value :: 0

The given matrix is a Sparse matrix.
Process returned 0```

Above is the source code for C Program to Check a Matrix is Sparse Matrix or Not which is successfully compiled and run on Windows System.The Output of the program is shown above .

If you found any error or any queries related to the above program or any questions or reviews , you wanna to ask from us ,you may Contact Us through our contact Page or you can also comment below in the comment section.We will try our best to reach up to you in short interval.

Article Rating
Category: C Programming Matrix Programs Tags:  