C 我需要我的方程覆盖一个循环中每10次迭代的文件中的数据 double-rho[1001],rhonew[1001]; 内部主(空) { int-tstep,tmax,n,nmax,r; 双t,dt,x,dx; dt=0.001; tmax=1000; dx=0.1; nmax=1000; rho0=1.0; r=1; 文件*最终; afinal=fopen(“afinal.txt”,“w”); 文件*中间; amid=fopen(“amid.txt”,“w”); 对于(n=0;n
你的意思是这样吗?:C 我需要我的方程覆盖一个循环中每10次迭代的文件中的数据 double-rho[1001],rhonew[1001]; 内部主(空) { int-tstep,tmax,n,nmax,r; 双t,dt,x,dx; dt=0.001; tmax=1000; dx=0.1; nmax=1000; rho0=1.0; r=1; 文件*最终; afinal=fopen(“afinal.txt”,“w”); 文件*中间; amid=fopen(“amid.txt”,“w”); 对于(n=0;n,c,loops,for-loop,C,Loops,For Loop,你的意思是这样吗?: double rho[1001], rhonew[1001]; int main(void) { int tstep, tmax, n, nmax, r; double t, dt, x, dx; dt = 0.001; tmax = 1000; dx = 0.1; nmax = 1000; rho0=1.0; r=1; FILE *afinal; afinal = fopen("afina
double rho[1001], rhonew[1001];
int main(void)
{
int tstep, tmax, n, nmax, r;
double t, dt, x, dx;
dt = 0.001;
tmax = 1000;
dx = 0.1;
nmax = 1000;
rho0=1.0;
r=1;
FILE *afinal;
afinal = fopen("afinal.txt","w");
FILE *amid;
amid = fopen("amid.txt","w");
for (n = 0; n <= nmax; n++)
{
rho[n] = 500;
}
for (n = 0; n <= nmax; n++)
{
rhonew[n] = 1;
}
for (tstep=1; tstep<=tmax; tstep++)
{
rho[tstep] += -tstep;
if(tstep == r*10)
//I want this if statement to execute every 10 "tsteps" to overwrite the data in amid.txt
{
for (n = 0; n <= nmax; n++)
{
x = n*dx;
fprintf(amid, "%f \t %f \n", x, rho[n]);
}
fclose(amid);
r++;
}
}
for (n = 0; n <= nmax; n++)
{
x = n*dx;
fprintf(afinal, "%f \t %f \n", x, rho[n]);
}
fclose(afinal);
return 0;
}
for(tstep=1;tstep尝试:
这是mod运算符。它将tstep除以10并返回余数。如果余数为零,则执行for循环
此外,如果在十个步骤中需要一个“阶段”,那么t步骤%10==2
或1,3,直到9,那么您仍然会每十个步骤执行一次循环,并且仅与外部循环有一个偏移量。模(%)是一个。与一个固定值进行比较,然后每次向该值添加10更有效。我怀疑你是否能将差异基准化到任何显著程度。真正需要考虑的是,其他人阅读和维护你的代码有多困难。程序员的时间比cpu的毫秒要宝贵得多。调用ofprintf()
必须是100倍,如果不是1000倍的话,那么CPU使用率要比%
操作员高出许多倍。绝对同意:使用%
。我同意你们两个。
for (tstep=1; tstep<=tmax; tstep++)
{
rho[tstep] += -tstep;
if(tstep == r)
{
rewind(amid);
for (n = 0; n <= nmax; n++)
{
x = n*dx;
fprintf(amid, "%f \t %f \n", x, rho[n]);
}
r += 10;
}
}
// later....
close(amid);
if (tstep%10 == 0)
//I want this if statement to execute every 10 "tsteps" to
// overwrite the data in amid.txt