gpt4 book ai didi

c - For Loop 多次迭代失败

转载 作者:太空宇宙 更新时间:2023-11-04 05:58:22 24 4
gpt4 key购买 nike

我正在借助 Numerical Recipes 例程求解 ODE。为了及时传播我的解决方案,我使用了一个 for 循环,但是这个循环不会随着时间的推移推进解决方案,并且不会显示任何错误。这种行为的原因可能是什么,我在下面粘贴我的代码:

//Compilation and running
//gcc Q3.c RK4.c nrutil.c -lm -o Q3 && ./Q3

#include <stdio.h>
#define NRANSI
#include "nr.h"
#include "nrutil.h"

#define N 2
#define H 10
#define h 0.01
void derivs(float t,float y[],float dydt[])
{
dydt[1]=y[2];
dydt[2] = -32 -y[1];
}

int main(void)
{
int k,j;
// The time is set to be zero when the mass is released
float t=0.0,*y,*dydt,*yout;

y=vector(1,N);
dydt=vector(1,N);
yout=vector(1,N);
y[1]= H;
y[2]= 0;

derivs(t,y,dydt);
printf("\n%16s %5s %12s %12s %12s\n",
"Function:","y","dydt","d2ydt2","t");
for(k = 0; k < 5; k++);
{
rk4(y,dydt,N,t,h,yout,derivs);
printf("\nfor a step size of: %6.2f\n",h);
printf("%12s","rk4:");
for (j=1;j<=N;j++) printf(" %12.6f",yout[j]);
t += h;
printf("%12.6f %12.6f \n", -32 - yout[2], t);
y[1] = yout[1];
y[2] = yout[2];
}
free_vector(yout,1,N);
free_vector(dydt,1,N);
free_vector(y,1,N);
return 0;
}
#undef NRANSI

问题出在以“k”为循环变量的for循环中,由于未知原因,循环没有迭代超过一次。如果能就此主题提供任何帮助,我将不胜感激。

最佳答案

从 for 语句中删除分号。

for(k = 0; k < 5; k++);  // loops an empty statement
{
//doesn't loop
}

关于c - For Loop 多次迭代失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23028527/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com