将下列程序的注释添加上#include<stdio.h> #include<iostream> #include<math.h> #define max 100 using namespace std; int main() { double A[max][max], x[max], b[max],re[max]; int n; double e; printf("请输入原方程的阶数:\n"); scanf("%d",&n); printf("请输入原方程的增广矩阵:\n"); for (int i = 0; i < n; ++i) { for (int j = 0; j < n ; ++j) { cin >> A[i][j]; //设置方程组的系数矩阵 } cin >> b[i]; //设置常数项向量 } printf("请输入初始迭代向量值:\n"); for ( i = 0; i < n; ++i) { cin >> x[i]; //设置初始向量 } printf("请输入误差上限:\n"); cin >> e; //设置误差上限 int count = 0; while (true) { cout << "第" << ++count << "次迭代: "; //输出迭代次数 int flag = 0; for ( i = 0; i < n; ++i) //迭代计算 { re[i] = 0; //清零 for (int j = 0; j < n; ++j) { if (i != j) { re[i] += - A[i][j] * x[j]; //计算每一项的系数 } } re[i] = (re[i] + b[i]) / A[i][i]; //计算新的迭代值 if (fabs(x[i] - re[i]) < e) //精度判断 ++flag; cout << re[i]<<" "; } cout << endl; if (flag == n) break; //如果最大误差已经在允许误差范围内,停止迭代 for ( i = 0; i < n; ++i) { x[i] = re[i]; //更新迭代向量 } } cout << "第" << count << "次迭代满足精度!\n"; cout << "方程组的近似解为:"; for ( i = 0; i < n; ++i) { cout << re[i] <<" " ; //输出解向量 } return 0; }
时间: 2023-06-18 13:06:18 浏览: 107
```c++
#include<stdio.h>
#include<iostream>
#include<math.h>
#define max 100
using namespace std;
int main() {
double A[max][max], x[max], b[max], re[max];
int n;
double e;
printf("请输入原方程的阶数:\n");
scanf("%d", &n);
printf("请输入原方程的增广矩阵:\n");
for (int i = 0; i < n; ++i) {
for (int j = 0; j < n; ++j) {
cin >> A[i][j]; //设置方程组的系数矩阵
}
cin >> b[i]; //设置常数项向量
}
printf("请输入初始迭代向量值:\n");
for (int i = 0; i < n; ++i) {
cin >> x[i]; //设置初始向量
}
printf("请输入误差上限:\n");
cin >> e; //设置误差上限
int count = 0;
while (true) {
cout << "第" << ++count << "次迭代: "; //输出迭代次数
int flag = 0;
for (int i = 0; i < n; ++i) //迭代计算
{
re[i] = 0; //清零
for (int j = 0; j < n; ++j) {
if (i != j) {
re[i] += -A[i][j] * x[j]; //计算每一项的系数
}
}
re[i] = (re[i] + b[i]) / A[i][i]; //计算新的迭代值
if (fabs(x[i] - re[i]) < e) //精度判断
++flag;
cout << re[i] << " ";
}
cout << endl;
if (flag == n) break; //如果最大误差已经在允许误差范围内,停止迭代
for (int i = 0; i < n; ++i) {
x[i] = re[i]; //更新迭代向量
}
}
cout << "第" << count << "次迭代满足精度!\n";
cout << "方程组的近似解为:";
for (int i = 0; i < n; ++i) {
cout << re[i] << " ";
}
return 0;
}
```
阅读全文