/* Problem 4--The Temperature Table
   This one was pretty straightforward.  You just print out the
   Fahrenheit temperatures, with the Centigrade ones thrown in when
   appropriate. */

#include <stdio.h>
#include <stdlib.h>
#include <math.h>

int main (int argc, char **argv);

FILE *in, *out;

int main (int argc, char **argv) {

  int cs=0, l, h, f;
  double c, newf;

 in = fopen ("prob4.in","r");
 out = fopen ("prob4.out","w");
 while (fscanf(in,"%d %d",&l,&h),l<h) {
  fprintf (out,"Table %d: %d to %d degrees Fahrenheit\n",++cs,l,h);
  fprintf (out,"Fahrenheit Centigrade\n");
  fprintf (out,"---------- ----------\n");
  for (f=l;f<=h;f++) {
   fprintf (out,"%10d",f); /* print Fahrenheit */
   c = 5.0/9*(f-32); /* If C and F exactly match, print C. */
   if (c-floor(c)< 1e-6) fprintf (out,"%11d\n",(int)c);
   else {
    newf = 9.0/5*ceil(c)+32; /* if C is between adjacent F's print on */
    if (f+1-newf > 1e-6 && f < h) /* next line. */
     fprintf (out,"\n%21d\n",(int)ceil(c));
    else
     fprintf (out,"\n");
   }
  }
  fprintf (out,"\n");
 }
 fclose (in);
 fclose (out);
 return EXIT_SUCCESS;
}
