gpt4 book ai didi

C结构式

转载 作者:太空宇宙 更新时间:2023-11-04 06:41:29 25 4
gpt4 key购买 nike

此公式的输出有问题:

typedef struct
{
float t, Vx, Vy, Px, Py;
} datapoint;

datapoint *data;
data = malloc(steps * sizeof(datapoint));

data[0].Vx = (20*cos(30)); //30 is changed to radians
data[0].Vy = (20*sin(30));
data[0].Px = 0;
data[0].Py = 0;

steps=100;
i=1;

do
{
printf("Time: %.2f\t",i*q5);

// X
data[i].Vx = data[i-1].Vx ;//- CalculateDrag(data[i-1].Vx, q4);
data[i].Px = ((data[i-1].Px) + ((data[i].Vx) * i));

printf("X = %.2f\t",data[i].Px);

// Y
data[i].Vy= data[i-1].Vy - (9.81)*i; //- CalculateDrag(data[i-1].Vy,q4);
data[i].Py= data[i-1].Py + (data[i].Vy * i);

printf("Y = %.2f\t", data[i].Py);

printf("\n");
i++;
} while(((data[i].Py) >0) && (i<=steps));

输出应该是这样的:

Time 0.10s: X = 1.73m Y = 1.00m
Time 0.20s: X = 3.46m Y = 1.90m
Time 0.30s: X = 5.20m Y = 2.71m
....
....
Time 2.00s: X = 34.64m Y = 1.36m
Time 2.10s: X = 36.37m Y = 0.40m
Time 2.20s: X = 38.11m Y = -0.66m
Landed at X = 38.11 at time 2.20s

但它会打印出其他值。尽我所能,但认为公式中可能存在错误代码。

最佳答案

根据您发布的代码:

datapoint *data;
data[0].Vx = (20*cos(30));

您在此处使用未初始化的数据。它可能指向任何地方,这是未定义的行为,所以任何事情都可能发生。

通过阅读您的代码,我认为您想做的事情如下:

data = malloc(sizeof(datapoint) * steps);

关于C结构式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7633995/

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