1

I wish i could be more specific but i really don't understand the errors i'm getting

This is my reflect function:

// Reflect image horizontally
void reflect(int height, int width, RGBTRIPLE image[height][width])
{
    int tempRed = 0;
    int tempGreen = 0;
    int tempBlue = 0;

    for (int i = 0; i < height; i++)
    {
        for (int j = 0; j < width; j++)
        {
            int index = width - j;

            tempRed = image[i][j].rgbtRed;
            tempGreen = image[i][j].rgbtGreen;
            tempBlue = image[i][j].rgbtBlue;

            image[i][j].rgbtRed = image[i][index].rgbtRed;
            image[i][j].rgbtGreen = image[i][index].rgbtGreen;
            image[i][j].rgbtBlue = image[i][index].rgbtBlue;

            image[i][index].rgbtRed = tempRed;
            image[i][index].rgbtGreen = tempGreen;
            image[i][index].rgbtBlue = tempBlue;
        }
    }
    return;
}

And these are the errors i'm getting:

helpers.c:89:35: runtime error: index 600 out of bounds for type 'RGBTRIPLE [width]'
helpers.c:90:37: runtime error: index 600 out of bounds for type 'RGBTRIPLE [width]'
helpers.c:91:36: runtime error: index 600 out of bounds for type 'RGBTRIPLE [width]'
helpers.c:93:13: runtime error: index 600 out of bounds for type 'RGBTRIPLE [width]'
helpers.c:94:13: runtime error: index 600 out of bounds for type 'RGBTRIPLE [width]'
helpers.c:95:13: runtime error: index 600 out of bounds for type 'RGBTRIPLE [width]'

1 Answer 1

5

For j being 0, your index would be width. But valid index is from 0 to width-1. Subtract one and it should be right.

After fixing this, I see another issue: You first swap the left half with the right, and then the right with the left, returning the image to its old state. Stop at the right point.

BTW, you can assign whole RGBTRIPLE type variables, like RGBTRIPLE temp = image[i][j];

2
  • I don't understand why the width is out of boundaries, could you explain? If I understood correctly we are to change the first pixel (j = 0) with the last pixel (j = width). Is it that in the array, width will be '/0'?
    – pedro
    Commented May 21, 2020 at 19:39
  • As I wrote in my answer, valid index ranges from 0 to width-1, so last pixel is not at width.
    – Blauelf
    Commented May 21, 2020 at 19:51

You must log in to answer this question.

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