0

Reflects sort of works, but when running cs50 check the message:

:( reflect correctly filters 1x2 image
    expected "0 0 255\n255 0...", not "0 0 255\n0 0 2..."
:( reflect correctly filters 1x3 image
    expected "0 0 255\n0 255...", not "0 0 255\n0 255..."
:) reflect correctly filters image that is its own mirror image
:( reflect correctly filters 3x3 image
    expected "70 80 90\n40 5...", not "70 80 90\n40 5..."
:( reflect correctly filters 4x4 image
    expected "100 110 120\n7...", not "100 110 120\n7..."

When I don't divide by a float width/2.0 (that is width/2) only one of those :( lines appear. What could I be doing wrong?

Insted of using lines for the three colours, could I be using RGBTRIPLE once and it will affect all the variables?

Should I be explicitly be using pointers? (that is writing int*... etc etc)

void reflect(int height, int width, RGBTRIPLE image[height][width])
{
    int tempRed[height][width];
    int tempBlue[height][width];
    int tempGreen[height][width];
    
    for (int i=0 ; i<height ; i++)
    {
        for (int j=0 ; j<width ; j++)
        {
            tempRed[i][j]=image[i][j].rgbtRed;
            tempBlue[i][j]=image[i][j].rgbtBlue;
            tempGreen[i][j]=image[i][j].rgbtGreen;
        }
    }
    
    for (int i=0 ; i<height ; i++)
    {
        for (int j=0 ; j<width ; j++)
        {
            
            if (j<round((width)/2.0))
            {
                image[i][j].rgbtRed=image[i][width-1-j].rgbtRed;
                image[i][j].rgbtBlue=image[i][width-1-j].rgbtBlue;
                image[i][j].rgbtGreen=image[i][width-1-j].rgbtGreen;
            }
            
            else if (j>round((width)/2.0))
            {
                image[i][j].rgbtRed=tempRed[i][width-1-j];
                image[i][j].rgbtBlue=tempBlue[i][width-1-j];
                image[i][j].rgbtGreen=tempGreen[i][width-1-j];
            }
        }
    }
return;
}
1

At first glance at the check50 results, it seems like the last line of the image isn't being processed. But that's not the case. In reality, the left half is being updated to look like the right, but the right half isn't.

It's a complicated little problem, and I'll leave it to you to troubleshoot, but here are some hints. The short version is that the else if clause isn't executing for 3x3 at all, and doesn't start soon enough with 4x4.

What's the impact on finding the midpoint of the array, given that an array index would run from 0 to width-1 rather than 1 to width?

What happens when j == round((width)/2.0)?

And yes, you can just use RGPTRIPLE and do a single assignment instead of 3 individual ones, i.e., RGBTRIPLE temp[height][width]

There may be other issues, but I'll let you work through them. I sense that you have the skill to figure it out. ;-)

Having said all that, I recommend that you finish debugging this code (or switch to using RGBTRIPLE) for the practice. Once that's done, think about this. Instead of creating a copy of the entire image array, why not just have the code exchange values from left to right as it walks across each line?

If this answers your question, please click on the check mark to accept. Let's keep up on forum maintenance. ;-)

1
  • Thank you for your answer. I changed the else if condition to (j >= round(width(2.0)) in order to leave the "center" pixels "untouched" as you might have pointed out! hehe Thank you. Also could you explain a little bit more "exchange values from left to right as it walks across each line?" I might have read that quickly and I can't just figure out what you mean Aug 31 '20 at 16:38

You must log in to answer this question.

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