C++ Program to Check Matrix is a Sparse Matrix

Write a C++ Program to Check Matrix is a Sparse Matrix with an example. Any matrix can be a sparse matrix if it contains a large number of zeros. In this C++ sparse matrix example, we check for zeros and increment the total value. Next, we used the sparse matrix math formula (total no of zeros > (rows * columns)/2) within the If condition to return the result.

#include<iostream>
using namespace std;

int main()
{
	int i, j, rows, columns, total = 0;
	
	cout << "\nPlease Enter the Matrix rows and Columns =  ";
	cin >> i >> j;
	
	int sparseMat[i][j];
	
	cout << "\nPlease Enter the Sparse Matrix Items\n";
	for(rows = 0; rows < i; rows++)	
	{
		for(columns = 0; columns < i; columns++) 
		{
			cin >> sparseMat[rows][columns];
		}		
	}
 	
	for(rows = 0; rows < i; rows++)
  	{
  		for(columns = 0; columns < j; columns++)
  		{
  			if(sparseMat[rows][columns] == 0)
  			{
  				total++;
			}
		}
  	}

 	if(total > (rows * columns)/2)
  	{
  		cout << "\nThe Matrix you have entered is a Sparse Matrix";
	}
	else
	{
		cout << "\nThe Matrix you have entered is Not a Sparse Matrix";
	}  	

 	return 0;
}
C++ Program to Check Matrix is a Sparse Matrix 1

C++ Program to Check Matrix is a Sparse Matrix using a While loop

#include<iostream>
using namespace std;

int main()
{
	int i, j, rows, columns, total = 0;
	
	cout << "\nPlease Enter the Matrix rows and Columns =  ";
	cin >> i >> j;
	
	int sparseMat[i][j];
	
	cout << "\nPlease Enter the Sparse Matrix Items\n";
	for(rows = 0; rows < i; rows++)	
	{
		for(columns = 0; columns < i; columns++) 
		{
			cin >> sparseMat[rows][columns];
		}		
	}
 	rows = 0; 
	while(rows < i)
  	{
  		columns = 0; 
  		while(columns < j)
  		{
  			if(sparseMat[rows][columns] == 0)
  			{
  				total++;
			}
			columns++;
		}
		rows++;
  	}

 	if(total > (rows * columns)/2)
  	{
  		cout << "\nThe Matrix you have entered is a Sparse Matrix";
	}
	else
	{
		cout << "\nThe Matrix you have entered is Not a Sparse Matrix";
	}  	

 	return 0;
}
Please Enter the Matrix rows and Columns =  3 3

Please Enter the Sparse Matrix Items
1 2 3
0 0 0
0 0 1

The Matrix you have entered is a Sparse Matrix

In this C++ Sparse Matrix example, we added extra cout statements to show the iteration-wise row value, sparseMat[rows][columns], column value, and the if condition result.

#include<iostream>
using namespace std;

int main()
{
	int i, j, rows, columns, total = 0;
	
	cout << "\nPlease Enter the Matrix rows and Columns =  ";
	cin >> i >> j;
	
	int sparseMat[i][j];
	
	cout << "\nPlease Enter the Sparse Matrix Items\n";
	for(rows = 0; rows < i; rows++)	
	{
		for(columns = 0; columns < i; columns++) 
		{
			cin >> sparseMat[rows][columns];
		}		
	}
 	
	for(rows = 0; rows < i; rows++)
  	{
  		cout << "\nRow Iteration = " << rows + 1 << ", Row Number = " << rows;
  		for(columns = 0; columns < j; columns++)
  		{
  			cout << "\nColumn Iteration = " << columns + 1 << ", Column Number = " << 
			  	columns << ", and Row Number = " << rows;
			cout << "\nThe Value in sparseMat[" << rows << "][" << columns << "] = " << sparseMat[rows][columns];
			cout << "\nThe Result of (sparseMat[" << rows << "][" << columns << "] == 0) " << 
				  	(sparseMat[rows][columns] == 0) ;
  			if(sparseMat[rows][columns] == 0)
  			{
  				total++;
			}
		}
  	}

 	if(total > (rows * columns)/2)
  	{
  		cout << "\nThe Matrix you have entered is a Sparse Matrix";
	}
	else
	{
		cout << "\nThe Matrix you have entered is Not a Sparse Matrix";
	}  	

 	return 0;
}
Please Enter the Matrix rows and Columns =  2 2

Please Enter the Sparse Matrix Items
10 0
0 0

Row Iteration = 1, Row Number = 0
Column Iteration = 1, Column Number = 0, and Row Number = 0
The Value in sparseMat[0][0] = 10
The Result of (sparseMat[0][0] == 0) 0
Column Iteration = 2, Column Number = 1, and Row Number = 0
The Value in sparseMat[0][1] = 0
The Result of (sparseMat[0][1] == 0) 1
Row Iteration = 2, Row Number = 1
Column Iteration = 1, Column Number = 0, and Row Number = 1
The Value in sparseMat[1][0] = 0
The Result of (sparseMat[1][0] == 0) 1
Column Iteration = 2, Column Number = 1, and Row Number = 1
The Value in sparseMat[1][1] = 0
The Result of (sparseMat[1][1] == 0) 1
The Matrix you have entered is a Sparse Matrix