Check the Sparse matrix with C.
#include <stdio.h>
int main()
{
int A[3][3];
int row, col, total = 0;
/* Input elements in matrix from user */
printf("Enter elements in matrix of 3 3x3: \n");
for (row = 0; row < 3; row++)
{
for (col = 0; col < 3; col++)
{
scanf("%d", &A[row][col]);
}
}
/* Count total number of zero elements in the matrix */
for (row = 0; row < 3; row++)
{
for (col = 0; col < 3; 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
Enter elements in matrix of size 3x3 :
1 0 0
4 5 0
0 0 0
The given matrix is a Sparse matrix.
0 Comments