0

Here is my code. I have overcomplicated it.

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

    int value[3];
   
    value[0]=image[i][j].rgbtBlue;
    value[1]=image[i][j].rgbtGreen;
    value[2]=image[i][j].rgbtRed;
    

    
    int b[3];
    b[0]=image[i][j- 1].rgbtBlue;
    b[1]=image[i][j- 1].rgbtGreen;
    b[2]=image[i][j - 1].rgbtRed;
    
     int temp[3];
         temp[0]=value[0];
         temp[1]=value[1];
         temp[2]=value[2];
         
         temp[0]=image[i][j].rgbtBlue;
         temp[1]= image[i][j].rgbtGreen;
         temp[2]= image[i][j].rgbtRed;
         
         value[0]=b[0];
         value[1]=b[1];
         value[2]=b[2];
         
         image[i][j].rgbtBlue =image[i][j- 1].rgbtBlue;
        image[i][j].rgbtGreen  = image[i][j- 1].rgbtGreen;
         image[i][j].rgbtRed = image[i][j- 1].rgbtRed;
    
     
      image[i][j].rgbtBlue=image[i][j - 1].rgbtBlue;
      image[i][j].rgbtGreen=image[i][j - 1].rgbtGreen;
      image[i][j].rgbtRed=image[i][j - 1].rgbtRed;

pls help. Thanks

0

You're right, it's way overcomplicated. It's using too many temp vars, and it assigns and immediately reassigns vars. That means that some of the code is dead code - code that never executes or is meaningless and can be removed without affecting execution. There's also code that serves no purpose, like unused variables that have been assigned values but whose values are never actually used. This is what it looks like when the dead code is removed:

for (int i=0;i<height -1; i++) 
{ 
    for (int j=0;j<width- 1;j++) 
    {
        image[i][j].rgbtBlue =image[i][j- 1].rgbtBlue;
        image[i][j].rgbtGreen  = image[i][j- 1].rgbtGreen;
        image[i][j].rgbtRed = image[i][j- 1].rgbtRed;
         
        image[i][j].rgbtBlue=image[i][j - 1].rgbtBlue;
        image[i][j].rgbtGreen=image[i][j - 1].rgbtGreen;
        image[i][j].rgbtRed=image[i][j - 1].rgbtRed;
    }
}

As you can see, all of the temp values served no purpose. When they're removed, we see that the code is merely moving the pixels 1 position to the left. This is not what is sought.

The code needs to swap the first with the last pixel, the second with the 2nd to last, etc. That should suggest a pattern to you.

Next, you need to understand how to swap two vars. Here's a demo of that process. It requires only one temp var. (for 3 elements in an array, you'd need 1 array of 3 elements.)

 tmp = a;
 a = b;
 b = temp;

That's all there is to it.

Finally, there's one major issue. Think about the for loop setup and what it means.

    for (int j=0;j<width- 1;j++) 

When n=0, it would swap the first pixel with the last. n=1 should swap the 2nd with the second to last, and so on. But what happens when n gets to the midpoint? At this point, all the pixels in the first half have been swapped with all the pixels in the second half. If the loop keeps going from here, then the swapping continues until the last pixel is swapped again with the first one. In other words, everything gets swapped again. returning everything back to the way it was when the loop started!

You need to think carefully about where to end the width loop. ;-)

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

5
  • Thanks a lot. After that I made a lot of changes. I have figured out that it should be width/2. Also I have used swap function to swap the values. Lets see if it works – Cooldude Nov 6 '20 at 8:28
  • I have modified the code to this – Cooldude Nov 6 '20 at 17:16
  • void reflect(int height, int width, RGBTRIPLE image[height][width]) { for (int i=0;i<height; i++) { for (int j=0;j<width/2;j++) { RGBTRIPLE ini=image[i][j]; RGBTRIPLE temp=a; RGBTRIPLE final=image[i][width -(j+ 1)]; ini=final; final=temp; – Cooldude Nov 6 '20 at 17:17
  • now what is the error? – Cooldude Nov 6 '20 at 17:17
  • new problem - new question. – Cliff B Nov 6 '20 at 20:19

You must log in to answer this question.

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