Could someone explain me why there is a difference when you use float in front of image[i][j] compare to not using it. Because if I'm not mistaken, image[i][j].rgbtRed or the 2 others are already integers so I don't get why it needs to be a float. I have question for the blur filter. thx for your answers.

// Convert image to grayscale

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

1 Answer 1


If all values in the equation are integers, integer math will be performed (i.e. the remainder will be dropped - truncated towards zero - and the result will be an integer). In this case, the call to round would be meaningless since there's no remainder.

One note: floating point math is performed if any value in the equation is floating, so even just making the denominator 3.0 instead of 3 would be sufficient.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .