// Reflect image horizontally

void reflect(int height, int width, RGBTRIPLE image[height][width])

{ int t[3]; // using temporary array to swap values

for(int i=0;i<height-1;i++) // loop thru rows top to bottom

{ int n=width-1;

for(int j=0;j<n;j++) // loop thru columns left to right // alternate: j<round(n/2) so ij would swap from left to middle ik from right to middle so the don't swap twice and get the initial position again


for(int k=n;k==0;k--)// loop thru columns right to left // alternate: k<round(n/2)


{ // swapping, rgbt for particular colors from left pixel to temp , then right pixel to left, temp to right pixel



image[i][j].rgbtRed= image[i][k].rgbtRed;

image[i][j].rgbtGreen= image[i][k].rgbtGreen;

image[i][j].rgbtBlue= image[i][k].rgbtBlue;








  • using n-j(n being width-1) works but if I initialize k to do the same it doesnt work. why is it that way? could anyone point my error ``` { image[i][j].rgbtRed= image[i][n-j].rgbtRed; image[i][j].rgbtGreen= image[i][n-j].rgbtGreen; image[i][j].rgbtBlue= image[i][n-j].rgbtBlue; ``` – Srushti Kanade May 2 '20 at 3:56

You only need two loops to go through all the pixels of the image. One for every row, and one for the horizontal switching. So think about this: How many switchings do you need to do for every row to flip an image horizontaly?

  • I saw in your other post that you got it running congrat!!. – Tritum May 2 '20 at 17:28
  • Hi, thank you. I did figure it out later but this analogy of horizontal reflect made the explanation plausible,that helped. You articulated it in a better way. – Srushti Kanade May 12 '20 at 11:25

You must log in to answer this question.

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