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

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

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

问题

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

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

期望输出:

  1. 0 0 255
  2. 0 255 0
  3. 255 0 0

实际输出:

  1. 0 0 0
  2. 0 0 255
  3. 0 255 0

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

  1. void reflect(int height, int width, RGBTRIPLE image[height][width])
  2. {
  3. //这是反映后的图像
  4. RGBTRIPLE reflected[height][width];
  5. for (int h = 0; h < height; h++)
  6. {
  7. for (int w = 0; w < width; w++)
  8. {
  9. reflected[h][w] = image[h][width - w];
  10. }
  11. }
  12. //从反映的图像复制到原图像
  13. for (int h = 0; h < height; h++)
  14. {
  15. for (int w = 0; w < width; w++)
  16. {
  17. image[h][w] = reflected[h][w];
  18. }
  19. }
  20. return;
  21. }

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

期望输出:

  1. 255 0 0
  2. 255 0 0
  3. 255 0 0
  4. 0 255 0
  5. 0 255 0
  6. 0 255 0
  7. 0 0 255
  8. 0 0 255
  9. 0 0 255

实际输出:

  1. 0 0 0
  2. 255 0 0
  3. 255 0 0
  4. 0 0 0
  5. 0 255 0
  6. 0 255 0
  7. 0 0 0
  8. 0 0 255
  9. 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

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

Expected Output:

  1. 0 0 255
  2. 0 255 0
  3. 255 0 0

Actual Output:

  1. 0 0 0
  2. 0 0 255
  3. 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:

  1. void reflect(int height, int width, RGBTRIPLE image[height][width])
  2. {
  3. //this is where the reflected image gonna be
  4. RGBTRIPLE reflected[height][width];
  5. for (int h = 0 ; h &lt; height ; h++)
  6. {
  7. for (int w = 0 ; w &lt; width ; w++)
  8. {
  9. reflected[h][w] = image[h][width - w];
  10. }
  11. }
  12. //copying from the reflected to the image
  13. for (int h = 0 ; h &lt; height ; h++)
  14. {
  15. for (int w = 0 ; w &lt; width ; w++)
  16. {
  17. image[h][w] = reflected[h][w];
  18. }
  19. }
  20. return;
  21. }

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

Expected Output:

  1. 255 0 0
  2. 255 0 0
  3. 255 0 0
  4. 0 255 0
  5. 0 255 0
  6. 0 255 0
  7. 0 0 255
  8. 0 0 255
  9. 0 0 255

Actual Output:

  1. 0 0 0
  2. 255 0 0
  3. 255 0 0
  4. 0 0 0
  5. 0 255 0
  6. 0 255 0
  7. 0 0 0
  8. 0 0 255
  9. 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:

确定