CS 50 – Filter More Edges – all test cases passing (incl. 3×3) but not 4×4

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.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật