title: How to rotate an image 90 degrees in C
body: <p>I've been trying to turn an image black and white, and then rotate it 90 degrees in C but i'm fairly new to programming, this is what i have to far. </p>
  
  <pre><code>#include&lt;stdio.h&gt;
  #include&lt;string.h&gt;
  
  int main () {
  FILE* first;
  FILE* second;
  FILE* third;
  int counter;
  char c;
  int width, height, pixelmax, average;
  int pixelred, pixelgreen, pixelblue, black[300][300][3];
  int i, j, timer=0;
  int k, f=0;
  first=fopen("blackbuck.ppm","r");
  second=fopen("blacktrout.ppm","w");
  </code></pre>
  
  <p>this skips the first few lines of code</p>
  
  <pre><code>for(counter=1;counter&lt;3;counter++){
  do{
      c=getc(first);
  }while(c != '\n');
      }
  fscanf(first,"%d%d", &amp;width,&amp;height);
  fscanf(first,"%d", &amp;pixelmax);
  </code></pre>
  
  <p>in this part of the program, i turn the pixels to black and white by taking their average, this is the easy part.</p>
  
  <pre><code>for(i=0, j=0; i&lt;width;i++, timer++){
  fscanf(first,"%d%d%d",&amp;pixelred,&amp;pixelgreen,&amp;pixelblue);
  average=(pixelred+pixelgreen+pixelblue)/3;
  black[i][j][0]=average;
  black[i][j][1]=average;
  black[i][j][2]=average;
  fprintf(second,"%d %d %d\n",black[i][j][0],black[i][j][1],black[i][j][2]);
  
  if (i==(width-1)&amp;&amp; j&lt;height){
  i=0;
  j++;
  }
  }
  fclose(first);
  fclose(second);
  third=fopen("blackflip.ppm","w");
  </code></pre>
  
  <p>This is the part where i am completely lost, i have no idea how to rotate the pixels stores in my 3d array 90 degrees. Any help please? A well explained explanation would go a long way for a newbie programmer like myself. thanks!</p>
  
  <pre><code>for(...???)
  }
  }
  
  
  return 0;
  </code></pre>