【发布时间】:2023-04-08 08:25:02
【问题描述】:
我有以下算法非常奇怪,为了清楚起见,这里是代码
#include <iostream>
using namespace std;
#define maxn 1000
#define n 3
double sum=0;
double sum1=0;
double a[maxn][maxn];
double l[maxn][maxn];
double u[maxn][maxn];
void read(){
for(int i=1;i=n;i++){
for(int j=1;j<=n;j++){
cin>>a[i][j];
}
}
}
void decomposition(){
for(int i=1;i<=n;i++)
l[i][1]=a[i][1];
for(int j=1;j<=n;j++)
u[1][j]=a[1][j]/l[1][1];
for(int j=2;j<=n;j++){
for(int i=j;i<=n;i++){
for(int k=1;k<j;k++){
sum+=l[i][k]*u[k][j];
}
l[i][j]=a[i][j]-sum;
}
u[j][j]=1;
for(int i=j+1;i<=n;i++){
for(int k=1;k<j;k++){
sum1+=l[j][k]*u[k][i];
}
u[j][i]=(a[j][i]-sum1)/l[j][j];
}
}
}
void print(){
cout<<" L matrix "<<endl;
for(int i=1;i<=n;i++){
for(int j=1;j<=n;j++){
cout<<l[i][j]<<" ";
}
cout<<endl;
}
cout<<endl;
cout<<" U matrix "<<endl;
for(int i=1;i<=n;i++){
for(int j=1;j<=n;j++){
cout<<u[i][j]<<" ";
}
cout<<endl;
}
}
int main(){
cout<<"enter the matrix "<<endl;
read();
cout<<endl;
decomposition();
cout<<"print twwo matrix "<<endl;
print();
return 0;
}
但是当我输入矩阵时,例如我想分解这个矩阵
3 -1 2
1 2 3
2 -2 -1
程序不显示输出,只需要再次输入一些输入,我在我的代码中看不到需要输入更多的矩阵或数据,所以有什么问题?
【问题讨论】:
-
我会说我编辑的代码看起来比这个更好:D,这是个笑话
-
抱歉,不小心把包含的间距弄丢了。固定
-
在您的编程语言中数组索引是从零开始还是从一开始?
-
@MBo:在 C++ 中一般是零
-
我们有 c++ 零基数组中的一般情况,但我尝试使用基于 1 的数组,所以引入 maxn 太高了