【发布时间】:2020-06-09 13:19:30
【问题描述】:
灰度代码似乎在以整数为平均值的程序中运行良好。但会给出复杂平均值的错误,其中结果与预期代码仅相差 1。
// Convert image to grayscale
void grayscale(int height, int width, RGBTRIPLE image[height][width])
{
double avgcolor;
for (int i = 0; i < height; i++)
{
for (int j = 0; j < width; j++)
{
avgcolor = (image[i][j].rgbtRed + image[i][j].rgbtBlue + image[i][j].rgbtGreen) / 3;
image[i][j].rgbtRed = image[i][j].rgbtBlue = image[i][j].rgbtGreen = round(avgcolor);
}
}
return;
}
错误信息
:( grayscale correctly filters single pixel without whole number average
Cause
expected "28 28 28\n", not "27 27 27\n"
Log
testing with pixel (27, 28, 28)
running ./testing 0 1...
checking for output "28 28 28\n"...
Expected Output:
28 28 28
Actual Output:
27 27 27
我在另外两种情况下遇到此类错误。这可能是轮函数的一个小问题。代码查了好几遍,还是找不到错误原因。
【问题讨论】:
标签: cs50