c-Cs50 pset4滤波器灰度圆形函数问题



我正在研究cs50 pset4滤波器(不太舒服(灰度,如果数字是小数,我必须对它们进行四舍五入。但由于某种原因,check50会打印以下内容:

:( grayscale correctly filters single pixel without whole number average
expected "28 28 28n", not "27 27 27n"
:( grayscale correctly filters more complex 3x3 image
expected "20 20 20n50 5...", not "20 20 20n50 5..."
:( grayscale correctly filters 4x4 image
expected "20 20 20n50 5...", not "20 20 20n50 5..."

这些只是悲伤的面孔。这是我的代码:

void grayscale(int height, int width, RGBTRIPLE image[height][width])
{
for(int j = 0; j < width; j++)
for(int i = 0; i < height; i ++) {
double av = (image[i][j].rgbtGreen + image[i][j].rgbtRed + image[i][j].rgbtBlue)/3;
int average = round(av);
image[i][j].rgbtGreen = average;
image[i][j].rgbtRed = average;
image[i][j].rgbtBlue = average;
}
}

圆形函数在这里:

int average = round(av);

但根据check50,它不起作用。请帮我弄清楚。我唯一的怀疑是我是c的新手,所以我的功能可能有问题。我试着在谷歌上搜索,但没有任何意义。我有

#include<math.h>

部分,就在我向您展示的部分之上。

谢谢,丢失在代码中:(

这似乎是划分的结果

(image[i][j].rgbtGreen + image[i][j].rgbtRed + image[i][j].rgbtBlue)/3

被截断,因为所有成员都是整数。

尝试

(image[i][j].rgbtGreen + image[i][j].rgbtRed + image[i][j].rgbtBlue)/3.0

(使用3.0而不是3

不使用双数据类型,而是使用浮动

float av = (image[i][j].rgbtGreen + image[i][j].rgbtRed + image[i][j].rgbtBlue)/3.0

使用3.0,因为在某些情况下,你的值可能是整数,所以它不会四舍五入到最近的整数

最新更新