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

Can true be a variable name in c?

750


Explain enumerated types.

798


Wt are the Buses in C Language

2962


What does void main return?

837


How to write a code for implementing my own printf() and scanf().... Please hep me in this... I need a guidance... Can you give an coding for c... Please also explain about the header files used other than #include...

5159


What is the difference between int main and void main?

791


In cryptography, you could often break the algorithm if you know what was the original (plain) text that was encoded into the current ciphertext. This is called the plain text attack. In this simple problem, we illustrate the plain text attack on a simple substitution cipher encryption, where you know each letter has been substituted with a different letter from the alphabet but you don’t know what that letter is. You are given the cipherText as the input string to the function getwordSets(). You know that a plain text "AMMUNITION" occurs somewhere in this cipher text. Now, you have to find out which sets of characters corresponds to the encrypted form of the "AMMUNITION". You can assume that the encryption follows simple substitution only. [Hint: You could use the pattern in the "AMMUNITION" like MM occurring twice together to identify this]

2227


what are the facialities provided by you after the selection of the student.

1931


What is the use of void pointer and null pointer in c language?

852


I was asked to write a program in c which when executed displays how many no.of clients are connected to the server.

2107


What are the different types of linkage exist in c?

787


What's the best way of making my program efficient?

829


Why we not create function inside function.

1970


What is return in c programming?

719


what is reason of your company position's in india no. 1.

1970