From 005af5d471a878e35dc07ae227fa4782d9bc776f Mon Sep 17 00:00:00 2001 From: Sandra Date: Thu, 25 Aug 2022 11:54:32 +0530 Subject: [PATCH 1/3] 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"< Date: Sun, 28 Aug 2022 23:12:51 +0530 Subject: [PATCH 2/3] add tupple form --- algorithms/CPlusPlus/Arrays/sparse_matrix.cpp | 76 ++++++++++++++++--- 1 file changed, 66 insertions(+), 10 deletions(-) diff --git a/algorithms/CPlusPlus/Arrays/sparse_matrix.cpp b/algorithms/CPlusPlus/Arrays/sparse_matrix.cpp index a7526beb..786c5a76 100644 --- a/algorithms/CPlusPlus/Arrays/sparse_matrix.cpp +++ b/algorithms/CPlusPlus/Arrays/sparse_matrix.cpp @@ -1,40 +1,96 @@ /*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.*/ +it is flagged as a sparse matrix. +Also the tuple form of the matrix(if the said matrix is sparse) is displayed.*/ #include using namespace std; + +int compactMatrix(int sparseMatrix[4][5]) +{ + int size = 0; + for (int i = 0; i < 4; i++) + for (int j = 0; j < 5; j++) + if (sparseMatrix[i][j] != 0) + size++; + + /* number of columns in compactMatrix (size) must be + equal to number of non - zero elements in sparseMatrix */ + int compactMatrix[size][3]; + + // Making of new matrix + int k = 0; + for (int i = 0; i < 4; i++) + for (int j = 0; j < 5; j++) + if (sparseMatrix[i][j] != 0) + { + compactMatrix[k][0] = i; + compactMatrix[k][1] = j; + compactMatrix[k][2] = sparseMatrix[i][j]; + k++; + } + cout<<"The tuple form is:"< ((row * col)/ 2)) - cout<<"This is a sparse matrix"< Date: Thu, 1 Sep 2022 16:40:53 +0530 Subject: [PATCH 3/3] updated readme --- algorithms/CPlusPlus/README.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/algorithms/CPlusPlus/README.md b/algorithms/CPlusPlus/README.md index 0350073e..40c98abb 100644 --- a/algorithms/CPlusPlus/README.md +++ b/algorithms/CPlusPlus/README.md @@ -30,6 +30,8 @@ - [All unique triplet that sum up to given value](Arrays/three-sum.cpp) - [Next permutation](Arrays/next-permutation.cpp) - [Maximum Minimum Average of numbers](Arrays/max-min-avg.cpp) +- [Sparse Matrix](Arrays/sparse_matrix.cpp) + ## Dynamic-Programming