0

This code does indeed reflect the image, but, there is a slight line in the middle. And CS50 fails it completely

// Reflect image horizontally
void reflect(int height, int width, RGBTRIPLE image[height][width])
{
    int temp1, temp2, temp3, temp_1, temp_2, temp_3 = 0;

    if((width % 2) == 0)// EVEN NUMBER OF PIXELS. PERFECT HALF
    {
        for (int i = 0; i < height; i++)
        {
            for (int j = 0; j < width / 2; j++)
            {
                temp_1 = image[i][j].rgbtBlue;
                image[i][j].rgbtBlue = image[i][width - 1 - j].rgbtBlue;
                image[i][width - 1 - j].rgbtBlue = temp_1;

                temp_2 = image[i][j].rgbtGreen;
                image[i][j].rgbtGreen = image[i][width - 1 - j].rgbtGreen;
                image[i][width - 1 - j].rgbtGreen = temp_2;

                temp_3 = image[i][j].rgbtRed;
                image[i][j].rgbtRed = image[i][width - 1 - j].rgbtRed;
                image[i][width - 1 - j].rgbtRed = temp_1;
            }
        }
        return;
    }
        if ((width % 2) != 0)// OOD NUMBER OF PIXEL. ONE CENTRAL PIXEL
        {
            for (int i = 0; i < height; i++)
            {
                int j, k = 0;

                for (j = 0; j < round((width - 1)/ 2); j++)     //(width - 1)/2
                {
                    temp_1 = image[i][j].rgbtBlue;
                    image[i][j].rgbtBlue = image[i][width - 1 - j].rgbtBlue;
                    image[i][width - 1 - j].rgbtBlue = temp_1;

                    temp_2 = image[i][j].rgbtGreen;
                    image[i][j].rgbtGreen = image[i][width - 1 - j].rgbtGreen;
                    image[i][width - 1 - j].rgbtGreen = temp_2;

                    temp_3 = image[i][j].rgbtRed;
                    image[i][j].rgbtRed = image[i][width - 1 - j].rgbtRed;
                    image[i][width - 1 - j].rgbtRed = temp_1;
                }
                //if (k == (width -1) / 2)
                k = (width -1) / 2;
                //{
                    image[i][j].rgbtBlue = image[i][k].rgbtBlue;

                    image[i][j].rgbtGreen = image[i][k].rgbtGreen;

                    image[i][j].rgbtRed = image[i][k].rgbtRed;
                //}
            }
        }
    return;
}
1

Ah, it's always those simple cut and paste errors that get us. ;-)

If you look at the extended results in the link posted with your check50 results, you can see the full raw data of the errors. It's always the last pixel on a line and always the red value, so let's look at that code:

            temp_3 = image[i][j].rgbtRed;
            image[i][j].rgbtRed = image[i][width - 1 - j].rgbtRed;
            image[i][width - 1 - j].rgbtRed = temp_1;

Now, look very carefully at the temp variables in this code. See anything wrong????

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

1
  • Thank you very much. such a tiny mistake that goes unnoticed, and spoils all the work. MUITO OBRIGADO TEAM. THS IS CS50! – Tiago Marques Jul 24 '20 at 7:37

You must log in to answer this question.

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