diff --git a/Sparse2d.cpp b/Sparse2d.cpp new file mode 100644 index 0000000..ffa6f67 --- /dev/null +++ b/Sparse2d.cpp @@ -0,0 +1,89 @@ +// C++ program for Sparse Matrix Representation +// using Array +#include + + + +int main() +{ + + // Assume 4x5 sparse matrix + + int sparseMatrix[4][5] = + + { + + {0 , 0 , 3 , 0 , 4 }, + + {0 , 0 , 5 , 7 , 0 }, + + {0 , 0 , 0 , 0 , 0 }, + + {0 , 2 , 6 , 0 , 0 } + + }; + + + + 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[3][size]; + + + + // 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[0][k] = i; + + compactMatrix[1][k] = j; + + compactMatrix[2][k] = sparseMatrix[i][j]; + + k++; + + } + + + + for (int i=0; i<3; i++) + + { + + for (int j=0; j + + + +int main() +{ + + // Assume 4x5 sparse matrix + + int sparseMatrix[4][5] = + + { + + {0 , 0 , 3 , 0 , 4 }, + + {0 , 0 , 5 , 7 , 0 }, + + {0 , 0 , 0 , 0 , 0 }, + + {0 , 2 , 6 , 0 , 0 } + + }; + + + + 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[3][size]; + + + + // 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[0][k] = i; + + compactMatrix[1][k] = j; + + compactMatrix[2][k] = sparseMatrix[i][j]; + + k++; + + } + + + + for (int i=0; i<3; i++) + + { + + for (int j=0; j