#include<stdio.h>
#include<conio.h>
void main()
main()
{
clrscr();
int mat[10][10],sparse[20][3];
int i,j,nzero=0,mr,mc,sr,s;
printf("Enter number of rows : ");
scanf("%d",&mr);
printf("Enter number of columns : ");
scanf("%d",&mc);
for(i=0;i<mr;I++)
for(j=0;j<mc;J++)
{
printf("Enter element for row %d,column %d : ",i+1,j+1);
scanf("%d",&mat[i][j]);
}
printf("Entered matrix is : \n");
for(i=0;i<mr;I++)
{
for(j=0;j<mc;J++)
{
printf("%6d",mat[i][j]);
if(mat[i][j]!=0)
nzero++;
}
printf("\n");
}
sr=nzero+1;
sparse[0][0]=mr;
sparse[0][1]=mc;
sparse[0][2]=nzero;
s=1;
for(i=0;i<MR;I++)
for(j=0;j<MC;J++)
{
if(mat[i][j]!=0)
{
sparse[s][0]=i+1;
sparse[s][1]=j+1;
sparse[s][2]=mat [i][j];
s++;
}
}
printf("Sparse matrix is :\n");
for(i=0;i<SR;I++)
{
for(j=0;j<3;j++)
printf("%5d",sparse[i][j]);
printf("\n");
}
getch();
}
=================*****=================***==============
Due to machine problem i am posting manual output:-
Output:
Enter no, of rows columns:
3
Enter no, of columns:
4
Enter the element for row , column
0 1 3
1 2 1
2 1 6
3 2 3
The entered matrix is:
0 3 0
0 0 1
0 6 0
0 0 3
The sparse matrix is:
0 1 3
1 2 1
2 1 6
3 2 3
No comments:
Post a Comment
Note: only a member of this blog may post a comment.