SPARSE MATRIX OR NOT
Consider the following I/O samples.
SAMPLE INPUT : 3
4 0 0
3 0 0
0 0 1
#include
<stdio.h>
int main()
count+=1;
}
}
if(count>((n*m)/2))
printf("SPARSE MATRIX");
else
printf("NOT A SPARSE MATRIX");
return 0;
The given source code in C program is short and simple to understand. The source code is well tested in DEV-C++ and is completely error free. If you have any feedback about this article and want to improve this, please comment in the comment section.
3 0 0
0 0 1
SAMPLE OUTPUT: SPARSE MATRIX
SPARSE MATRIX is a matrix which contains very few non-zero elements.
In the above example, number of non - zero elements are : 6
number of zero's : 10 6 < 10
C PROGRAM:
int main()
{
int n,m,i,j,arr[100][100],count=0;
scanf("%d %d",&n,&m);
for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
{
scanf("%d",&arr[i][j]);
}
}
for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
{
if(arr[i][j] == 0)
count+=1;
}
}
if(count>((n*m)/2))
printf("SPARSE MATRIX");
else
printf("NOT A SPARSE MATRIX");
return 0;
}
OUTPUT:
The given source code in C program is short and simple to understand. The source code is well tested in DEV-C++ and is completely error free. If you have any feedback about this article and want to improve this, please comment in the comment section.
Comments
Post a Comment