fixed bugs and improved the code

This commit is contained in:
Christian Bender 2018-03-27 17:51:33 +02:00
parent 6ea11b31d6
commit 86a241bff5

View File

@ -1,27 +1,40 @@
/*A sparse matrix is a matrix which has number of zeroes greater than (m*n)/2, /*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.*/ where m and n are the dimensions of the matrix.*/
#include <iostream> #include <iostream>
using namespace std;
int main() int main()
{ {
int m,n,i,j,c=0; int m,n;
cout << "Enter dimensions of matrix:"; int counterZeros=0;
cout << "Enter dimensions of matrix (seperated with space): ";
cin >> m >> n; cin >> m >> n;
int a[m][n]; int a[m][n];
cout << "Enter matrix elements:"; cout << "Enter matrix elements:";
for(i=0;<m;i++) cout << "\n";
// reads the matrix from stdin
for(int i=0;i<m;i++)
{ {
for(j=0;j<n;j++) for(int 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) cout << "element? ";
c++; //Counting number of zeroes cin >> a[i][j];
} }
} }
if(c>((m*n)/2)) //Checking for sparse matrix
// counts the zero's
for(int i=0;i<m;i++)
{
for(int j=0;j<n;j++)
{
if(a[i][j]==0)
counterZeros++; //Counting number of zeroes
}
}
// makes sure the matrix is a sparse matrix
if(counterZeros>((m*n)/2)) //Checking for sparse matrix
cout << "Sparse matrix"; cout << "Sparse matrix";
else else
cout << "Not a sparse matrix"; cout << "Not a sparse matrix";