Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions data-structures/Check_for_sparse.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#include<iostream>
using namespace std;
int main () {
int i, j, count = 0;
int row , col;
cin>>row>>col;
int a[row][col];
for (i = 0; i < row; ++i) {
for (j = 0; j < col; ++j){
cin>>a[i][j];
}
}
for (i = 0; i < row; ++i) {
for (j = 0; j < col; ++j){
if (a[i][j] == 0)
count++;
}
}
cout<<"The matrix is "<<endl;
for (i = 0; i < row; ++i) {
for (j = 0; j < col; ++j) {
cout<<a[i][j]<<" ";
}
cout<<endl;
}
cout<<"The number of zeros in the matrix are "<< count <<endl;
if (count > ((row * col)/ 2))
cout<<"This is a sparse matrix"<<endl;
else
cout<<"This is not a sparse matrix"<<endl;
return 0;
}