C++ Program to Check if it is Sparse Matrix or Not

What is a matrix:

A matrix is a rectangular numeric sequence separated into columns and rows. A matrix element, also known as an entry, is a number that occurs in a matrix.

Example:

The matrix shown above has 5 rows and 4 columns, with entries ranging from 1 to 20.

The dimensions of a matrix reflect the number of rows and columns in this sequence.

Because there are 5 rows and 4 columns, this is referred to as a 5*4 matrix.

Sparse Matrix:

A matrix is said to be sparse if the majority of its members are 0. It means that it has a small number of non-zero elements.

To determine if the given matrix is sparse or not, we first count the number of zero members in the matrix. The matrix’s size is then determined. The number of zero items in an array must be more than size/2 for the matrix to be sparse.

Program to Check if it is Sparse Matrix or Not in C++

We will create a program to determine whether or not the given matrix is sparse matrix or not.

Approach:

  • Scan the number of rows and columns of the given matrix and store it in variables row sum and colsum
  • Create a matrix with given dimensions rowsum and colsum
  • Loop through the array, counting the amount of zeros in the array and storing the result in the variable count.
  • Calculate the array’s size by multiplying the number of rows by the array’s number of columns.
  • If the count exceeds size/2, the given matrix is a sparse matrix. That signifies that the majority of the array’s elements are zeroes.
  • Otherwise, the matrix is not sparse.

Below is the implementation:

#include <bits/stdc++.h>
using namespace std;

// Driver Code
int main()
{
    int rownum, colnum;
    // Scanning number of rows of the given matrix
    cout << "Enter number of rows of the given matrix : "
         << endl;
    cin >> rownum;
    cout << endl;
    // Scanning number of columns of the given matrix
    cout << "Enter number of columns of the given matrix : "
         << endl;
    cin >> colnum;
    cout << endl;
    // creating a matrix with rownum as rows and colnum as
    // columns
    int givenMat[rownum][colnum];
    // scanning all the elements of the matrix
    for (int i = 0; i < rownum; i++) {
        cout << "enter the row elements " << endl;
        for (int j = 0; j < colnum; j++) {
            cout << "enter the element" << endl;
            cin >> givenMat[i][j];
        }
    }
    // taking a variable zeroCount which counts the
    // total number of 0 elements in the given matrix
    // and initializing it to 0
    int zeroCount = 0;
    // traversing the matrix and counting numbeer of zeros
    // in it
    for (int i = 0; i < rownum; i++) {
        for (int j = 0; j < colnum; j++) {
            // cheecking if the element is 0 or not
            // if the element is 0 then increase the zero
            // count by 1
            zeroCount = zeroCount + 1;
        }
    }
    // printing the matrix
    for (int i = 0; i < rownum; i++) {

        for (int j = 0; j < colnum; j++) {

            cout << givenMat[i][j] << " ";
        }
        cout << endl;
    }
    // checking the condition of sparse matrix
    if (zeroCount > (rownum * colnum) / 2) {
        cout
            << "the given matrix givenMat is sparse matrix";
    }
    else {
        cout << "the given matrix givenMat is not a sparse "
                "matrix";
    }

    return 0;
}

Output:

Enter number of rows of the given matrix : 
5
Enter number of columns of the given matrix : 
4
enter the row elements 
enter the element
1
enter the element
0
enter the element
0
enter the element
2
enter the row elements 
enter the element
5
enter the element
0
enter the element
0
enter the element
0
enter the row elements 
enter the element
5
enter the element
0
enter the element
0
enter the element
9
enter the row elements 
enter the element
12
enter the element
0
enter the element
8
enter the element
0
enter the row elements 
enter the element
7
enter the element
0
enter the element
0
enter the element
0
1 0 0 2 
5 0 0 0 
5 0 0 9 
12 0 8 0 
7 0 0 0 
the given matrix givenMat is sparse matrix

Drive into Python Programming Examples and explore more instances related to python concepts so that you can become proficient in generating programs in Python Programming Language.

Leave a Comment