0

Hello CS50 community,

my question is about the reflection filter (less comfortable):

  1. I have compiled a code that is reflecting (YEAH!) but there seems to be the following errors when checking:

*:) reflect correctly filters 1x2 image

:( reflect correctly filters 1x3 image expected "0 0 255\n0 255...", not "255 0 0\n0 255..."

:) reflect correctly filters image that is its own mirror image*

:( reflect correctly filters 3x3 image expected "70 80 90\n40 5...", not "10 20 30\n40 5..."

:) reflect correctly filters 4x4 image*

  1. Why does it work even if i do not use pointers?

Thank you for pointing me in the right direction, and happy new year!

    void reflect(int height, int width, RGBTRIPLE image[height][width])
    {
        if (width%2 == 0)     
    {
    for (int i=0 ; i < height; i++)
        {
        for (int j=0 ; j < width/2; j++)
        {
        
            int blue = image[i][j].rgbtBlue;
            int green = image[i][j].rgbtGreen;
            int red = image[i][j].rgbtRed;
        
            int Bluereflect = image[i][width - 1 - j].rgbtBlue;
            int Greenreflect = image[i][width - 1 - j].rgbtGreen;
            int Redreflect = image[i][width - 1 - j].rgbtRed;
        
            int tmpB = image[i][j].rgbtBlue;
            image[i][j].rgbtBlue = image[i][width - 1 - j].rgbtBlue;
            image[i][width - 1 - j].rgbtBlue = tmpB;

            int tmpG = image[i][j].rgbtGreen;
            image[i][j].rgbtGreen = image[i][width - 1 - j].rgbtGreen;
            image[i][width - 1 - j].rgbtGreen = tmpG;
            
            int tmpR = image[i][j].rgbtRed;
            image[i][j].rgbtRed = image[i][width - 1 - j].rgbtRed;
            image[i][width - 1 - j].rgbtRed = tmpR;
        
        }
    }
}
return;

if (width%2!=0)     
    {
    for (int i=0 ; i < height; i++)
        {
        for (int j=0 ; j < round((width-1)/2); j++)
            {
                        
            int blue = image[i][j].rgbtBlue;
            int green = image[i][j].rgbtGreen;
            int red = image[i][j].rgbtRed;
        
            int Bluereflect = image[i][width - 1 - j].rgbtBlue;
            int Greenreflect = image[i][width - 1 - j].rgbtGreen;
            int Redreflect = image[i][width - 1 - j].rgbtRed;
        
            int tmp_B = image[i][j].rgbtBlue;
            image[i][j].rgbtBlue = image[i][width - 1 - j].rgbtBlue;
            image[i][width - 1 - j].rgbtBlue = tmp_B;

            int tmp_G = image[i][j].rgbtGreen;
            image[i][j].rgbtGreen = image[i][width - 1 - j].rgbtGreen;
            image[i][width - 1 - j].rgbtGreen = tmp_G;
            
            int tmp_R = image[i][j].rgbtRed;
            image[i][j].rgbtRed = image[i][width - 1 - j].rgbtRed;
            image[i][width - 1 - j].rgbtRed = tmp_R;

            }
        }   
    }
return;

}

0

The problem is that the second block of code that processes odd width is dead code. It will never run.

The cause is the placement of the first return statement. It is outside of the first if statement code block, so it will ALWAYS be executed. The first return statement should be placed before the closing curly brace just above it. This will put the return inside the code block for the if statement so that it will only be executed if the code is processing an even width.

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

Happy New Year!

1
  • Oh man, thanks Cliff, you're the best! Happy new year!
    – SirenLu
    Jan 1 at 17:07

You must log in to answer this question.

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