I run my code and it complies and provides a (kind of) reflected image. It breaks the image in half and mirrors it instead of just reflecting. My goal was to switch the place of each pixel in the row, but my logic must be off. I've looked around and noticed some people recommend dividing the width in half, but I can't understand why that would help.


void reflect(int height, int width, RGBTRIPLE image[height][width])
    // literally just reflect bro
    for (int i = 0; i < height; i++)
        for (int j = 0; j < width; j++)
            image [i][j] = image [i][(width -1) - j];



Let's see the very first switch being the first pixel on the left and the last pixel on the right. In your code: image [i][j] = image [i][(width -1) - j]; you copied the last pixel in the first position. Now you have the same pixel in the first and last position and no access to the one that was first. So How can you switch the two pixels without losing any of them? Maybe you need to store one of them for a while? Now we keep going through the image from the outside to the inside switching pixels left to rigth and right to left. What happens when you reach to the middle point? What happens if you keep going? You can try the diferent otputs modifiyng the width in your 'j' loop.

  • I understand it now (I think). Theoretically it should work, but after I copy the last pixel into the first section of the array, I overwrite what is in the first array and I have no ability to access it again.
    – michael
    Apr 16 '20 at 15:51
  • That's it! So before you overwrite it, you could store a copy in a variable. Should be something like: 1- copy first pixel into a temp variable 2- copy last pixel into the first position: (that would be your line: 'image [i][j] = image [i][(width -1) - j]'. 3- copy the temp variable into the last position.
    – Tritum
    Apr 16 '20 at 16:31

I have this exact question. So I think it has to do with what the lecture was discussing, ie memory in functions. The only thing I don't understand is I thought it was writing to a separate file so that shouldn't matter. Ugh.

You must log in to answer this question.

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