PSET4 CS50反射分配;代码未通过测试 [英] PSET4 CS50 Reflect Assignment; Code is failing the tests

查看:113
本文介绍了PSET4 CS50反射分配;代码未通过测试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尽管我的照片还不错,但我找不到解决这两个错误的方法。

I can't find a way to fix these two errors despite my pic turns out fine. Would appreciate it if anyone can enlighten me.

这是我得到的报告:


:(正确反映滤镜1x2图像

预期为 0 0 255\n255 0 ...,而不是 255 0 0\n0 0 2 ...


:)正确反映滤镜1x3图像

:)正确反映滤镜本身就是镜像的图像

: )正确反映滤镜3x3图像

:((正确反映滤镜4x4图像

预期为 100110 120\n7 ...,而不是 100110 120\n4 ...

:( reflect correctly filters 1x2 image
expected "0 0 255\n255 0...", not "255 0 0\n0 0 2..."

:) reflect correctly filters 1x3 image
:) reflect correctly filters image that is its own mirror image
:) reflect correctly filters 3x3 image
:( reflect correctly filters 4x4 image
expected "100 110 120\n7...", not "100 110 120\n4..."

这是我的代码:

RGBTRIPLE tmp[width];
for (int i = 0; i < height; i++)
{
    for (int j = 0; j <= width / 2; j++)
    {
        tmp[j] = image[i][j];
        image[i][j] = image[i][width - 1 - j];
        image[i][width - 1 - j] = tmp[j];
    }
}
return;


推荐答案

解决方案;

for循环中的条件需要稍作调整;

Condition in the for loop needs a small tweak;

j < width / 2

了解原因;

对于1x2图像,一行中有2个像素。宽度为2。为此只需进行1次迭代即可。在旧方案中,当j = 0时执行第一次迭代。但是随后还会执行第二次迭代,因为j = 1仍在条件内(等于width / 2)。不需要第二次迭代。您只是反映了两次,导致图像没有变化。

For 1x2 image, you have 2 pixels in one row. Width is 2. Just 1 iteration is enough for this. In the old scenario, 1st iteration is executed when j=0. But then 2nd iteration is also executed because j=1 is still within the condition (equals to width/2). 2nd iteration is unnecessary. You are just reflecting twice which results in no change in the image.

对于4x4图像,类似的问题。当j = 0且j = 1时,您应使用2次迭代来完成反射。但是,当j = 2(j仍等于且小于width / 2)时,将执行第3次迭代并将第3个像素替换为第一个像素错误。因为您已经更换过一次。

For 4x4 image, the similar issue. You should finish reflection with 2 iterations when j = 0, and when j = 1. However, when j = 2 (j is still equal and smaller than width/2), 3rd iteration is executed and replaces 3rd pixel with 1st pixel which is wrong. Because you had already replaced them once.

这篇关于PSET4 CS50反射分配;代码未通过测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆