0

Image seems to be reflected correctly but the program doesn't pass check50.

Code:

void swap(RGBTRIPLE *a, RGBTRIPLE *b);

void reflect(int height, int width, RGBTRIPLE image[height][width])
{
    for (int i = 0; i < height; i++)
    {
        for (int j = 0; j < (width / 2); j++)
        {
            swap(&image[i][j], &image[i][width - j]);
        }
    }
    return;
}

void swap(RGBTRIPLE *a, RGBTRIPLE *b)
{
    RGBTRIPLE tmp = *a;
    *a = *b;
    *b = tmp;
}

Check50:

**:( reflect correctly filters 1x2 image
    expected "0 0 255\n255 0...", not "0 0 0\n0 0 255..."
:( reflect correctly filters 1x3 image
    expected "0 0 255\n0 255...", not "0 0 0\n0 255 0..."
:( reflect correctly filters image that is its own mirror image
    expected "255 0 0\n255 0...", not "0 255 0\n255 0..."
:( reflect correctly filters 3x3 image
    expected "70 80 90\n40 5...", not "110 130 140\n4..."
:( reflect correctly filters 4x4 image
    expected "100 110 120\n7...", not "110 130 140\n1..."**

1 Answer 1

2

Let's look carefully at this code:

    for (int j = 0; j < (width / 2); j++)
    {
        swap(&image[i][j], &image[i][width - j]);
    }

Now, let's walk through what happens on the first pass. Note that j is 0. It's going to swap the first pixel, image[0][0] with image[0][width-j].

Two questions.

  1. what is the value of width-j?
  2. Is that value valid for the array?????

Happy coding! ;-)

You must log in to answer this question.

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