# Problem with Edges Pset4

I've been at this Pset for a while now, I've got everything down except for Edges, and if I'm understanding correctly, every single value is coming out correctly except for the values for blue are 99% of the time off, I've been going over the code for a while now and I can't find anything that makes blue stand out, maybe I understand the check50 output wrong, but either way if anyone can help me with where the bug is I'd really appreciate it. Here's my code:

void edges(int height, int width, RGBTRIPLE image[height][width]){
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}};
int GxR = 0;
int GxG = 0;
int GxB = 0;
int GyR = 0;
int GyG = 0;
int GyB = 0;
float GR = 0;
float GG = 0;
float GB = 0;
int red = 0;
int green = 0;
int blue = 0;
RGBTRIPLE temp[height][width];
for (int i = 0; i < height; i++)
{
for (int j = 0; j < width; j++)
{
temp[i][j] = image[i][j];
temp[i][j].rgbtRed = image[i][j].rgbtRed;
temp[i][j].rgbtGreen = image[i][j].rgbtGreen;
temp[i][j].rgbtBlue = image[i][j].rgbtBlue;
}
}
for (int i = 0; i < height; i++)
{
for (int j = 0; j < width; j++)
{
//iterate over the 9 surrounding pixels as long as they're within bounds
for (int k = i - 1; k < i + 2; k++)
{
for (int m = j - 1; m < j + 2; m++)
{

if (k < 0 || m < 0 || m >= width || k >= height)
{

}
else
{
GxR += temp[k][m].rgbtRed*Gx[k - (i - 1)][m - (j - 1)];
GxG += temp[k][m].rgbtGreen*Gx[k - (i - 1)][m - (j - 1)];
GxB += temp[k][m].rgbtBlue*Gx[k - (i - 1)][m - (j - 1)];
GyR += temp[k][m].rgbtRed*Gy[k - (i - 1)][m - (j - 1)];
GyG += temp[k][m].rgbtGreen*Gy[k - (i - 1)][m - (j - 1)];
GyB += temp[k][m].rgbtBlue*Gy[k - (i - 1)][m - (j - 1)];
}
}
}
GR = sqrt((GxR * GxR) + (GyR * GyR));
GG = sqrt((GxG * GxG) + (GyG * GyG));
GB = sqrt((GxB * GxB) + (GyB * GyB));
red = round(GR);
green = round(GG);
blue = round(GB);
if (GR > 255)
{
GR = 255;
}
if (GG > 255)
{
GG = 255;
}
if (GB > 255)
{
GB = 255;
}
image[i][j].rgbtRed = red;
image[i][j].rgbtGreen = green;
image[i][j].rgbtBlue = blue;
GxR = 0;
GxR = 0;
GxG = 0;
GxB = 0;
GyR = 0;
GyG = 0;
GyB = 0;
GR = 0;
GG = 0;
GB = 0;
}
}
return;

} When I use Check50 I get the following Output:

Here's the 4x4 one in detail

And this is the Output I get. It's so close yet I can't figure out what's wrong:

Edit: I also just noticed that it's only wrong when the expected Blue output is 255

Found it! Just in case anyone faces the same problem in the future.

if (GR > 255)
{
GR = 255;
}
if (GG > 255)
{
GG = 255;
}
if (GB > 255)
{
GB = 255;
}
red = round(GR);
green = round(GG);
blue = round(GB);

I set the final values for red green and blue before I checked if any of them went over 255. I switched the order of the code and now it works perfectly!