training

Code I wrote during training
git clone git://git.bitsmanent.org/training
Log | Files | Refs | README

c2f2c_2.c (1225B)


      1 /* Exercise 5.24 */
      2 
      3 #include <stdio.h>
      4 
      5 double celsius(double);
      6 double fahrenheit(double);
      7 
      8 int main()
      9 {
     10    double i, j;
     11    int line = 9; /* number of entries for line */
     12 
     13    /* print 1st table */
     14    printf("\t\t\tFrom °C to °F (1-100)\n\n");
     15    for(i = 0.0; i < 100 ; i += line) {
     16       printf("°C:\t");
     17       for(j = i; j < i + line && j <= 100; j++)
     18 	 printf("%.1f\t", j);
     19 
     20       printf("\n°F:\t");
     21       for(j = i; j < i + line && j <= 100; j++) {
     22 	 printf("%.1f\t", fahrenheit(j));
     23       }
     24       printf("\n");
     25    }
     26 
     27    /* print 2nd table */
     28    printf("\n\t\t\tFrom °F to °C (32-212)\n\n");
     29    for(i = 32; i <= 212; i += line) {
     30       printf("°F:\t");
     31       for(j = i; j < i + line && j <= 212; j++)
     32 	 printf("%.1f\t", j);
     33 
     34       printf("\n°C:\t");
     35       for(j = i; j < i + line && j <= 212; j++)
     36 	 printf("%.1f\t", celsius(j));
     37 
     38       printf("\n");
     39    }
     40 
     41    return 0;
     42 } /* E0F main */
     43 
     44 /* convert from Celsius to Fahrenheit */
     45 double celsius(double fahrenheit)
     46 {
     47    /* °C=(5/9)x(°F-32) */
     48    return (5.0 / 9.0) * (fahrenheit - 32);
     49 } /* eof celsius() */
     50 
     51 /* convert from Fahrenheit to Celsius */
     52 double fahrenheit(double celsius)
     53 {
     54    /* °F=(9/5)°C+32 */
     55    return (9.0 / 5.0) * celsius + 32;
     56 } /* eof fahrenheit() */
     57