check50 saying CS50 filter-more edges is not correct?

35 Views Asked by At
// Detect edges
void edges(int height, int width, RGBTRIPLE image[height][width])
{
    // Copying
    RGBTRIPLE copy[height][width];
    for (int i = 0; i < height; i++)
    {
        for (int j = 0; j < width; j++)
        {
            copy[i][j] = image[i][j];
        }
    }

    int Gx[3][3] = {{-1, 0, 1}, {-2, 0, 2}, {-1, 0, 1}};
    int Gy[3][3] = {{-1, -2, -1}, {0, 0, 0}, {1, 2, 1}};

    for (int i = 0; i < height; i++)
    {
        for (int j = 0; j < width; j++)
        {
            int GxsumRed = 0;
            int GxsumGreen = 0;
            int GxsumBlue = 0;

            int GysumRed = 0;
            int GysumGreen = 0;
            int GysumBlue = 0;

            // Forming 3x3 grid
            int m = 0;  // For Gx and Gy
            for (int k = i - 1; k < i + 2; k++)
            {
                if (k < 0)
                {
                    continue;
                }

                // if k > height then break
                if (k >= height)
                {
                    break;
                }
                int n = 0;  // For Gx and Gy
                for (int l = j - 1; l < j + 2; l++)
                {
                    if (l < 0)
                    {
                        continue;
                    }
                    if (l >= width)
                    {
                        break;
                    }

                    GxsumRed += Gx[m][n] * copy[k][l].rgbtRed;
                    GxsumGreen += Gx[m][n] * copy[k][l].rgbtGreen;
                    GxsumBlue += Gx[m][n] * copy[k][l].rgbtBlue;

                    GysumRed += Gy[m][n] * copy[k][l].rgbtRed;
                    GysumGreen += Gy[m][n] * copy[k][l].rgbtGreen;
                    GysumBlue += Gy[m][n] * copy[k][l].rgbtBlue;
                    n++;
                }
                m++;
            }

            int newRed = round(sqrt((GxsumRed * GxsumRed) + (GysumRed * GysumRed)));
            int newGreen = round(sqrt((GxsumGreen * GxsumGreen) + (GysumGreen * GysumGreen)));
            int newBlue = round(sqrt((GxsumBlue * GxsumBlue) + (GysumBlue * GysumBlue)));

            // Caping to 255
            if (newRed > 255)
            {
                newRed = 255;
            }

            if (newGreen > 255)
            {
                newGreen = 255;
            }

            if (newBlue > 255)
            {
                newBlue = 255;
            }

            image[i][j].rgbtRed = newRed;
            image[i][j].rgbtGreen = newGreen;
            image[i][j].rgbtBlue = newBlue;
        }
    }
    return;
}

This CS50 Problem-Set 4. filter-more problem. Everything is okay except for edges function. ChatGPT says that my code is correct, but check50 says it is not correct why? What did I do wrong?

Please can you anyone tell me? Please Please?

This is all the details:

check50

1

There are 1 best solutions below

0
chqrlie On

The problem is you increment n and m at the end of the loop bodies, but this increment is bypassed when k < 0 or l < 0 respectively are true and the continue statement is evaluated.

You should either avoid the continue statement or move the increment to the third clause of the for statements.

Breaking from the loops early if k >= height or l >= width is an optimisation that breaks the symmetry of the code and makes it more difficult for the compiler to unroll or parallelize the loops. This optimisation is marginal for even moderately large images. It is recommended to favor code readability and regularity over premature optimisation.

Here is a modified version of the inner loops:

            // Forming 3x3 grid
            for (int m = 0; m < 3; m++)
            {
                int k = i - 1 + m;
                if (k >= 0 && k < height)
                {
                    for (int n = 0; n < 3; n++)
                    {
                        int l = j - 1 + n;
                        if (l >= 0 && l < width)
                        {
                            GxsumRed   += Gx[m][n] * copy[k][l].rgbtRed;
                            GxsumGreen += Gx[m][n] * copy[k][l].rgbtGreen;
                            GxsumBlue  += Gx[m][n] * copy[k][l].rgbtBlue;

                            GysumRed   += Gy[m][n] * copy[k][l].rgbtRed;
                            GysumGreen += Gy[m][n] * copy[k][l].rgbtGreen;
                            GysumBlue  += Gy[m][n] * copy[k][l].rgbtBlue;
                        }
                    }
                }
            }