cs50反思代码失败。 值没有放在正确的位置。

huangapple go评论57阅读模式
英文:

cs50 reflect code is failing. values are not in there right place

问题

我找不到解决这两个错误的方法,尽管当我自己测试时,图片反映得很正确。
这是错误信息:

使用1x3图片进行测试
第一行:(255, 0, 0), (0, 255, 0), (0, 0, 255)
运行 ./testing 2 1...
检查输出是否为 "0 0 255\n0 255 0\n255 0 0\n"...

期望输出:

0 0 255
0 255 0
255 0 0

实际输出:

0 0 0
0 0 255
0 255 0

我注意到第一行中有来自第二行的一个值,第二行中有来自第三行的一个值。
这是代码:

void reflect(int height, int width, RGBTRIPLE image[height][width])
{
    //这是反映后的图像
    RGBTRIPLE reflected[height][width];
    for (int h = 0; h < height; h++)
    {
        for (int w = 0; w < width; w++)
        {
            reflected[h][w] = image[h][width - w];
        }
    }
    //从反映的图像复制到原图像
    for (int h = 0; h < height; h++)
    {
        for (int w = 0; w < width; w++)
        {
           image[h][w] = reflected[h][w];
        }
    }
    return;
}

问题已解决,但现在我在3x3图像中遇到了另一个问题,每列的第一行都是零

期望输出:

255 0 0
255 0 0
255 0 0
0 255 0
0 255 0
0 255 0
0 0 255
0 0 255
0 0 255

实际输出:

0 0 0
255 0 0
255 0 0
0 0 0
0 255 0
0 255 0
0 0 0
0 0 255
0 0 255

尝试以不同方式编写代码,但最终仍然遇到了相同的错误。

英文:

I cant find a way to fix these two errors despite that the pic reflects correctly when i test it by myself.
this is the error

testing with sample 1x3 image
first row: (255, 0, 0), (0, 255, 0), (0, 0, 255)
running ./testing 2 1...
checking for output &quot;0 0 255\n0 255 0\n255 0 0\n&quot;...

Expected Output:

0 0 255
0 255 0
255 0 0

Actual Output:

0 0 0
0 0 255
0 255 0

I noticed that there is a value from the second row in the first row and a value from the third row in the second row.
this is the code:

void reflect(int height, int width, RGBTRIPLE image[height][width])
{
    //this is where the reflected image gonna be
    RGBTRIPLE reflected[height][width];
    for (int h = 0 ; h &lt; height ; h++)
    {
        for (int w = 0 ; w &lt; width ; w++)
        {
            reflected[h][w] = image[h][width - w];
        }
    }
    //copying from the reflected to the image
    for (int h = 0 ; h &lt; height ; h++)
    {
        for (int w = 0 ; w &lt; width ; w++)
        {
           image[h][w] = reflected[h][w];
        }
    }
    return;
}

the problem is solved but now I have another problem in 3x3 image where the first row of each column is zeros

Expected Output:

255 0 0
255 0 0
255 0 0
0 255 0
0 255 0
0 255 0
0 0 255
0 0 255
0 0 255

Actual Output:

0 0 0
255 0 0
255 0 0
0 0 0
0 255 0
0 255 0
0 0 0
0 0 255
0 0 255

tried writing the code in different ways but I ended up with the same bug.

答案1

得分: 1

你必须始终检查你的范围:image[r][c] 假定 0 <= r < height0 <= c < width

现在考虑你的 image[h][width-w]width-w 的范围是多少?当 w = 0 => width-w = width。超出范围(并且是未定义的行为)。

你只需使用 image[h][width-1 - w] 来修复它。

英文:

You must always check your ranges: image[r][c] assumes that 0 &lt;= r &lt; height and that 0 &lt;= c &lt; width.

Now consider your image[h][width-w]. What is the range of width-w? When w = 0 =&gt; width-w = width. Out of range (and UB).

You just fix it with image[h][width-1 - w].

huangapple
  • 本文由 发表于 2023年4月4日 09:18:06
  • 转载请务必保留本文链接:https://go.coder-hub.com/75924807.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定