﻿ Example of C Program/Code to Accept a matrix and determine whether it is a sparse matrix - EngineersGarage
Close or Esc Key

# Accept a matrix and determine whether it is a sparse matrix

`/*  Write a C program to accept a matric and determine whether    * * it is a sparse matrix. A sparse martix 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 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",&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);}       /* EN dof main() *//*----------------------------------------------OutputEnter the order of the matix2 2Enter the co-efficients of the matix1 23 4The given matrix is not a sparse matrixThere are 0 number of zerosRun 2Enter the order of the matix3 3Enter the co-efficients of the matix1 0 00 0 10 1 0The given matrix is sparse matrixThere are 6 number of zeros-----------------------------------------------*/`