C++ Program to Check if a Matrix is a Sparse Matrix on Command Line

You are currently viewing C++ Program to Check if a Matrix is a Sparse Matrix on Command Line

Hey guys! If you want to Check if a Matrix is a Sparse Matrix on Command Line using the C++ program then you are in the perfect tutorial. Let’s move on to the tutorial directly.

C++ Program to Check if a Matrix is a Sparse Matrix on Command Line

Program Code

#include <iostream>
#include <conio.h>
 
using namespace std;
int main(int argc, char **argv)
{
    cout<<"Enter the dimensions of the matrix: ";
    int m, n;
    cin>>m>>n;
    double mat[m][n];
    int zeros = 0;
    cout<<"Enter the elements of the matrix: ";
    for(int i=0; i<m; i++)
    {
        for(int j=0; j<n; j++)
        {
            cin>>mat[i][j];
            if(mat[i][j] == 0)
            {
                zeros++;
            }
        }
    }
 
    if(zeros > (m*n)/2)
    {
        cout<<"The matrix is a sparse matrix";
    }
    else
    {
        cout<<"The matrix is not a sparse matrix";
    }
 
}

Final Words

We hope this article will help you to understand about “C++ Program to Check if a Matrix is a Sparse Matrix on Command Line“. If you have any queries about the concept contact us via the comment section. Don’t forget to bookmark us.

Ranjith Kumar

Hi, I'm Ranjith a full-time Blogger, YouTuber, Affiliate Marketer, & founder of Coding Diksha. Here, I post about programming to help developers.

Leave a Reply