2

when I try to test the code I get an infinite loop of errors: UndefinedBehaviorSanitizer:DEADLYSIGNAL

what am i missing ???

void grayscale(int height, int width, RGBTRIPLE image[height][width]) {

for (int i = 0; i < width; i++)
{
    for (int j = 0; j < height; j++)
    {
        int greysc = round((image[i][j].rgbtRed + image[i][j].rgbtGreen + image[i][j].rgbtBlue) / 3);
        
        image[i][j].rgbtRed = greysc;
        image[i][j].rgbtGreen = greysc;
        image[i][j].rgbtBlue = greysc;
    }
}
return;

}

1

You are getting that error because the width and height variables in your for loops are switched.

Although fixing that will get the code running, you won't pass check50 because the average operation is done with int operators, and that will result in a trimmed value. For example if the result of the division is 1.9, you'll get 1 and the decimal part will be lost. To get around it, you can divide by 3.0.

| improve this answer | |
  • i would not have noticed that in a million years ! thanks! – andream Jul 20 at 14:30
1

There's 2 problem with your code.

1st. Your height and width variables are switched (you assigned "i" to width and "j" to height then used image[i][j]) (this is reason behind the error messages!)

Sol.--> Either change "i" to height and "j" to width in for loops. Or change "image[i][j]" to "image[j][i]" everywhere.

2nd. Your average value of RGB is being calculated as an int. That means all decimal points are being dropped from result. (your code will fail check50)

Sol.--> replace "3" with a "3.0" and use round function to round off the float to an int (round gives rounded values away from zero while calculating directly to int gives value closer to zero there's no visible difference in result images but check50 will find this error)

Result code:

for (int i = 0; i < height; i++)
{
    for (int j = 0; j < width; j++)
    {
        int greysc = round((image[i][j].rgbtRed + image[i][j].rgbtGreen + image[i][j].rgbtBlue) / (3.0));
        
        image[i][j].rgbtRed = greysc;
        image[i][j].rgbtGreen = greysc;
        image[i][j].rgbtBlue = greysc;
    }
}
return;
| improve this answer | |
0

Remember taking average of all the RGB values of a pixel results in a point notation figure which means that you have to store it in float

float (any_name) = round(average_formula);

So assigning average value directly to the pixel won't work, as pixels only take integers, so you need to pass that float value to an int

int (any_name) = round(float (name_above));
| improve this answer | |

You must log in to answer this question.

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