mirror of
https://hub.njuu.cf/TheAlgorithms/C-Plus-Plus.git
synced 2023-10-11 13:05:55 +08:00
Merge pull request #21 from shivhek25/master
Added sparse matrix program.
This commit is contained in:
commit
ba6fe803ce
28
Sparse matrix.cpp
Normal file
28
Sparse matrix.cpp
Normal file
@ -0,0 +1,28 @@
|
|||||||
|
/*A sparse matrix is a matrix which has number of zeroes greater than (m*n)/2,
|
||||||
|
where m and n are the dimensions of the matrix.*/
|
||||||
|
#include <iostream>
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
int m,n,i,j,c=0;
|
||||||
|
cout << "Enter dimensions of matrix:";
|
||||||
|
cin >> m >> n;
|
||||||
|
int a[m][n];
|
||||||
|
cout << "Enter matrix elements:";
|
||||||
|
for(i=0;<m;i++)
|
||||||
|
{
|
||||||
|
for(j=0;j<n;j++)
|
||||||
|
cin >> a[i][j];
|
||||||
|
}
|
||||||
|
for(i=0;i<m;i++)
|
||||||
|
{
|
||||||
|
for(j=0;j<n;j++)
|
||||||
|
{
|
||||||
|
if(a[i][j]==0)
|
||||||
|
c++; //Counting number of zeroes
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if(c>((m*n)/2)) //Checking for sparse matrix
|
||||||
|
cout << "Sparse matrix";
|
||||||
|
else
|
||||||
|
cout << "Not a sparse matrix";
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user