Interview Questions   Tutorials   Discussions   Programs

# C - How to generate sparse matrix in c?

156
views
asked marvit August 18, 2014 07:46 AM

How to generate sparse matrix in c?

answered By bhjahnavi   0

/* program to generate sparse matrix*/ #include void main () { static int array[10][10]; int i, j, m, n; int counter = 0; printf("Enter the order of the matix \n"); scanf("%d %d", &m, &n); printf("Enter the co-efficients of the matix \n"); for (i = 0; i < m; ++i) { for (j = 0; j < n; ++j) { scanf("%d", &array[i][j]); if (array[i][j] == 0) { ++counter; } } } if (counter > ((m * n) / 2)) { printf("The given matrix is sparse matrix \n"); } else printf("The given matrix is not a sparse matrix \n"); printf("There are %d number of zeros", counter); }
out put:
Enter the order of the matix
3 3
Enter the co-efficients of the matix
10 20 30
5 10 15
3 6 9
The given matrix is not a sparse matrix
There are 0 number of zeros

\$ a.out
Enter the order of the matix
3 3
Enter the co-efficients of the matix
5 0 0
0 0 5
0 5 0
The given matrix is sparse matrix
There are 6 number of zeros
flag

### Join with account you already have

Preview

Online-Classroom Classes
www.writeabc.com

1 person following this question

Question tags

c × 1

Asked 2 years and 4 months ago ago
Number of Views -156
Number of Answers -1
Last updated
2 years and 2 months ago ago

Ready to start your tutorial with us? That's great! Send us an email and we will get back to you as soon as possible!