我正在使用cs50 pset4 filter(不太舒服)灰度,如果数字是小数,我必须对它们进行舍入。但出于某种原因,check50打印了以下内容:
:( grayscale correctly filters single pixel without whole number average
expected "28 28 28\n", not "27 27 27\n"
:( grayscale correctly filters more complex 3x3 image
expected "20 20 20\n50 5...", not "20 20 20\n50 5..."
:( grayscale correctly filters 4x4 image
expected "20 20 20\n50 5...", not "20 20 20\n50 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;
}
}round函数如下:
int average = round(av);但根据check50的说法,它不起作用。请帮我弄清楚。我唯一的怀疑是我对c是个新手,所以我的函数可能有问题。我试着用谷歌搜索了一下,但什么都说不通。我确实有
#include<math.h>我的代码中的一部分,就在我展示给你的部分之上。
谢谢,迷失在代码中:)
发布于 2020-08-15 22:53:19
这似乎是分裂的结果
(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
发布于 2020-08-15 23:09:36
使用浮点型而不是双精度数据类型
float av = (image[i][j].rgbtGreen + image[i][j].rgbtRed + image[i][j].rgbtBlue)/3.0请使用3.0,因为在某些情况下,您的值可能是整数,因此不会舍入到最接近的整数
https://stackoverflow.com/questions/63427139
复制相似问题