我为我的大学项目编写了这段代码,但是当我尝试运行它时它停止工作。 任何人都可以帮我解决问题所在? 我猜它的指针,但我不知道它的错误 对于t和z,如果你想尝试使用20和5 当我进入它们时它停止工作而不是给我输出 它应该提供2 .txt文件,包括一些数字
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
int t;
int z;
double deltaz;
double deltat;
double beta;
double mv=0;
ofstream out1("Time-setllment.txt");
ofstream out2("isochrones.txt");
cout<<"Enter time step(>9&integer only):";
cin>>t;
cout<<endl<<"Enter height step(integer only):";
cin>>z;
cout<<endl;
double **u = new double * [z+1];
for (int i=0;i<z;i++)
u[i]=new double [t+1];
double *s = new double [t+1];
mv = (0.00000003*365*24*3600)/(2*10);
deltaz = 20/z;
deltat = 50/t;
beta = (2*deltat)/(deltaz*deltaz);
if (beta>0.5)
cout<<"Beta is more than 0.5 :|"<<endl;
else
{
for (int i=0;i<z+1;i++)
{
if (i==0 || i==z)
{
for (int j=0;j<t+1;j++)
u[i][j] = 0;
}
else
{
u[i][0]=70-3.5*(i*deltaz);
}
}
for (int j=1;j<t;j++)
{
for (int i=1;i<z;i++)
{
u[i][j] = u[i][j-1]+beta*(u[i-1][j-1]+u[i+1][j-1]-2*u[i][j-1]);
}
}
for (int j=1;j<t+1;j++)
{
double sigmau=0;
for (int i=1;i<z;i++)
{
sigmau +=u[i][j];
}
s[j]=mv*(35*20-deltaz*((u[0][j]+u[z][j])/2+sigmau));
}
out1<<"Time \t Settlment \n";
for (int j=1;j<t+1;j++)
{
out1<<j*deltat<<"\t"<<s[j]<<"\n";
}
out2<<"Depth \t";
for (int j=0;j<t+1;j++)
{
out2<<"t(year)="<<j*deltat<<"\t";
}
out2<<"\n";
for (int i=0;i<z+1;i++)
{
out2<<i*deltaz<<"\t";
for (int j=0;j<t+1;j++)
{
out2<<u[i][j]<<"\t";
}
}
for (int i=0;i<z+1;i++)
delete []u[i];
delete []u;
delete []s;
}
return 0;
}
答案 0 :(得分:0)
您在数据分配方面遇到错误:
double **u = new double * [z+1];
for (int i=0;i<z;i++)
您确实希望将其更改为:
double **u = new double * [z+1];
for (int i=0;i<z+1;i++)