C ++程序以矩阵形式表示线性方程
这是一个C++程序,以矩阵形式表示线性方程。
算法
Begin 1) Take the no of variables n and the coefficients of each variable as input. 2) Declare a matrix[n][n] and constant[n][1]. 3) Make for loops i = 0 to n-1 and j = 0 to n-1 to take the coefficients of each variable as the elements of the matrix. 4) Display the matrix by using nested for loops. End
示例
#include<iostream>
using namespace std;
int main(void) {
char variable[] = { 'x', 'y', 'z', 'd' };
cout << "Enter the number of variables in the
equations: ";
int n;
cin >> n;
cout << "\nEnter the coefficients of each variable for
each equation, ax + by + cz + ... = d:";
int matrix[n][n];
int constant[n][1];
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
cin >> matrix[i][j];
}
cin >> constant[i][0];
}
cout << "Matrix representation is: "<<endl;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
cout << " " << matrix[i][j];
}
cout << " " << variable[i];
cout << " = " << constant[i][0];
cout << "\n";
}
return 0;
}输出结果
Enter the number of variables in the equations: 3 Enter the coefficients of each variable for each equation, ax + by + cz + ... = d: 1 2 3 4 5 6 7 9 8 5 2 1 Matrix representation is: 1 2 3 x = 4 5 6 7 y = 9 8 5 2 z = 1
热门推荐
1 带狗狗训练
4 听力训练狗狗
6 怎训练狗狗
7 腹肌训练狗狗
8 网球训练狗狗
9 记叙文训练狗狗
10 狗狗太凶怎么训练
11 核心力量训练狗狗
12 狗狗乱叫怎么训练
13 发音训练狗狗
14 怎么训练狗狗叼东西给主人
15 荆州训练狗狗
16 篮球训练狗狗
17 做训练狗狗
18 训练狗狗坐姿