first post on here so be nice :)

I'm working my way through CS50 on edx.org, I've come to pset4 - Filter (less) and have become absolutely stuck.

I have programmed the other 3 photo filters with no issue, (grayscale, reverse & sepia) but have a strange issue with blur.

Off the bat the code 'works'. By this I mean the picture outputted is blurred and looks the same as the original in terms of colour etc, so at a high level its doing it mostly right. When I run check50 I get 2 OR 3 errors. And this changes from time to time whether I get 2 or 3.

I started doing my best to debug it, and I created an artifical 3x3 array or 'RGB' values and then printed the output to see what was going on. I found that all values were correct apart from the output at array[0][0], this number was changing each time the program ran (with no changes to the file) between the correct value 30, and -(2^32)/2, and other negative values. When I printed that single element from the arrray on its own, the value was 30 and did not change.

In this photo below I have used a copied version of the code, pretty much the same just the input to the code are 3off 3x3 arrays simulating the RGB values. Apart from that it is the same. Hopefully you can see in the terminal window that the output matrix at [0][0] is changing each time the code is run. If I only print that value [0][0], it will consistently print 30 (correct).

Error Photo[![]]1

// Blur image
void blur(int height, int width, RGBTRIPLE image[height][width])
int Red[9];
int Green[9];
int Blue[9];
int divisor = 0;
float RedTotal, GreenTotal, BlueTotal;

//Make copy of image
RGBTRIPLE ogImage[height][width];
for (int i = 0; i < height; i++)
    for (int j = 0; j < width; j++)
        ogImage[i][j] = image[i][j];

//Loop through height and width
for (int i = 0; i < height; i++)
    for (int j = 0; j < width; j++)
        //Loops in a 3x3 grid, starting 1 before each row & coloumn
        for (int k = -1; k < 2; k++)
            for (int l = -1; l < 2; l++)
                //Fill values if pixel is within image confines
                if ((i + k) >= 0 && (i + k) < height && (j + l) >= 0 && (j + l) < width)
                    Red[divisor] = ogImage[i + k][j + l].rgbtRed;
                    Green[divisor] = ogImage[i + k][j + l].rgbtGreen;
                    Blue[divisor] = ogImage[i + k][j + l].rgbtBlue;
                    //Count up divisor to know what to divide by

        //Sum R,G & B value
        for (int m = 0; m < divisor; m++)
            RedTotal += Red[m];
            GreenTotal += Green[m];
            BlueTotal += Blue[m];

        //Update image[i][j] with average RGB values
        image[i][j].rgbtRed = round(RedTotal / divisor);
        image[i][j].rgbtGreen = round(GreenTotal / divisor);
        image[i][j].rgbtBlue = round(BlueTotal / divisor);

        //Clear all variables for next pixel
        divisor = 0;
        RedTotal = 0;
        GreenTotal = 0;
        BlueTotal = 0;
        memset(Red, 0, sizeof(Red));
        memset(Green, 0, sizeof(Green));
        memset(Blue, 0, sizeof(Blue));


This is the output from check50. Sometimes I get this error below, OR the bottom 3 errors are red. And it changes each time I run check50.

Check50 output

I really cant think why this code changes each time I run it, any help would be greatly appreciated! Many thanks. Jim.


I was very intriged because looking through your code I couldn't find anything wrong. So I compared with my blur version and they were very similar except for one thing. For my average calculations I used a double variable. I test your code and it passed ok but I can't explain why. If you find out please let me know.


Your code assumes that every pixel can be made the centre pixel in a 3x3 block of pixels. This is not true for corner pixels as well as pixels at the vertical and horizontal borders of the image.

  1. Corner pixels are part of a 2x2 block,
  2. Pixels at the vertical edges would require a 3x2 block of pixels
  3. Pixels at the horizontal edge would require a 2x3 block of pixels

Your code should be able to check for these differences and adjust accordingly.

Hope this helps!

You must log in to answer this question.

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