I have written the below code for the Edges function for CS50. I pass all test cases except for the 4×4 image. I can’t seem to figure out why my code would work for a 3×3 image but not a 4×4 (or an NxN). Perhaps I’m too close to the code so I’m hoping you all can help me.
I was thinking perhaps it might be because of how I’m detecting the edges but I dont see any reason why this would be different for 3×3 or NxN
// Detect edges
void edges(int height, int width, RGBTRIPLE image[height][width])
{
RGBTRIPLE copy[height][width];
for (int i = 0; i < height; i++)
{
for (int j = 0; j < width ; j++)
{
copy[i][j] = image[i][j];
}
}
for (int i = 0; i < height; i++)
{
for (int j = 0; j < width ; j++)
{
edges_helper(height, width, i, j, image, copy);
}
}
return;
}
void edges_helper(int height, int width, int current_row, int current_column, RGBTRIPLE image[height][width], RGBTRIPLE copy[height][width])
{
// takes in my current position in the image and creates a 3x3 grid of the Red/Blue/Green value of the pixels around my position
// it multiples that 3x3 grid by Gx and Gy, sums their squares, and sets current position Red/Blue/Green equal to that
// create empty 3x3 grids for R, G, and B
int red[3][3];
int blue[3][3];
int green[3][3];
// populate Gx and Gy
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}};
// populate red, blue, and green grids
create_grids(current_row, current_column, height, width, red, blue, green, copy);
int new_red = pow(matrix_multiply(3, 3, gx, red),2) + pow(matrix_multiply(3, 3, gy, red),2);
int new_blue = pow(matrix_multiply(3, 3, gx, blue),2) + pow(matrix_multiply(3, 3, gy, blue),2);
int new_green = pow(matrix_multiply(3, 3, gx, green),2) + pow(matrix_multiply(3, 3, gy, green),2);
image[current_row][current_column].rgbtRed = minimum(round(sqrt(new_red)),255);
image[current_row][current_column].rgbtBlue = minimum(round(sqrt(new_blue)),255);
image[current_row][current_column].rgbtGreen = minimum(round(sqrt(new_green)),255);
return;
}
// populate the 3x3 grids for Red, Blue, and Green that will then multiple against Gx and Gy
void create_grids(int current_row, int current_column, int height, int width, int red[height][width], int blue[height][width], int green[height][width], RGBTRIPLE copy[height][width])
{
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
if (current_row + i - 1 < 0 || current_row + i - 1 >= height || current_column + j - 1 < 0 || current_column + j - 1 >= width)
{
red[i][j] = 0;
blue[i][j] = 0;
green[i][j] = 0;
}
else
{
red[i][j] = copy[current_row+i-1][current_column+j-1].rgbtRed;
blue[i][j] = copy[current_row+i-1][current_column+j-1].rgbtBlue;
green[i][j] = copy[current_row+i-1][current_column+j-1].rgbtGreen;
}
}
}
}
// multiple two matrices and return an integer value
int matrix_multiply(int height, int width, int x[height][width], int y[height][width])
{
int sum = 0;
for (int i = 0; i < height; i++)
{
for (int j = 0; j < width; j++)
{
sum += x[i][j] * y[i][j];
}
}
return sum;
}
int minimum(int x, int y)
{
return ((x<y) ? x : y);
}
2
You declared your function create_grids
like this:
void create_grids(int current_row, int current_column, int height, int width, int red[height][width], int blue[height][width], int green[height][width], RGBTRIPLE copy[height][width])
This is wrong. The parameters height
and width
represent the size of the image (so the size of copy
), but not the size of red
, blue
and green
. These arrays always have a size of 3 times 3, irrespective of the size of the image.
That is why your program only works when the image size is 3 times 3. When the image has a size of 4 times 4, then you specified the wrong size for the parameters red
, blue
and green
, so your program misbehaves.
In order to fix this, you can replace that line with the following:
void create_grids(int current_row, int current_column, int height, int width, int red[3][3], int blue[3][3], int green[3][3], RGBTRIPLE copy[height][width])
After doing that, your program will pass check50
.