Cover Image

Wednesday, 24 September 2014

Sparse matrix 3 tuple representation

#include<stdio.h>
#include<conio.h>
void main()
{
int i,j,mat[5][5],nonzero=0,row,column;
printf("Enter the size of matrix (max 5x5) : ");
scanf("%dx%d",&row,&column);
printf("Enter the elements of the matrix %dx%d :\n",row,column);
for(i=0;i<row;i++)
for(j=0;j<column;j++)
{
scanf("%d",&mat[i][j]);
if(mat[i][j]!=0)
nonzero++;
}
// 3 tuple representation
printf("\nThree tuple representation\n\n");
printf("%5d%5d%5d",row,column,nonzero);
printf("\n----------------------------\n");
for(i=0;i<row;i++)
for(j=0;j<column;j++)
{
if(mat[i][j]!=0)
printf("%5d%5d%5d\n",i,j,mat[i][j]);
}
}

Output:


No comments:

Post a Comment