how to generate sparse matrix in c

Answer Posted / d. prashant

/* Program of sparse matrix for 3-tuple method using array*/

#include
#define srow 50
#define mrow 20
#define mcolumn 20

main()
{
int mat[mrow][mcolumn],sparse[srow][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 for(j=0;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 {
for(j=0;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 for(j=0;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 {
for(j=0;j<3;j++)
printf("%5d",sparse[i][j]);
printf("\n");
}
}/*End of main()*/

Is This Answer Correct ?    57 Yes 50 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

How is null defined in c?

652


Explain the array representation of a binary tree in C.

728


What is linear search?

678


.main() { char *p = "hello world!"; p[0] = 'H'; printf("%s",p); }

712


what type of questions arrive in interview over c programming?

1559






Write the test cases for checking a variable having value in range -10.0 to +10.0?

1818


What does == mean in texting?

669


What is the difference between #include

and #include “header file”?

554


Are pointers integer?

551


Find duplicates in a file containing 6 digit number (like uid) in O (n) time.

2609


Who developed c language and when?

587


given post order,in order construct the corresponding binary tree

2324


What does != Mean in c?

592


What is the significance of an algorithm to C programming?

597


Why is python slower than c?

608