gpt4 book ai didi

无法弄清楚我的代码有什么问题:\(Array subtraction in C)

转载 作者:行者123 更新时间:2023-11-30 20:50:05 28 4
gpt4 key购买 nike

void signalclear(int noise[], int star[], int clear[]) {
int i = 0;
int j = 0;
while (clear[i] != -1) {
if (star[j] == -1) {
j = 0;
}
if (noise[i] == -1) {
clear[i] = -1;
break;
}
clear[i] = noise[i] - star[j];
i++;
j++;
}
}

它应该从 noise[] 中减去 star[] 直到达到 -1,star[] 更短,所以它必须从头开始重新启动,直到 noise[] 达到 -1,然后它将停止。

输入看起来像这样,但我现在使用的是实验值,您可以在下面看到它们。

(noise[])
382 450 402 490 592 652 712 832 422 370 362 450 512 512 512 532 683 694 700
712 789 509 480 540 512 469 450 412 402 422 462 522 -1

(star[])
120 120 140 160 200 260 320 440 160 40 100 120 120 -1

所以现在它在达到第一个 -1 后停止 {10,0,20,-1} 但它应该重新启动并继续减去,直到另一个序列达到 -1。

ERROR: signalclear({30,10,40,40,20,30,30,30,-1},{10,0,20,-1},{...}) // this sequence of numbers is only for testing.
Expected Result: {...} = {20,10,20,30,20,10,20,30,-1}
My Result: {...} = {20,10,20,30,-1}

最佳答案

您的代码中有两个问题。

  1. 您的索引从 1 开始,而不是从 0 开始。
  2. The "star" one is shorter so it has to restart from its beginning until "noise" hits '-1', then it will stop.

    你没有正确处理这个问题。

如果我正确理解你的问题。下面的代码应该符合您的预期。

void signalclear(int noise[64], int star[64], int clear[64]) {

int i = 0;
int j = 0;
while (i < 64) {

if (star[j] == -1) {
j = 0; //Reset the j to start from the beginning.
}

if (noise[i] == -1) {
clear[i] = -1;
break;
}

clear[i] = noise[i] - star[j];
i++;
j++;
}
}

这就是我从main调用的方式

void main()
{
int noise[64] = {30,10,40,40,20,30,30,30,-1};
int star[64] = {10,0,20,-1};
int clear [64];

signalclear(noise,star,clear);

int i=0;
for (i=0; i<64 && clear[i-1] != -1; i++)
printf("%d ", clear[i]);
}

输出:20 10 20 30 20 10 20 30 -1

关于无法弄清楚我的代码有什么问题:\(Array subtraction in C),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52645390/

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