1

Below is the code that generated the error message. From what i can tell some part of this created a [j] value that was larger than the width.

My thoughts on this problem was:

With a hypothetical width of 300, 150 would be the middle point. And to the distance of [p] from the middle would be the int 'distance'. The formula j = ((-distance from middle * 2) + 1) is my way of moving j to its opposite side from the middle point. If j > half it would be moved backwards that many places from the middle and, If j < half it would be moved forward that many places from the middle.

Where is the error in this logic? Might it be the formula for generating 'distance' or in the way the code is written? It compiles fine, but doesn't execute.

void reflect(int height, int width, RGBTRIPLE image[height][width]) { for( int i = 0; i < height; i++) { for( int j = 0; j < width; j++) { float x = (width / 2); int half = round(x); int distance = ( half - (j - 1) * 2 ) - 1;

        // Width is an even number
        // If its larger than the half way point the number increases by two for every increment away.
        // So... j = ((-distance from middle * 2) + 1)
        // how do we find the number away from the middle. 
        // we know J and width 
            if (j == half)
            {
                image[i][j] = image[i][j];
            }

            if (j > half)
            {

                image[i][j] = image[i][j - distance];

            }
            // j = ((distance from middle * 2) - 1)
            if (j < half)
            {

                image[i][j]= image[i][j + distance];
            }
    }

}
return;

}

1 Answer 1

3

Reading your code I can see that you're worried about the number of width pixels been even or not. Should you be worried? If you look at your code you can see that you're answering yourself. When reached the middle point you do this: image[i][j] = image[i][j]; which is like do nothing. You have to flip the half part of the pixels in the left with the ones in the right part, so if the number is even, no problem there. But if it's not, well, when you reach the middle point you leave it there as it's allways going to be the midle pixel. With that in mind I think you can simplify your code to fix this line: int distance = ( half - (j - 1) * 2 ) - 1; that is causing the index to go out of boundaries. Apart from that a hint question: how many flips do you need to do to flip an image?

You must log in to answer this question.

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