Contact Learn C
Copy Program Share Program

Program 136:To find whether given matrix is Sparse Matrix or not

Program 136:

#include<stdio.h>
main()
{ 
 int i,j,rows,col,count=0;
 printf("Enter number of rows and columns of a matrix\n");
 scanf("%d %d",&rows,&col);
int a[rows][col];

  //Taking input of matrix
    printf("Enter Matrix 1\n");
    for(i=0;i<rows;i++)
    {
       for(j=0;j<col;j++)
      {
     scanf("%d",&a[i][j]);
     if(a[i][j]==0)
     {
      count++;
        }
      }
    }
    
    printf("Given /matrix is\n");
        for(i=0;i<rows;i++)
         {
         for(j=0;j<col;j++)
         {
           printf("%d\t",a[i][j]);
           }
  
           printf("\n");
           }
         
         if(count>((rows*col)/2))
         {
          printf("Number of zeros is %d\n",count);
          printf("Number of elements other than zero are %d\n",(rows*col)-count);
          printf("As zeros are greater than remaining elements\n");
          printf("Hence Given Matrix is Sparse Matrix \n");          
         }
         else
         {
          printf("Number of zeros is %d\n",count);
          printf("Number of elements other than zero are %d\n",(rows*col)-count);
          printf("As zeros are lesser than remaining elements\n");
          printf("Hence Given Matrix is not Sparse Matrix \n"); 
         }
      
      

}

Explanation:

//Coming Soon...

Output:

After 1st run:

To find whether given matrix is Sparse Matrix or not





After 2nd run:
















 

Download App and Learn when ever you want

Get it on PlayStore
Get it on Amazon App Store
Get it on Aptoide