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 can I call a function with an argument list built up at run time?

930


What is wild pointer in c with example?

819


Which is an example of a structural homology?

1060


Explain how do you sort filenames in a directory?

815


Write a program to swap two numbers without using third variable in c?

880


Why main is not a keyword in c?

925


Can you add pointers together? Why would you?

897


What is the auto keyword good for?

842


What is an expression?

850


Are there namespaces in c?

805


What is register variable in c language?

846


how do you programme Carrier Sense Multiple Access

1740


In c programming language, how many parameters can be passed to a function ?

883


Should I learn data structures in c or python?

774


we need to calculating INCOME TAX for the person. The INCOME TAX is as follows:- First $10000/- of income : 4% tax Next $10000/- of income : 8% tax Next $10000/- of income : 11.5% tax above $10, 00,00/- : 15% tax What is the Solution of this Question ?

1048