[解决了谢谢]我用C++开发了下面的代码,用高斯-塞德尔方法求解线性方程组,但在运行时填充数组时,我似乎遇到了一个问题,我无法理解。这是我的密码。。。
#include<stdio.h>
int main(void)
{
float a[10][10],b[10],x[10],y[10];
int n=0,m=0,i=0,j=0;
printf("Enter size of 2d array(Square matrix) : ");
scanf("%d",&n);
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
printf("Enter values no. %d %d :",i,j);
scanf("%f",&a[i][j]);
}
}
printf("nEnter Values to the right side of equationn");
for(i=0;i<n;i++)
{
printf("Enter values no. %d :",i,j);
scanf("%f",&b[i]);
}
printf("Enter initial values of xn");
for(i=0;i<n;i++)
{
printf("Enter values no. %d :",i);
scanf("%f",&x[i]);
}
printf("nEnter the no. of iteration : " );
scanf("%d",&m);
while(m>0)
{
for(i=0;i<n;i++)
{
y[i]=(b[i]/a[i][i]);
for(j=0;j<n;j++)
{
if(j==i)
continue;
y[i]=y[i]-((a[i][j]/a[i][i])*x[j]);
x[i]=y[i];
}
printf("x%d = %f ",i+1,y[i]);
}
printf("nn");
m--;
}
return 0;
您没有为EquationHolder
分配第二个维度。由于是一个2D矩阵,您也必须分配第二个维度。将双for
循环更改为以下内容:
float ** EquationHolder=new float *[3];
for (int i=0; i<NumEquations; i++)
{
EquationHolder[i] = new float[3];
cout<<"Please Enter The Information Of Equation ("<<i+1<<")...n";
for (int j=0; j<NumEquations; j++)
{
cout<<"X"<<j+1<<": ";
cin>>EquationHolder[i][j];
}
}
然而,我建议使用std::vector<std::vector<double>>
而不是C原始数组,这样更安全。
第一件事:
为所有阵列分配存储:
int NumEquations=3;
// Equation Holder...
float ** EquationHolder= new float *[3];
for (int i=0; i<NumEquations; i++)
{
EquationHolder[ i] = new float[3];
// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
cout<<"Please Enter The Information Of Equation ("<<i+1<<")...n";
for (int j=0; j<NumEquations; j++)
{
cout<<"X"<<j+1<<": ";
cin>>EquationHolder[i][j];
}
}
此外,在主要程序中也有错误:
#include <iostream>
using namespace std;
void GaussSeidel(int Iterations, float **EquationHolder,
float *EquationResultHolder) {
int InitialGuess = 0;
float x1 = 0, x2 = 0, x3 = 0;
while (InitialGuess < Iterations) {
x1 = (1 / EquationHolder[0][0])* (EquationResultHolder[0]-
((EquationHolder[0][1]) * x2)-((EquationHolder[0][2]) * x3));
x2 = (1 / EquationHolder[1][1])*(EquationResultHolder[1]-
((EquationHolder[1][0]) * x1)-((EquationHolder[1][2]) * x3));
x3 = (1 / EquationHolder[2][2])*((EquationResultHolder[2]-
((EquationHolder[2][0]) * x1)-((EquationHolder[2][1]) * x2)));
InitialGuess += 1;
cout << "---------------------Iteration #" << InitialGuess
<< "---------------------" << std::endl;
cout << "X1: " << x1 << "t" << x2 << "t" << x3 << std::endl;
}
}
用法:
int main() {
int NumEquations = 3;
// Equation Holder...
float ** EquationHolder = new float *[3];
for (int i = 0; i < NumEquations; i++) {
EquationHolder[ i] = new float[3];
cout << "Please Enter The Information Of Equation(" << i + 1 << ")...n";
for (int j = 0; j < NumEquations; j++) {
cout << "X" << j + 1 << ": ";
cin >> EquationHolder[i][j];
}
}
//... as before
for ( int i = 0; i < NumEquations; i++) { // deallocate storage
delete [] EquationHolder[ i];
}
delete [] EquationHolder;
return 0;
}
输出:
(…)
请输入所需迭代次数:9
---------------------迭代#1--------------------
X1:4-2 1.42857
---------------------迭代#2--------------------
X1:4.14286-2.83333 2.10204
(…)
---------------------迭代#9--------------------
X1:3.81631-3.03054 2.36438
您没有初始化EquationHolder的行。添加此:
float ** EquationHolder = new float *[3];
for(int i=0; i<NumEquations; i++) // ADD
EquationHolder[i] = new float[3]; // ADD
此外,我建议您使用double,而不是float(double更精确,更不容易出现数字错误)。