Advertise With us

 

Sparse Matrix C program

#include<stdio.h>
#include<conio.h>
void main()
{
int a[10][10],b[10][3],r,c,s=0,i,j;
clrscr();
printf("\nenter the order of the sparse matrix");
scanf("%d%d",&r,&c);
printf("\nenter the elements in the sparse matrix(mostly zeroes)");
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
printf("\n%d row and %d column ",i,j);
scanf("%d",&a[i][j]);
if(a[i][j]!=0)
{
b[s][0]=a[i][j];
b[s][1]=i;
b[s][2]=j;
s++;
}
}
}
printf("\nthe sparse matrix is given by");
printf("\n");
for(i=0;i<s;i++)
{
for(j=0;j<3;j++)
{
printf("%d ",b[i][j]);
}
printf("\n");
}
getch();
}

 

 
Google