0

I am working on reflecting this image. I think I got the logic correctly, but I am not positive about that. However, my image did not reflect. Is this because I need to use malloc? I tried to but couldn't figure out what to set malloc to... what type (it's not being an int, char, etc).. Not sure what else to try...

// Reflect image horizontally
    void reflect(int height, int width, RGBTRIPLE image[height][width])
    {
        RGBTRIPLE reflection[height][width];
        for (int i = 0; i < height; i ++)
        {
            for (int j = 0; j < width; j++)
            {
                //need swap each pixel from left to right...
              reflection[i][j] = image[i][width-j];
    
            }
        }
        return;
    }

I figured out how to use malloc but it's still not working. Seems to run but then the result is the same as the oringal image... any suggestions?

Thanks!

// Reflect image horizontally
void reflect(int height, int width, RGBTRIPLE image[height][width])
{
    RGBTRIPLE reflection[height][width];
    struct reflection *p = malloc((height * width) + 1);
    for (int i = 0; i < height; i ++)
    {
        for (int j = 0; j < width; j++)
        {
            //need swap each pixel from left to right...
          reflection[i][j] = image[i][width-j];

        }
    }
    return;
}

I changed it up a bit... now it's still not working:

// Reflect image horizontally
void reflect(int height, int width, RGBTRIPLE image[height][width])
{
    RGBTRIPLE temp[height][width];
    RGBTRIPLE reflection[height][width];
    struct reflection *p = malloc((height * width) + 1);
    for (int i = 0; i < height; i ++)
    {
        for (int j = 0; j < width; j++)
        {
            //need swap each pixel from left to right...
          temp[i][j] = image[i][j];
          image[i][j] = reflection[i][width - j];
          reflection[i][width - j] = temp[i][j];

        }
    }
    return;
    free(reflection);
}

Now it works! But the check50 function is still marking it red... not sure why.

// Reflect image horizontally
void reflect(int height, int width, RGBTRIPLE image[height][width])
{
    void swap(RGBTRIPLE *a, RGBTRIPLE *b);
    RGBTRIPLE temp[height][width];
    for (int i = 0; i < height; i ++)
    {
        for (int j = 0; j < (width/2); j++)
        {
            //need swap each pixel from left to right...
          swap(&image[i][j], &image[i][width-j]);
        }
    }
   return;
}
    void swap(RGBTRIPLE *a, RGBTRIPLE *b)
    {
        RGBTRIPLE temp;
        temp = *a;
        *a = *b;
        *b = temp;
    }
1

Notice what your for loops are doing. i = 0 will not switch with i = height-1 here. What you are doing is replacing i = height - 1 with i = 0. In this process you are overwriting the last pixel (in this example) and losing it forever.

Recall that example from David sir's lecture where he called a student on stage to swap liquid in 2 glasses without mixing them. (If i am not wrong, it is in this lecture itself).

Then take a look at your code and make necessary changes. Have a great day!

7
  • I updated it as follows but it's still not working... – user29490 Aug 19 '20 at 23:50
  • Thank you! I updated my question..I fixed it based on what you said but it's still not working. Please advise. – user29490 Aug 19 '20 at 23:53
  • I dont understand the need for struct reflection. What i did was make a function to swap the two values. You have to swap image[i][j] with image[i][width-1] not with reflection[i][width - j].. Also when you swap 1st and last value there will come a point that all the values are swapped but we have not reached end of the row. Take that into consideration as well. – Himanshi Aug 20 '20 at 11:12
  • Thanks! I have updated it on top... but I am still getting it wrong in the cs50 check - even though the outcome is working!! – user29490 Aug 21 '20 at 3:47
  • here -> swap(&image[i][j], &image[i][width-j]); if width = 4, do you want to exchange with x = 4 or x = 3? (let x be any variable). Think about the logic. You are almost there. And please tick the answer to mark this question as solved if it helped you. – Himanshi Aug 21 '20 at 11:01

You must log in to answer this question.

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