编写一个 C++ 程序,通过示例打印矩阵的下三角。下图显示了矩阵的下三角。

在此 C++ 矩阵下三角示例中,我们使用嵌套的 for 循环来迭代 lowerTriMatrix 矩阵的行和列。在该循环内,我们使用 C++ If Else 语句(if(rows >= columns))来检查行值是否大于或等于列值。如果为真,则打印该位置的矩阵项;否则,在该位置打印 0。
#include<iostream>
using namespace std;
int main()
{
int i, j, rows, columns;
cout << "\nPlease Enter Matrix rows and Columns to find Lower Triangle = ";
cin >> i >> j;
int lowerTriMatrix[i][j];
cout << "\nPlease Enter the Matrix Items\n";
for(rows = 0; rows < i; rows++) {
for(columns = 0; columns < i; columns++) {
cin >> lowerTriMatrix[rows][columns];
}
}
cout << "\nThe Result of the Lower Triangle Matrix is :\n";
for(rows = 0; rows < i; rows++)
{
cout << "\n";
for(columns = 0; columns < j; columns++)
{
if(rows >= columns)
{
cout << lowerTriMatrix[rows][columns] << " ";
}
else
{
cout << "0 ";
}
}
}
return 0;
}

在此 C++ 矩阵下三角示例中,我们使用了一些额外的 cout 语句来显示迭代的行、列值以及 lowerTriMatrix[rows][columns] 值。
#include<iostream>
using namespace std;
int main()
{
int i, j, rows, columns;
cout << "\nPlease Enter Matrix rows and Columns to find Lower Triangle = ";
cin >> i >> j;
int lowerTriMatrix[i][j];
cout << "\nPlease Enter the Matrix Items\n";
for(rows = 0; rows < i; rows++) {
for(columns = 0; columns < i; columns++) {
cin >> lowerTriMatrix[rows][columns];
}
}
cout << "\nThe Result of the Lower Triangle Matrix is :\n";
for(rows = 0; rows < i; rows++)
{
cout << "\n";
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 result of (rows >= columns) = " << (rows >= columns) <<
" and lowerTriMatrix[" << rows << "][" << columns << "] = " << lowerTriMatrix[rows][columns] << endl;
if(rows >= columns)
{
cout << lowerTriMatrix[rows][columns] << " ";
}
else
{
cout << "0 ";
}
}
}
return 0;
}
Please Enter Matrix rows and Columns to find Lower Triangle = 3 3
Please Enter the Matrix Items
1 2 3
4 5 6
7 8 9
The Result of the Lower Triangle Matrix is :
Row Iteration = 1, Row Number = 0
Column Iteration = 1, Column Number = 0, and Row Number = 0
The result of (rows >= columns) = 1 and lowerTriMatrix[0][0] = 1
1
Column Iteration = 2, Column Number = 1, and Row Number = 0
The result of (rows >= columns) = 0 and lowerTriMatrix[0][1] = 2
0
Column Iteration = 3, Column Number = 2, and Row Number = 0
The result of (rows >= columns) = 0 and lowerTriMatrix[0][2] = 3
0
Row Iteration = 2, Row Number = 1
Column Iteration = 1, Column Number = 0, and Row Number = 1
The result of (rows >= columns) = 1 and lowerTriMatrix[1][0] = 4
4
Column Iteration = 2, Column Number = 1, and Row Number = 1
The result of (rows >= columns) = 1 and lowerTriMatrix[1][1] = 5
5
Column Iteration = 3, Column Number = 2, and Row Number = 1
The result of (rows >= columns) = 0 and lowerTriMatrix[1][2] = 6
0
Row Iteration = 3, Row Number = 2
Column Iteration = 1, Column Number = 0, and Row Number = 2
The result of (rows >= columns) = 1 and lowerTriMatrix[2][0] = 7
7
Column Iteration = 2, Column Number = 1, and Row Number = 2
The result of (rows >= columns) = 1 and lowerTriMatrix[2][1] = 8
8
Column Iteration = 3, Column Number = 2, and Row Number = 2
The result of (rows >= columns) = 1 and lowerTriMatrix[2][2] = 9
9
使用 while 循环打印矩阵下三角的 C++ 程序
#include<iostream>
using namespace std;
int main()
{
int size, i, j, rows, columns;
cout << "\nPlease Enter Matrix rows and Columns to find Lower Triangle = ";
cin >> i >> j;
int lowerTriMatrix[i][j];
cout << "\nPlease Enter the Matrix Items\n";
for(rows = 0; rows < i; rows++) {
for(columns = 0; columns < i; columns++) {
cin >> lowerTriMatrix[rows][columns];
}
}
cout << "\nThe Result of the Lower Triangle Matrix is :\n";
rows = 0;
while(rows < i)
{
cout << "\n";
columns = 0;
while(columns < j)
{
if(rows >= columns)
{
cout << lowerTriMatrix[rows][columns] << " ";
}
else
{
cout << "0 ";
}
columns++;
}
rows++;
}
return 0;
}
Please Enter Matrix rows and Columns to find Lower Triangle = 3 3
Please Enter the Matrix Items
1 2 3
9 8 7
4 7 6
The Result of the Lower Triangle Matrix is :
1 0 0
9 8 0
4 7 6