October 22, 2024
Chicago 12, Melborne City, USA
C++

Trouble with CS50 filter-less (blur) code works on an image but only passes 1/5 checks


I’ve been stuck with the same error messages and wondering if someone can help point out what I’m missing.

Currently the code does blur an image but the only check the code passes is regarding the corner pixels, by that logic i am finding it hard to understand why the edge and middle pixels would not work.

I am very new to programming so forgive the repeated code i’m sure there is a better way to approach this but i would first like to see if i can get this method to pass the checks after making it this far into it.

// Blur image
void blur(int height, int width, RGBTRIPLE image[height][width])
{
    RGBTRIPLE copy[height][width];
    float count = 0;

    for (int i = 0; i < height; i++)
    {
        for (int j = 0; j < width; j++)
        {
            copy[i][j] = image[i][j];
            // check that the + 1's and - 1's won't take loop out of bounds
            // left bottom corner (/4)
            if (i + 1 == height && j - 1 < 0)
            {
                count = 4.0;
                // red
                copy[i][j].rgbtRed = (image[i][j].rgbtRed + image[i - 1][j].rgbtRed +
                                      image[i][j + 1].rgbtRed + image[i - 1][j + 1].rgbtRed)  / count;
                // green
                copy[i][j].rgbtGreen = (image[i][j].rgbtGreen + image[i - 1][j].rgbtGreen +
                                        image[i][j + 1].rgbtGreen + image[i - 1][j + 1].rgbtGreen)  / count;
                // blue
                copy[i][j].rgbtBlue = (image[i][j].rgbtBlue + image[i - 1][j].rgbtBlue +
                                       image[i][j + 1].rgbtBlue + image[i - 1][j + 1].rgbtBlue)  / count;
            }
            // right top corner
            else if (i - 1 < 0 && j + 1 == width)
            {
                count = 4.0;
                // red
                copy[i][j].rgbtRed = (image[i][j].rgbtRed + image[i + 1][j].rgbtRed +
                                      image[i][j - 1].rgbtRed + image[i + 1][j - 1].rgbtRed)  / count;
                // green
                copy[i][j].rgbtGreen = (image[i][j].rgbtGreen + image[i + 1][j].rgbtGreen +
                                        image[i][j - 1].rgbtGreen + image[i + 1][j - 1].rgbtGreen)  / count;
                // blue
                copy[i][j].rgbtBlue = (image[i][j].rgbtBlue + image[i + 1][j].rgbtBlue +
                                       image[i][j - 1].rgbtBlue + image[i + 1][j - 1].rgbtBlue)  / count;
            }
            // right bottom corner
            else if (i + 1 == height && j + 1 == width)
            {
                count = 4.0;
                // red
                copy[i][j].rgbtRed = (image[i][j].rgbtRed + image[i - 1][j].rgbtRed +
                                      image[i][j - 1].rgbtRed + image[i - 1][j - 1].rgbtRed)  /
                                      count;
                // green
                copy[i][j].rgbtGreen = (image[i][j].rgbtGreen + image[i - 1][j].rgbtGreen +
                                        image[i][j - 1].rgbtGreen + image[i - 1][j - 1].rgbtGreen)  / c
                                        ount;
                // blue
                copy[i][j].rgbtBlue = (image[i][j].rgbtBlue + image[i - 1][j].rgbtBlue +
                                       image[i][j - 1].rgbtBlue + image[i - 1][j - 1].rgbtBlue)  /
                                       count;
            }
            // left top corner
            else if (i - 1 < 0 && j - 1 < 0)
            {
                count = 4.0;
                // red
                copy[i][j].rgbtRed = (image[i][j].rgbtRed + image[i + 1][j].rgbtRed +
                                      image[i][j + 1].rgbtRed + image[i + 1][j + 1].rgbtRed)  /
                                      count;
                // green
                copy[i][j].rgbtGreen = (image[i][j].rgbtGreen + image[i + 1][j].rgbtGreen +
                                        image[i][j + 1].rgbtGreen + image[i + 1][j + 1].rgbtGreen)  /
                                        count;
                // blue
                copy[i][j].rgbtBlue = (image[i][j].rgbtBlue + image[i + 1][j].rgbtBlue +
                                       image[i][j + 1].rgbtBlue + image[i + 1][j + 1].rgbtBlue)  /
                                       count;
            }
            // left edge (/6)
            else if (j - 1 < 0)
            {
                count = 6.0;
                // red
                copy[i][j].rgbtRed = (image[i][j].rgbtRed + image[i + 1][j].rgbtRed +
                                      image[i - 1][j].rgbtRed + image[i][j + 1].rgbtRed +
                                      image[i + 1][j + 1].rgbtRed + image[i - 1][j + 1].rgbtRed)  /
                                      count;
                // green
                copy[i][j].rgbtGreen = (image[i][j].rgbtGreen + image[i + 1][j].rgbtGreen +
                                        image[i - 1][j].rgbtGreen +
                                        image[i][j + 1].rgbtGreen + image[i + 1][j + 1].rgbtGreen +
                                        image[i - 1][j + 1].rgbtGreen)  / count;
                // blue
                copy[i][j].rgbtBlue = (image[i][j].rgbtBlue + image[i + 1][j].rgbtBlue +
                                       image[i - 1][j].rgbtBlue + image[i][j + 1].rgbtBlue +
                                       image[i + 1][j + 1].rgbtBlue + image[i - 1][j + 1].rgbtBlue)  /
                                       count;
            }
            // right edge
            else if (j + 1 == width)
            {
                count = 6.0;
                // red
                copy[i][j].rgbtRed = (image[i][j].rgbtRed + image[i + 1][j].rgbtRed +
                                      image[i - 1][j].rgbtRed + image[i][j - 1].rgbtRed +
                                      image[i + 1][j - 1].rgbtRed + image[i - 1][j - 1].rgbtRed)  /
                                      count;
                // green
                copy[i][j].rgbtGreen = (image[i][j].rgbtGreen + image[i + 1][j].rgbtGreen +
                                        image[i - 1][j].rgbtGreen + image[i][j - 1].rgbtGreen +
                                        image[i + 1][j - 1].rgbtGreen + image[i - 1][j - 1].rgbtGreen)  /
                                        count;
                // blue
                copy[i][j].rgbtBlue = (image[i][j].rgbtBlue + image[i + 1][j].rgbtBlue +
                                       image[i - 1][j].rgbtBlue + image[i][j - 1].rgbtBlue +
                                       image[i + 1][j - 1].rgbtBlue + image[i - 1][j - 1].rgbtBlue)  /
                                       count;
            }
            // top edge
            else if (i - 1 < 0)
            {
                count = 6.0;
                // red
                copy[i][j].rgbtRed = (image[i][j].rgbtRed + image[i + 1][j].rgbtRed +
                                      image[i][j + 1].rgbtRed + image[i][j - 1].rgbtRed +
                                      image[i + 1][j + 1].rgbtRed + image[i + 1][j - 1].rgbtRed)  /
                                      count;
                // green
                copy[i][j].rgbtGreen = (image[i][j].rgbtGreen + image[i + 1][j].rgbtGreen +
                                        image[i][j + 1].rgbtGreen + image[i][j - 1].rgbtGreen +
                                        image[i + 1][j + 1].rgbtGreen + image[i + 1][j - 1].rgbtGreen)  /
                                        count;
                // blue
                copy[i][j].rgbtBlue = (image[i][j].rgbtBlue + image[i + 1][j].rgbtBlue +
                                       image[i][j + 1].rgbtBlue + image[i][j - 1].rgbtBlue +
                                       image[i + 1][j + 1].rgbtBlue + image[i + 1][j - 1].rgbtBlue)  /
                                       count;
            }
            // bottom edge
            else if (i + 1 == height)
            {
                count = 6.0;
                // red
                copy[i][j].rgbtRed = (image[i][j].rgbtRed + image[i - 1][j].rgbtRed +
                                      image[i][j + 1].rgbtRed + image[i][j - 1].rgbtRed +
                                      image[i - 1][j + 1].rgbtRed + image[i - 1][j - 1].rgbtRed)  /
                                      count;
                // green
                copy[i][j].rgbtGreen = (image[i][j].rgbtGreen + image[i - 1][j].rgbtGreen +
                                        image[i][j + 1].rgbtGreen + image[i][j - 1].rgbtGreen +
                                        image[i - 1][j + 1].rgbtGreen + image[i - 1][j - 1].rgbtGreen)  /
                                        count;
                // blue
                copy[i][j].rgbtBlue = (image[i][j].rgbtBlue + image[i - 1][j].rgbtBlue +
                                       image[i][j + 1].rgbtBlue + image[i][j - 1].rgbtBlue +
                                       image[i - 1][j + 1].rgbtBlue + image[i - 1][j - 1].rgbtBlue)  /
                                       count;
            }
            else
            {
                count = 9.0;
                // red
                copy[i][j].rgbtRed =
                    (image[i][j].rgbtRed + image[i + 1][j].rgbtRed + image[i - 1][j].rgbtRed +
                     image[i][j + 1].rgbtRed + image[i][j - 1].rgbtRed + image[i + 1][j + 1].rgbtRed +
                     image[i + 1][j - 1].rgbtRed + image[i - 1][j + 1].rgbtRed +
                     image[i - 1][j - 1].rgbtRed)  / count;
                // green
                copy[i][j].rgbtGreen =
                    (image[i][j].rgbtGreen + image[i + 1][j].rgbtGreen + image[i - 1][j].rgbtGreen +
                     image[i][j + 1].rgbtGreen + image[i][j - 1].rgbtGreen +
                     image[i + 1][j + 1].rgbtGreen + image[i + 1][j - 1].rgbtGreen +
                     image[i - 1][j + 1].rgbtGreen + image[i - 1][j - 1].rgbtGreen)  / count;
                // blue
                copy[i][j].rgbtBlue =
                    (image[i][j].rgbtBlue + image[i + 1][j].rgbtBlue + image[i - 1][j].rgbtBlue +
                     image[i][j + 1].rgbtBlue + image[i][j - 1].rgbtBlue +
                     image[i + 1][j + 1].rgbtBlue + image[i + 1][j - 1].rgbtBlue +
                     image[i - 1][j + 1].rgbtBlue + image[i - 1][j - 1].rgbtBlue) / count;
            }

            image[i][j].rgbtRed = round(copy[i][j].rgbtRed);
            image[i][j].rgbtGreen = round(copy[i][j].rgbtGreen);
            image[i][j].rgbtBlue = round(copy[i][j].rgbtBlue);
        }
    }
    return;
}

check50 error messages:
🙁 blur correctly filters middle pixel
expected "127 140 149\n", not "145 160 169\n"

🙁 blur correctly filters pixel on edge
expected "80 95 105\n", not "90 106 116\n"
🙂 blur correctly filters pixel in corner

🙁 blur correctly filters 3×3 image
expected "70 85 95\n80 9…", not "70 85 95\n90 1…"

🙁 blur correctly filters 4×4 image
expected "70 85 95\n80 9…", not "70 85 95\n90 1…"



You need to sign in to view this answers

Leave feedback about this

  • Quality
  • Price
  • Service

PROS

+
Add Field

CONS

+
Add Field
Choose Image
Choose Video