1

I am working through CS50's PSET #4, specifically filter -> reflect. I can get the image to present correctly (i.e., it is mirrored), but it is not exact enough to pass the CS50 Check.

My code is as follows:

void reflect(int height, int width, RGBTRIPLE image[height][width])
{
    for (int i = 0; i < height; i++) // Loop over every line
    {
        for (int j = 0; j < width / 2.00; j++) // Loop over every pixel in line
        {
            RGBTRIPLE tmp = image[i][j]; // tmp = pix[0]
            image[i][j] = image[i][width - j]; // make pix[0] = pix[n]
            image[i][width - j] = tmp; // make pix[n] = tmp
        }
    }
}

My results are on this page (link). It looks like my array is swapping beyond the array, and seems to be misaligned by one. For example, the below shows the blocks of code that are misaligned (by one row, it appears).

enter image description here

Any help is appreciated!

1

You are right, it is misaligned by one. Which index does the last pixel in the row have? And which index do you use for j of 0? Off-by-one.

BTW, width / 2 would work as well (with 2.0, you would have an extra swap of the middle pixel with itself).

3
  • Got it! Thank you! I see now that I was extending my j past the array by one, and fixed it to [width - 1 - j]. – tmichal2 Mar 24 '20 at 12:07
  • Hey, Im haveing the same problem can you elaborate on this – Yash Wath May 3 '20 at 7:20
  • @YashWath The valid index on the x-axis ranges from 0 to width-1, so those, the left-most and the right-most, would be a pair of pixels to swap. – Blauelf May 3 '20 at 18:58

You must log in to answer this question.

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