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++; }}
Its supposed to subtract star[]
from noise[]
until it hits -1
, star[] is shorter so it has to restart from its beginning until noise[]
hits -1
, then it will stop.
它应该从noise []中减去star []直到它达到-1,star []更短,所以它必须从它的开始重新开始直到noise []命中-1,然后它将停止。
The input looks like this, but i'm using experimental values right now, you can see them below.
输入看起来像这样,但我现在正在使用实验值,你可以在下面看到它们。
(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
So right now it stops after it hits the first -1 here {10,0,20,-1}
but it should just restart and keep substracting until the other sequence hits -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}
2 个解决方案
#1
3
There are two problems in your code.
您的代码中存在两个问题。
- Your indexing is starting from
1
instead of0
. -
The "star" one is shorter so it has to restart from its beginning until "noise" hits '-1', then it will stop.
“星”一个较短,因此它必须从头开始重新启动,直到“噪音”达到'-1',然后它将停止。
This you are not handling correctly.
这你没有正确处理。
您的索引从1而不是0开始。
If I understood your problem correctly. The below code should do what you have expected.
如果我理解你的问题。以下代码应该达到您的预期。
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++; } }
This is how I call from main
这是我从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]);}
Output:20 10 20 30 20 10 20 30 -1
产量:20 10 20 30 20 10 20 30 -1
#2
-3
Try this code. I feel you are accessing from the second element rather than the first. Though it doesn't have a part to play in not stopping the loop, it is a glitch nevertheless. So, increment i
at the end of the loop. And to prevebt infifite looping because of the continue, insert an i++
inside that loop as well.
试试这个代码。我觉得你是从第二个元素而不是第一个元素访问的。虽然它没有一个部分可以不停止循环,但它仍然是一个小故障。所以,在循环结束时增加i。并且由于继续预设了infifite循环,所以在该循环中插入一个i ++。
void signalclear(int noise[64], int star[64], int clear[64]) { int i = 0, j = 0 ; while (i < 63) { if (star[j] == -1) { j = 0; continue; } if (noise[i] == -1) { break; } clear[i] = noise[i] - star[j]; j++; i++; }}
#1
3
There are two problems in your code.
您的代码中存在两个问题。
- Your indexing is starting from
1
instead of0
. -
The "star" one is shorter so it has to restart from its beginning until "noise" hits '-1', then it will stop.
“星”一个较短,因此它必须从头开始重新启动,直到“噪音”达到'-1',然后它将停止。
This you are not handling correctly.
这你没有正确处理。
您的索引从1而不是0开始。
If I understood your problem correctly. The below code should do what you have expected.
如果我理解你的问题。以下代码应该达到您的预期。
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++; } }
This is how I call from main
这是我从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]);}
Output:20 10 20 30 20 10 20 30 -1
产量:20 10 20 30 20 10 20 30 -1
#2
-3
Try this code. I feel you are accessing from the second element rather than the first. Though it doesn't have a part to play in not stopping the loop, it is a glitch nevertheless. So, increment i
at the end of the loop. And to prevebt infifite looping because of the continue, insert an i++
inside that loop as well.
试试这个代码。我觉得你是从第二个元素而不是第一个元素访问的。虽然它没有一个部分可以不停止循环,但它仍然是一个小故障。所以,在循环结束时增加i。并且由于继续预设了infifite循环,所以在该循环中插入一个i ++。
void signalclear(int noise[64], int star[64], int clear[64]) { int i = 0, j = 0 ; while (i < 63) { if (star[j] == -1) { j = 0; continue; } if (noise[i] == -1) { break; } clear[i] = noise[i] - star[j]; j++; i++; }}