Description
This program implements sparse matrix representation. A sparse matrix is a matrix in which the majority of its elements are 0. In terms of computer storage, storing a sparse matrix as itself is a wastage of space. The representation of a sparse matrix discards all 0 elements and only represents the non zero elements with their position parameters.
Program
#include<stdio.h>
#include<conio.h>
void main()
{
int r,c,i,j,a[10][10],s[10][10],k;
printf("Enter the row and colum value of the matrix");
scanf("%d%d",&r,&c);
printf("Enter the elements to the matrix");
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
scanf("%d",&a[i][j]);
}
}
k=1;
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
if(a[i][j]!=0)
{
s[k][0]=i;
s[k][1]=j;
s[k][2]=a[i][j];
k++;
}}}
s[0][0]=r;
s[0][1]=c;
s[0][2]=k-1;
for(i=0;i<k;i++)
{
printf("\n");
for(j=0;j<3;j++)
{
printf(" %d",s[i][j]);
}}
getch();
}
No comments:
Post a Comment