C++ 数组值不断将计算值添加到数组的上一个值
我的数组不断地将数组的前一个值添加到值中,有人能帮我解决这个问题吗?我只需要计算出的值,而不是添加到数组中的前一个值 下面是我的代码中计算和排序值的部分:C++ 数组值不断将计算值添加到数组的上一个值,c++,arrays,for-loop,C++,Arrays,For Loop,我的数组不断地将数组的前一个值添加到值中,有人能帮我解决这个问题吗?我只需要计算出的值,而不是添加到数组中的前一个值 下面是我的代码中计算和排序值的部分: for (int i = 0; i < ARRAYSIZE; i++) { angle = random_angle(angle, STEP_SIZE); // Compute the initial speed, using the selected angle.
for (int i = 0; i < ARRAYSIZE; i++)
{
angle = random_angle(angle, STEP_SIZE);
// Compute the initial speed, using the selected angle.
xspeed = INITSPEED * cos(2 * PI * angle / 360.0);
yspeed = INITSPEED * sin(2 * PI * angle / 360.0);
//Look up windspeed
if (beaufort == 6)
{
wind = random_speed(STRONG_BREEZE_LO, STRONG_BREEZE_HI, STEP_SIZE);
wave = random_wave(B6_WAVE_LO, B6_WAVE_HI, STEP_SIZE);
}
else if (beaufort == 5)
{
wind = random_speed(FRESH_BREEZE_LO, FRESH_BREEZE_HI, STEP_SIZE);
wave = random_wave(B5_WAVE_LO, B5_WAVE_HI, STEP_SIZE);
}
else if (beaufort == 4)
{
wind = random_speed(MODERATE_BREEZE_LO, MODERATE_BREEZE_HI, STEP_SIZE);
wave = random_wave(B4_WAVE_LO, B4_WAVE_HI, STEP_SIZE);
}
else if (beaufort == 3)
{
wind = random_speed(GENTLE_BREEZE_LO, GENTLE_BREEZE_HI, STEP_SIZE);
wave = random_wave(B3_WAVE_LO, B3_WAVE_HI, STEP_SIZE);
}
else if (beaufort == 2)
{
wind = random_speed(LIGHT_BREEZE_LO, LIGHT_BREEZE_HI, STEP_SIZE);
wave = random_wave(B2_WAVE_LO, B2_WAVE_HI, STEP_SIZE);
}
else if (beaufort == 1)
{
wind = random_speed(LIGHT_AIR_LO, LIGHT_AIR_HI, STEP_SIZE);
wave = random_wave(B1_WAVE_LO, B1_WAVE_HI, STEP_SIZE);
}
else
{
wind = random_speed(CALM_LO, CALM_HI, STEP_SIZE);
}
//Repeat the following, as long a the y-position is greater than equal to wave height or the speed in y-direction is positive
while(ycoord >= wave || yspeed >= 0)
{
//Compute the new positions
xcoord = xcoord + xspeed * DELTA_T;
ycoord = ycoord + yspeed * DELTA_T;
//Compute new speeds
xspeed = xspeed - DELTA_T * DRAG * (xspeed + wind);
yspeed = yspeed - DELTA_T * GRAVITY - DELTA_T * DRAG * yspeed;
}
array[i] = xcoord;
}
for(int i=0;i=wave | yspeed>=0)
{
//计算新的位置
xcoord=xcoord+xspeed*DELTA\T;
ycoord=ycoord+Y速度*增量T;
//计算新速度
xspeed=xspeed-DELTA_T*阻力*(xspeed+风);
yspeed=yspeed-DELTA_T*重力-DELTA_T*阻力*yspeed;
}
数组[i]=xcoord;
}
您正在将上一个值添加到此处的计算值
array[i] = xcoord;
然后u分配给数组
您可能缺少重置xcoord/ycoord等的步骤请尝试使用调试器…此处没有足够的信息来诊断问题。您还需要什么信息?您能否帮助我获得修复的步骤,请写入
xcoord=ycoord=0代码>在while
循环之前。事实上,最好是intxcoord=0;int ycoord=0
并删除循环外的任何声明。@user130824您可以按照Matt的建议进行操作。我同意第二种解决方案更好
array[i] = xcoord;