I've seen all the other questions and answers regarding this specific error. Thankfully, or not so, my error was not due to the order in which I looped through the image. Here's my code:

void grayscale(int height, int width, RGBTRIPLE image[height][width])
    for (int i = 0; i < height; i++)
        for (int j = 0; i < width; i++)
            int avg = round((image[i][j].rgbtRed + image[i][j].rgbtGreen + image[i][j].rgbtBlue) / 3.0);
            image[i][j].rgbtRed = avg;
            image[i][j].rgbtGreen = avg;
            image[i][j].rgbtBlue = avg;
    // iterate through row
    //   iterate through each pixel in the row
    //     calculate average rgb value
    //     change values to average (rounded to nearest int)

Previous posts used i for width and j for height, creating the problem. Even though that wasn't the issue, I tried reversing the order and I didn't get the error message but the program still wouldn't work. I also use 3.0 instead of 3 to make the division a float to be able to round it. I'm not quite sure what else to do, hopefully one of you knows.

1 Answer 1


here " for (int j = 0; i < width; i++)" instead of 'j' you used 'i' by mistake..

  • Thank you! Can't believe I didn't see that, I guess I tend to go over the "basic" parts of the code as if I was practiced enough to not make mistakes there. Jul 26, 2020 at 17:18
  • haha it happens to everyone. sometimes it just needs a second eye to find out what's wrong.
    – Himanshi
    Jul 27, 2020 at 12:13

You must log in to answer this question.

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