From 005af5d471a878e35dc07ae227fa4782d9bc776f Mon Sep 17 00:00:00 2001 From: Sandra Date: Thu, 25 Aug 2022 11:54:32 +0530 Subject: [PATCH] add-new-file --- algorithms/CPlusPlus/Arrays/sparse_matrix.cpp | 40 +++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 algorithms/CPlusPlus/Arrays/sparse_matrix.cpp diff --git a/algorithms/CPlusPlus/Arrays/sparse_matrix.cpp b/algorithms/CPlusPlus/Arrays/sparse_matrix.cpp new file mode 100644 index 00000000..a7526beb --- /dev/null +++ b/algorithms/CPlusPlus/Arrays/sparse_matrix.cpp @@ -0,0 +1,40 @@ +/*Description :c++ solution to check if a given matrix is sparse matrix. +If the count of zeroes present in the mmatrix is more than half the elements of the matrix, +it is flagged as a sparse matrix.*/ + + +#include +using namespace std; +int main () { + int a[10][10] = { {0, 0, 9} , {5, 0, 8} , {7, 0, 0} }; + int i, j, count = 0; + int row = 3, col = 3; + for (i = 0; i < row; ++i) { + for (j = 0; j < col; ++j){ + if (a[i][j] == 0) + count++; + } + } + cout<<"The matrix is:"< ((row * col)/ 2)) + cout<<"This is a sparse matrix"<