Been stuck on reflect for a couple of hours now and can't seem to figure it out.

I've written the following code. I take the values, find what the opposite should be, put both sets into variables and then swap their pointers out. Not sure where I am going wrong here.

void reflect(int height, int width, RGBTRIPLE image[height][width])
    for(int i = 0; i < height ; i++)
        for(int j = 0; j < width; j++)
            // Takes original pixel information
            int B = image[i][j].rgbtBlue;
            int G = image[i][j].rgbtGreen;
            int R = image[i][j].rgbtRed;
            // Takes image I want to swap it with
            int rB = image[i][width - j].rgbtBlue;
            int rG = image[i][width - j].rgbtGreen;
            int rR = image[i][width - j].rgbtRed;
            swap(&B , &rB);
            swap(&G , &rG);
            swap(&R , &rR);

            //Write new values to pixels
            image[i][j].rgbtBlue = B;
            image[i][j].rgbtGreen = G;
            image[i][j].rgbtRed = R;
            image[i][width - j].rgbtBlue = rB;
            image[i][width - j].rgbtGreen = rG;
            image[i][width - j].rgbtRed = rR;

Here is my swap function.

void swap (int *a, int *b)
    int temp = *a;
    *a = *b;
    *b = temp;

Please help me.


Let me guess. It doesn't seem to be doing anything, right?

Without doing a deep dive analysis, one thing jumps out at me. The inner for loop runs from 0 to width. Think carefully about the practical effect of that.

From 0 to width/2, it's going to swap the left half with the right half. Then from width/2 to width, it's going to swap everything back!

If this answers your question, please click on the check mark to accept. Let's keep up on forum maintenance. ;-)

  • You got it! Can't believe I didn't see it. I also needed to subtract 1 from the image[i][width - j] lines of code to make sure I get the initial 0 value. – Multiplify Apr 17 at 18:05
  • Sometimes you have to remember the real effects of code, not just to get the code to execute. This is a perfect example of that. The code worked perfectly for what it was told to do. It was a simple oversight in the design logic that caused the failure. Happy coding! ;-) – Cliff B Apr 17 at 18:12

You must log in to answer this question.

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