# Problem Set 4 - Filter (Blur)

I know its pretty lame and uninspired to hard-code each and every special case of the blur function, but somehow it still refuses to pass most of check50's tests (aside from the middle and edge pixel calculations). Any ideas on what might be causing this?

``````void blur(int height, int width, RGBTRIPLE image[height][width])
{
float avgRed, avgGreen, avgBlue;
RGBTRIPLE orig[height][width];
for(int i = 0; i < height; i++)
{

for (int j = 0; j < width; j++)
{
orig[i][j] = image[i][j];
}
}

for (int i = 0; i < height; i++)
{
for (int j = 1; j < width; j++)
{
avgRed = orig[i][j].rgbtRed;
avgGreen = orig[i][j].rgbtGreen;
avgBlue = orig[i][j].rgbtBlue;
int counter = 1;
if(j - 1 > -1) //possible to add the element to the left
{
avgRed+= orig[i][j-1].rgbtRed;
avgGreen+= orig[i][j-1].rgbtGreen;
avgBlue+= orig[i][j-1].rgbtBlue;
counter++;
}
if (i - 1 > -1) //possible to add the element above
{
avgRed+= orig[i-1][j].rgbtRed;
avgGreen+= orig[i-1][j].rgbtGreen;
avgBlue+= orig[i-1][j].rgbtBlue;
counter++;
}
if (j + 1 < width) //possible to add the element to the right
{
avgRed+= orig[i][j+1].rgbtRed;
avgGreen+= orig[i][j+1].rgbtGreen;
avgBlue+= orig[i][j+1].rgbtBlue;
counter++;
}
if (i + 1 < height) //possible to add the element below
{
avgRed+= orig[i+1][j].rgbtRed;
avgGreen+= orig[i+1][j].rgbtGreen;
avgBlue+= orig[i+1][j].rgbtBlue;
counter++;
}
if (j - 1 > -1 && i - 1 > -1)  //possible to add the element upper left
{
avgRed+= orig[i-1][j-1].rgbtRed;
avgGreen+= orig[i-1][j-1].rgbtGreen;
avgBlue+= orig[i-1][j-1].rgbtBlue;
counter++;
}
if (j + 1 < width && i - 1 > -1)  //possible to add the element upper right
{
avgRed+= orig[i-1][j+1].rgbtRed;
avgGreen+= orig[i-1][j+1].rgbtGreen;
avgBlue+= orig[i-1][j+1].rgbtBlue;
counter++;
}
if (j - 1 > -1 && i + 1 < height)  //possible to add the element bottom left
{
avgRed+= orig[i+1][j-1].rgbtRed;
avgGreen+= orig[i+1][j-1].rgbtGreen;
avgBlue+= orig[i+1][j-1].rgbtBlue;
counter++;
}
if (j + 1 < width && i + 1 < height) //possible to add the element bottom right
{
avgRed+= orig[i+1][j+1].rgbtRed;
avgGreen+= orig[i+1][j+1].rgbtGreen;
avgBlue+= orig[i+1][j+1].rgbtBlue;
counter++;
}
avgRed = avgRed/counter;
avgBlue = avgBlue/counter;
avgGreen = avgGreen/counter;
image[i][j].rgbtRed = round(avgRed);
image[i][j].rgbtBlue = round(avgBlue);
image[i][j].rgbtGreen = round(avgGreen);
}
}
return;
}
``````

## 1 Answer

``````for (int i = 0; i < height; i++)
{
for (int j = 1; j < width; j++)
{
``````

Your `j` variable here shouldn't start at 1.

• My god am I an idiot, everything is working now. Thanks so much. – Sorphil Mar 28 '20 at 12:14