Users Online
· Guests Online: 26
· Members Online: 0
· Total Members: 188
· Newest Member: meenachowdary055
· Members Online: 0
· Total Members: 188
· Newest Member: meenachowdary055
Forum Threads
Newest Threads
No Threads created
Hottest Threads
No Threads created
Latest Articles
Articles Hierarchy
C Program to Check if a Matrix is a Sparse Matrix
/*
* C Program to check if a Matrix is a Sparse Matrix
*/
#include <stdio.h>
void main ()
{
int matrix[10][10];
int i, j, m, n;
int sparse_counter = 0;
printf("Enter the order of the matix \n");
scanf("%d %d", &m, &n);
printf("Enter the elements of the matix \n");
for (i = 0; i < m; ++i)
{
for (j = 0; j < n; ++j)
{
scanf("%d", &matrix[i][j]);
if (matrix[i][j] == 0)
{
++sparse_counter;
}
}
}
if (sparse_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.", sparse_counter);
}
Output
$ gcc sparse_matrix.c -o sparse_matrix
$ ./sparse_matrix
Enter the order of the matix 3 3
Enter the elements of the matix
1 2 3
4 0 0
0 0 0
The given matrix is Sparse Matrix !!!
There are 5 number of Zeros.
* C Program to check if a Matrix is a Sparse Matrix
*/
#include <stdio.h>
void main ()
{
int matrix[10][10];
int i, j, m, n;
int sparse_counter = 0;
printf("Enter the order of the matix \n");
scanf("%d %d", &m, &n);
printf("Enter the elements of the matix \n");
for (i = 0; i < m; ++i)
{
for (j = 0; j < n; ++j)
{
scanf("%d", &matrix[i][j]);
if (matrix[i][j] == 0)
{
++sparse_counter;
}
}
}
if (sparse_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.", sparse_counter);
}
Output
$ gcc sparse_matrix.c -o sparse_matrix
$ ./sparse_matrix
Enter the order of the matix 3 3
Enter the elements of the matix
1 2 3
4 0 0
0 0 0
The given matrix is Sparse Matrix !!!
There are 5 number of Zeros.
Comments
No Comments have been Posted.
Post Comment
Please Login to Post a Comment.