# Not getting the edge function to work

Please find my code for the edge function below, I'm getting all errors on check50 but can't understand what's wrong.

``````// Detect edges
void edges(int height, int width, RGBTRIPLE image[height][width])
{

//Initialize variables
int RedGx = 0;
int GreenGx = 0;
int BlueGx = 0;
int RedGy = 0;
int GreenGy = 0;
int BlueGy = 0;
int Redsum = 0;
int Greensum = 0;
int Bluesum = 0;

//Create temporary array & populate kernels
RGBTRIPLE temp[height][width];
//Kernel 1
int Gx[3][3] = {{-1, 0, 1}, {-2, 0, 2}, {-1, 0, 1}};
//Kernel 2
int Gy[3][3] = {{-1, -2, -1}, {0, 0, 0}, {1, 2, 1}};

//Loop trough all the rows of the image array
for (int i = 0; i < height; i++)
{
//Loop trough all the columns of the image array
for (int j = 0; j < width; j++)
{
//Reset the temporary variables before moving on to next pixel
Redsum = 0;
Greensum = 0;
Bluesum = 0;
RedGx = 0;
GreenGx = 0;
BlueGx = 0;
RedGy = 0;
GreenGy = 0;
BlueGy = 0;

//Loop over the adjacent 9 pixels (first rows then columns)
for (int k = -1; k < 2; k++)
{
for (int l = -1; l < 2; l++)
{
//If a pixel is outside of the grid boundaries of the image array
//skip and move to next iteration as it will not affect the sum
//(because Red = 0,Green = 0, Blue = 0)
if (i + k < 0 || i + k > height -1 || j + l < 0 || j + l < width - 1)
{
continue;
}
//For those pixels that are within the grid
//multiply each with the corresponding pixel in the Gx and Gy array respectively
else
{
RedGx = RedGx + (image[i + k][j + l].rgbtRed * Gx[k + 1][l + 1]);
GreenGx = GreenGx + (image[i + k][j + l].rgbtGreen * Gx[k + 1][l + 1]);
BlueGx = BlueGx + (image[i + k][j + l].rgbtBlue * Gx[k + 1][l + 1]);

RedGy = RedGy + (image[i + k][j + l].rgbtRed * Gy[k + 1][l + 1]);
GreenGy = GreenGy + (image[i + k][j + l].rgbtGreen * Gy[k + 1][l + 1]);
BlueGy = BlueGy + (image[i + k][j + l].rgbtBlue * Gy[k + 1][l + 1]);
}
}
}
//Square the Gx and Gy values, take the square root and store it in temporary variable
Redsum = sqrt(RedGx * RedGx + RedGy * RedGy);
Greensum = sqrt(GreenGx * GreenGx + GreenGy * GreenGy);
Bluesum = sqrt(BlueGx * BlueGx + BlueGy * BlueGy);

//Cap the sum for each value at 255
if (Redsum >= 255)
{
Redsum = 255;
}
if (Greensum >= 255)
{
Greensum = 255;
}
if (Bluesum >= 255)
{
Bluesum = 255;
}

//Store the rounded values in the temporary array
temp[i][j].rgbtRed = round(Redsum);
temp[i][j].rgbtGreen = round(Greensum);
temp[i][j].rgbtBlue = round(Bluesum);
}
}
//Copy the temp array to the original image array
for (int i = 0; i < height ; i++)
{
for (int j = 0; j < width; j++)
{
image[i][j].rgbtRed = temp[i][j].rgbtRed;
image[i][j].rgbtGreen = temp[i][j].rgbtGreen;
image[i][j].rgbtBlue = temp[i][j].rgbtBlue;
}
}
// Return from function when done
return;
}
``````

Fixed it, some bugs and the major one was in the if statement that should limit which pixels to move through. Posting final code.

``````// Detect edges
void edges(int height, int width, RGBTRIPLE image[height][width])
{

//Initialize variables
float RedGx = 0;
float GreenGx = 0;
float BlueGx = 0;
float RedGy = 0;
float GreenGy = 0;
float BlueGy = 0;
float Redsum = 0;
float Greensum = 0;
float Bluesum = 0;

//Create temporary array & populate kernels
RGBTRIPLE temp[height][width];
//Kernel 1
int Gx[3][3] = {{-1, 0, 1}, {-2, 0, 2}, {-1, 0, 1}};
//Kernel 2
int Gy[3][3] = {{-1, -2, -1}, {0, 0, 0}, {1, 2, 1}};

//Loop trough all the rows of the image array
for (int i = 0; i < height; i++)
{
//Loop trough all the columns of the image array
for (int j = 0; j < width; j++)
{
//Reset the temporary variables before moving on to next pixel
Redsum = 0;
Greensum = 0;
Bluesum = 0;
RedGx = 0;
GreenGx = 0;
BlueGx = 0;
RedGy = 0;
GreenGy = 0;
BlueGy = 0;

//Loop over the adjacent 9 pixels (first rows then columns)
for (int k = -1; k < 2; k++)
{
for (int l = -1; l < 2; l++)
{
//If a pixel is outside of the grid boundaries of the image array
//skip and move to next iteration as it will not affect the sum
//(because Red = 0,Green = 0, Blue = 0)
if (i + k < 0 || i + k > height - 1 || j + l < 0 || j + l > width - 1)
{
continue;
}
//For those pixels that are within the grid
//multiply each with the corresponding pixel in the Gx and Gy array respectively
else
{
RedGx = RedGx + (image[i + k][j + l].rgbtRed * Gx[k + 1][l + 1]);
GreenGx = GreenGx + (image[i + k][j + l].rgbtGreen * Gx[k + 1][l + 1]);
BlueGx = BlueGx + (image[i + k][j + l].rgbtBlue * Gx[k + 1][l + 1]);

RedGy = RedGy + (image[i + k][j + l].rgbtRed * Gy[k + 1][l + 1]);
GreenGy = GreenGy + (image[i + k][j + l].rgbtGreen * Gy[k + 1][l + 1]);
BlueGy = BlueGy + (image[i + k][j + l].rgbtBlue * Gy[k + 1][l + 1]);
}
}
}
//After looping the adjacent pixels square Gx and Gy values and take the square root
//of the sum and store it in temporary variable
Redsum = sqrt((RedGx * RedGx) + (RedGy * RedGy));
Greensum = sqrt((GreenGx * GreenGx) + (GreenGy * GreenGy));
Bluesum = sqrt((BlueGx * BlueGx) + (BlueGy * BlueGy));

//Cap the sum for each value at 255
if (Redsum >= 255)
{
Redsum = 255;
}
if (Greensum >= 255)
{
Greensum = 255;
}
if (Bluesum >= 255)
{
Bluesum = 255;
}

//Store the rounded values in the temporary array
temp[i][j].rgbtRed = round(Redsum);
temp[i][j].rgbtGreen = round(Greensum);
temp[i][j].rgbtBlue = round(Bluesum);
}
}
//Copy the temp array to the original image array
for (int i = 0; i < height ; i++)
{
for (int j = 0; j < width; j++)
{
image[i][j].rgbtRed = temp[i][j].rgbtRed;
image[i][j].rgbtGreen = temp[i][j].rgbtGreen;
image[i][j].rgbtBlue = temp[i][j].rgbtBlue;
}
}
// Return from function when done
return;
}
``````