0

The code seems to spit out a nice-looking reflected image, however, check50 says some of the values are not reflecting the way they should be. It's odd because it works for a 1x3 image and 3x3 image, but not for the 1x2 and 4x4 images.

This is what the code looks like:

void reflect(int height, int width, RGBTRIPLE image[height][width])
{
    RGBTRIPLE temporary[((width - 1) / 2) + 1]; // Making a temporary array

    for (int i = 0; i < height; i++)
    {
        for (int j = 0; j <= round((width - 1) / 2); j++) // Assign the first half of the pixels to the temp array
        {
            temporary[j] = image[i][j];
        }

        for (int j = 0; j < round((width - 1) / 2); j++) // Move the second half of the pixels to the first half
        {
            image[i][j] = image[i][width + (-j - 1)];
        }

        for (int j = 0; j < round((width - 1) / 2); j++) // Move the pixels from temp to the second half
        {
            image[i][width + (-j - 1)] = temporary[j];
        }

    }
    return;
}

To give an example of the error, check50 says:

"Testing with sample 1x2 image. First row (255,0,0), (0,0,255).

Expected output:
0 0 255
255 0 0

Actual output:
255 0 0
0 0 255"

and in the 4x4 situation, the rows alternate.

"Expected output:
100, 110, 120
70, 80, 90
40, 50, 60

10, 20, 30
(etc)

Actual output:
100, 110, 120
40, 50, 60
70, 80, 90

10, 20, 30
(etc)".

But again, the 1x3 and 3x3 images seem to be fine. What could be causing this?

3

So you have an issue with even width only. In the example of a width of 2,

round((width - 1) / 2)

would be

round((2 - 1) / 2)

or

round(1 / 2)

and since that's integer division (both operands are integer, integer division produces an integer again),

round(0)

Your <= would work correctly here, your < wouldn't. For odd width, the <= would represent a swap of the middle element with itself, I guess that's why you tried < instead.

Why not just j < width / 2 for all three of them? width / 2 again is an integer division, truncating the result. And that's exactly the number of pairs we need to swap each line.

Also, you need each of those array elements exactly once. You could use a single RGBTRIPLE variable for swapping just the current pair.

| improve this answer | |
  • Solved, thank you! – azb1297 Jan 18 at 17:44

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .