C++ program to Convert a Matrix to Sparse Matrix

15 Mar, 2021
Given a matrix with most of its elements as 0, convert this matrix to sparse matrix in C++Examples: 
Input: Matrix:
0 1 1
1 2 2
2 1 3
3 2 5
4 3 4
Output: Sparse Matrix: 
0 1 0 0 
0 0 2 0 
0 3 0 0 
0 0 5 0 
0 0 0 4
Explanation:
Here the Sparse matrix is represented
in the form Row Column Value

Hence the row 0 1 1 means that the value
of the matrix at row 0 and column 1 is 1
Approach:  
Get the matrix with most of its elements as 0.Create a new 2D array to store the Sparse Matrix of only 3 columns (Row, Column, Value).Iterate through the Matrix, and check if an element is non zero. In this case insert this element into the Sparse Matrix.After each insertion, increment the value of variable length(here ‘len’). This will serve as the row dimension of the Sparse MatrixPrint the Dimension of the Sparse Matrix and its elements.
Get the matrix with most of its elements as 0.
Create a new 2D array to store the Sparse Matrix of only 3 columns (Row, Column, Value).
Iterate through the Matrix, and check if an element is non zero. In this case insert this element into the Sparse Matrix.
After each insertion, increment the value of variable length(here ‘len’). This will serve as the row dimension of the Sparse Matrix
Print the Dimension of the Sparse Matrix and its elements.
CPP
// C++ program to convert a Matrix// into Sparse Matrix #include <iostream>using namespace std; // Maximum number of elements in matrix#define MAX 100 // Array representation// of sparse matrix//[][0] represents row//[][1] represents col//[][2] represents valueint data[MAX][3]; // total number of elements in matrixint len; // insert elements into sparse matrixvoid insert(int r, int c, int val){    // insert row value    data[len][0] = r;     // insert col value    data[len][1] = c;     // insert element's value    data[len][2] = val;     // increment number of data in matrix    len++;} // printing Sparse Matrixvoid print(){    cout << "\nDimension of Sparse Matrix: "         << len << " x " << 3;    cout << "\nSparse Matrix: \nRow Column Value\n";     for (int i = 0; i < len; i++) {         cout << data[i][0] << " "             << data[i][1] << " "             << data[i][2] << "\n";    }} // Driver codeint main(){    int i, j;    int r = 5, c = 4;     // Get the matrix    int a[r] = { { 0, 1, 0, 0 },                    { 0, 0, 2, 0 },                    { 0, 3, 0, 0 },                    { 0, 0, 5, 0 },                    { 0, 0, 0, 4 } };     // print the matrix    cout << "\nMatrix:\n";    for (i = 0; i < r; i++) {        for (j = 0; j < c; j++) {            cout << a[i][j] << " ";        }        cout << endl;    }     // iterate through the matrix and    // insert every non zero elements    // in the Sparse Matrix    for (i = 0; i < r; i++)        for (j = 0; j < c; j++)            if (a[i][j] > 0)                insert(i, j, a[i][j]);     // Print the Sparse Matrix    print();     return 0;}
Matrix:
0 1 0 0 
0 0 2 0 
0 3 0 0 
0 0 5 0 
0 0 0 4 

Dimension of Sparse Matrix: 5 x 3
Sparse Matrix: 
Row Column Value
0 1 1
1 2 2
2 1 3
3 2 5
4 3 4
 
asme
C++ Programs
Matrix
Matrix
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Shallow Copy and Deep Copy in C++
C++ Program to check if a given String is Palindrome or not
How to find the minimum and maximum element of a Vector using STL in C++?
C Program to Swap two Numbers
Passing a function as a parameter in C++
Print a given matrix in spiral form
Program to find largest element in an array
Rat in a Maze | Backtracking-2
Sudoku | Backtracking-7
The Celebrity Problem