C++ 程序检查矩阵是否为稀疏矩阵

编写一个 C++ 程序,通过示例检查矩阵是否为稀疏矩阵。如果矩阵包含大量零,则任何矩阵都可以是稀疏矩阵。

在此稀疏矩阵示例中,我们检查零并增加总值。接下来,我们在 If 条件中使用稀疏矩阵数学公式(零的总数 > (行数 * 列数)/2)来返回结果。

#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;
}
Program to Check Matrix is a Sparse Matrix

使用 While 循环检查矩阵是否为稀疏矩阵的 C++ 程序

#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

在此 程序 示例中,我们添加了额外的 cout 语句来显示迭代的行值、sparseMat[rows][columns]、列值和 if 条件结果。

#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