C++ 程序查找矩阵是否为单位矩阵

编写一个 C++ 程序,通过示例查找矩阵是否为单位矩阵。C++ 单位矩阵是一个主对角线元素为 1,所有其他元素都为零的方阵。

在此 C++ 单位矩阵示例中,我们允许用户输入行数和列数。接下来,我们使用 C++ 嵌套 for 循环遍历矩阵。在 for 循环中,我们使用 C++ If 语句检查主对角线元素是否为 1,非对角线元素是否为零。如果为真,我们将 Flag 值更改为 0,并应用 break 语句退出 for 循环。接下来,我们使用 C++ If Else 语句根据 Flag 值打印单位矩阵的输出。

// C++ Program to Check for Identity Matrix using For Loop 
#include<iostream>
using namespace std;

int main()
{
	int i, j, rows, columns, flag = 1;
	
	cout << "\nPlease Enter Matrix rows and Columns to find Identity Matrix =  ";
	cin >> i >> j;
	
	int identMatrix[i][j];
	
	cout << "\nPlease Enter the Identity Matrix Items\n";
	for(rows = 0; rows < i; rows++)	{
		for(columns = 0; columns < i; columns++) {
			cin >> identMatrix[rows][columns];
		}		
	}

 	for(rows = 0; rows < i; rows++)
  	{
   		for(columns = 0; columns < j; columns++)
    	{
    		if(identMatrix[rows][columns] != 1 && identMatrix[columns][rows] != 0)
    		{
    			flag = 0;
    			break;
			}
   	 	}
  	}
  	if(flag == 1)
  	{
  		cout << "\nThe Matrix that you entered is an Identity Matrix";
	}
	else
	{
		cout << "\nThe Matrix that you entered is Not an Identity Matrix";
	}  	

 	return 0;
}
C++ Program to find Matrix is an Identity Matrix

C++ 单位矩阵代码 使用 Else If 语句检查给定的矩阵是否为单位矩阵。

#include<iostream>
using namespace std;

int main()
{
	int i, j, rows, columns, flag = 1;
	
	cout << "\nPlease Enter Matrix rows and Columns to find Identity Matrix =  ";
	cin >> i >> j;
	
	int identMatrix[i][j];
	
	cout << "\nPlease Enter the Identity Matrix Items\n";
	for(rows = 0; rows < i; rows++)	{
		for(columns = 0; columns < i; columns++) {
			cin >> identMatrix[rows][columns];
		}		
	}

 	for(rows = 0; rows < i; rows++)
  	{
   		for(columns = 0; columns < j; columns++)
    	{
    		if(rows == columns && identMatrix[rows][columns] != 1)
    		{
    			flag = 0;
			}
			else if(rows != columns && identMatrix[rows][columns] != 0)
			{
				flag = 0;
			}
   	 	}
  	}
  	if(flag == 1)
  	{
  		cout << "\nThe Matrix that you entered is an Identity Matrix";
	}
	else
	{
		cout << "\nThe Matrix that you entered is Not an Identity Matrix";
	}  	

 	return 0;
}
Please Enter Matrix rows and Columns to find Identity Matrix =  3 3

Please Enter the Identity Matrix Items
1 0 0
0 1 0
0 0 1

The Matrix that you entered is an Identity Matrix

让我尝试一个非单位矩阵。

Please Enter Matrix rows and Columns to find Identity Matrix =  3 3

Please Enter the Identity Matrix Items
1 0 0
0 0 1
0 1 0

The Matrix that you entered is Not an Identity Matrix

C++ 程序使用 While 循环查找矩阵是否为单位矩阵

#include<iostream>
using namespace std;

int main()
{
	int i, j, rows, columns, flag = 1;
	
	cout << "\nPlease Enter Matrix rows and Columns to find Identity Matrix =  ";
	cin >> i >> j;
	
	int identMatrix[i][j];
	
	cout << "\nPlease Enter the Identity Matrix Items\n";
	for(rows = 0; rows < i; rows++)	{
		for(columns = 0; columns < i; columns++) {
			cin >> identMatrix[rows][columns];
		}		
	}
	
	rows = 0; 
 	while(rows < i )
  	{
  		columns = 0; 
   		while(columns < j)
    	{
    		if(rows == columns && identMatrix[rows][columns] != 1)
    		{
    			flag = 0;
			}
			else if(rows != columns && identMatrix[rows][columns] != 0)
			{
				flag = 0;
			}
			columns++;
   	 	}
   	 	rows++;
  	}
  	if(flag == 1)
  	{
  		cout << "\nThe Matrix that you entered is an Identity Matrix";
	}
	else
	{
		cout << "\nThe Matrix that you entered is Not an Identity Matrix";
	}  	

 	return 0;
}
Please Enter Matrix rows and Columns to find Identity Matrix =  3 3

Please Enter the Identity Matrix Items
1 0 0
0 1 0
0 0 1

The Matrix that you entered is an Identity Matrix