C program to accept a matrix and determine whether it is a sparse matrix. A sparse matrix is matrix which has more zero elements than nonzero elements

by Nideesh C on April 13, 2011 · 0 comments

in C Programming




/* C program to accept a matrix and determine whether it is a sparse matrix. A sparse matrix is matrix which has more zero elements than nonzero elements */

#include <stdio.h>

void main ()
{
static int m1[10][10];
int i,j,m,n;
int counter=0;

printf (“Enter the order of the matrix\n”);
scanf (“%d %d”,&m,&n);

printf (“Enter the co-efficient s of the matrix\n”);
for (i=0;i<m;++i)
{
for (j=0;j<n;++j)
{
scanf (“%d”,&m1[i][j]);
if (m1[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);

}       /* END of main() */
/*———————————————-
Output
Enter the order of the matrix
2 2
Enter the co-efficient s of the matrix
1 2
3 4
The given matrix is not a sparse matrix
There are 0 number of zeros

Run 2
Enter the order of the matrix
3 3
Enter the co-efficient s of the matrix
1 0 0
0 0 1
0 1 0
The given matrix is sparse matrix
There are 6 number of zeros

———————————————–*/

Not Satisfied ? Just search & get the result

Related Posts Plugin for WordPress, Blogger...
Be Sociable, Share!

Related posts:

  1. C program to accept a matrix of order M x N and store its elements and interchange the main diagonal elements of the matrix with that of the secondary diagonal elements
  2. C program to accept a matrix of order MxN and find the trace and normal of a matrix HINT:Trace is defined as the sum of main diagonal elements and Normal is defined as square root of the sum of all the elements
  3. C program to find accept a matrix of order M x N and find the sum of the main diagonal and off diagonal elements
  4. C program to accept a matrix of given order and interchange any two rows and columns in the original matrix
  5. C program to accept a matrix of order M x N and find the sum of each row and each column of a matrix

Leave a Comment

Previous post:

Next post: