2

I am not sure why I am having issues with my code. I know I can store the rgb values for the left pixels in 3 temporary variables then assign the right values to the left and the temp values to the right but I feel that is just writing out the swap function we learned in the lesson and that is how I saw most people do it but I would like to incorporate the swap function. Can someone help me figure out why it isn't working properly? Here is my code:

void reflect(int height, int width, RGBTRIPLE image[height][width])
{
    void swap(int *a, int *b);
    int s = 1;
    int red = 0;
    int green = 0;
    int blue = 0;
    int Red = 0;
    int Green = 0;
    int Blue = 0;
    for (int i = 0; i < height; i++)
    {
        for (int j = 0; j < width / 2; j++)
        {
            red = image[i][j].rgbtRed;
            green = image[i][j].rgbtGreen;
            blue = image[i][j].rgbtBlue;
            Red = image[i][width - s].rgbtRed;
            Green = image[i][width - s].rgbtGreen;
            Blue = image[i][width - s].rgbtBlue;
            swap(&red, &Red);
            swap(&blue, &Blue);
            swap(&green, &Green);
            s++;
        }

    }
    return;
}
void swap(int *a, int *b)
{
    int tempB = *a;
    *a = *b;
    *b = tempB;
} 

Here are the errors:

:( reflect correctly filters 1x2 image expected "0 0 255\n255 0...", not "255 0 0\n0 0 2..."

:( reflect correctly filters 1x3 image expected "0 0 255\n0 255...", not "255 0 0\n0 255..."

:) reflect correctly filters image that is its own mirror image

:( reflect correctly filters 3x3 image expected "70 80 90\n40 5...", not "10 20 30\n40 5..."

:( reflect correctly filters 4x4 image expected "100 110 120\n7...", not "10 20 30\n40 5..."

2 Answers 2

2

So my swap function was switching the values of my variables but the variables themselves did not get switched so I had to write out the code to swap the variables (from what i'm told the swap function is basically useless I guess) then make sure to initialize my s after because I originally added the variable swaps after the s and s was being incremented before the swap leading to the wrong pixels being swapped.

{
    void swap(int *a, int *b);
    int red = 0;
    int green = 0;
    int blue = 0;
    int Red = 0;
    int Green = 0;
    int Blue = 0;
    for (int i = 0; i < height; i++)
    {
        int s = 1;
        for (int j = 0; j < width / 2; j++)
        {
            red = image[i][j].rgbtRed;
            green = image[i][j].rgbtGreen;
            blue = image[i][j].rgbtBlue;
            Red = image[i][width - s].rgbtRed;
            Green = image[i][width - s].rgbtGreen;
            Blue = image[i][width - s].rgbtBlue;
            swap(&red, &Red);
            swap(&blue, &Blue);
            swap(&green, &Green);
            image[i][j].rgbtRed = red;
            image[i][j].rgbtGreen = green;
            image[i][j].rgbtBlue = blue;
            image[i][width - s].rgbtRed = Red;
            image[i][width - s].rgbtGreen = Green;
            image[i][width - s].rgbtBlue = Blue;
            s++;
        }

    }
    return;
}
void swap(int *a, int *b)
{
    int temp = *a;
    *a = *b;
    *b = temp;
}
-1

Remember that your array starts at 0, not 1. So you have to replace

Red = image[i][width - s].rgbtRed;
Green = image[i][width - s].rgbtGreen;
Blue = image[i][width - s].rgbtBlue;

with

Red = image[i][width - s - 1].rgbtRed;
Green = image[i][width - s - 1].rgbtGreen;
Blue = image[i][width - s - 1].rgbtBlue;

The difference is the - 1s

1
  • Would I need to add the - 1 though because my s int is supposed to account for the array starting at 0. I start s at 1 because if the width was 5, the 5th pixel will be 4 and the 1st would be zero so Red = image[i][5 - 1].rgbtRed; then once the swaps complete, s increments to ensure it goes to the next pixel, the 4th (3) so Red = image[i][5 - 2].rgbtRed; right? I even added the - 1's and switched my s int with j (width in iteration) but it still had the errors, and I still can't pinpoint the issue Commented Jun 22, 2020 at 0:45

You must log in to answer this question.

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