1-3/4
Quest: page 13
- Modify the temperature conversion program to print a heading above the table.
 - Write a program to print the correspongind celsius to farh table (i did kelvin instead)
 
To accomplish this we need to understand the printf formatting:
- %c character
 - %d decimal (integer) number (base 10)
 - %e exponential floating-point number
 - %f floating-point number
 - %i integer (base 10)
 - %o octal number (base 8)
 - %s a string of characters
 - %u unsigned decimal (integer) number
 - %x number in hexadecimal (base 16)
 
examples:
#include <stdio.h>
#include <stdlib.h>
int main() {
  // print min width of 3 right justified
  printf("%3d\n", 0);
  printf("%3d\n", 10);
  printf("%3d\n", 100);
  printf("\n");
  // left justified
  printf("%-3d\n", 0);
  printf("%-3d\n", 10);
  printf("%-3d\n", 100);
  printf("\n");
  // printf zero fill options
  printf("%03d\n", 0);
  printf("%03d\n", 10);
  printf("%03d\n", 100);
  printf("\n");
  // At least five-wide, with a plus sign
  printf("%+5d\n", 10);
  // Five-wide, plus sign, left-justified
  printf("'%-+5d'", 10);
  printf("\n");
  // quick float example
  // Eight-wide, two after the decimal
  printf("'%8.2f'", 10.3456);
  printf("\n");
  // string
  printf("%s\n", "Hello");
  // 10 char string
  printf("%10s\n", "Hello");
  // 10 char left justified
  printf("%-10s\n", "Hello");
  printf("\n");
}
solution
To print the table. I will reserve the needed space to make the table left aligned.
note
stdlib is included by default. I included to have a ref for my code editor. EXIT_SUCCESS is defined in stdlib.h
definition example
/* We define these the same for all machines.
   Changes from this to the outside world should be done in `_exit'.  */
#define EXIT_FAILURE    1   /* Failing exit status.  */
#define EXIT_SUCCESS    0   /* Successful exit status.  */
print aligned table
#include <stdio.h>
#include <stdlib.h>
#define LOWER 0
#define UPPER 300
#define STEP 20
// fahr to celsius
double ftoc(int f) { return (5.0 / 9.0) * (f - 32); }
// kelvin to celsius
double ktoc(int k) { return k - 273.15; }
int main() {
  int fahr;
  printf("%-10s %-10s\n", "fahrenheit", "celsius");
  for (fahr = UPPER; LOWER <= fahr; fahr -= STEP) {
    printf("%-10d %-10.2f\n", fahr, ftoc(fahr));
  }
  for (int i = 0; i < 50; i++) {
    printf("=");
  }
  printf("\n");
  // kelvin heading
  printf("%-10s %-10s\n", "kelvin", "celsius");
  int kelvin;
  for (kelvin = LOWER; kelvin <= UPPER; kelvin += STEP) {
    printf("%-10d %-10.2f\n", kelvin, ktoc(kelvin));
  }
  return EXIT_SUCCESS;
}
printed table
fahrenheit celsius   
300        148.89    
280        137.78    
260        126.67    
240        115.56    
220        104.44    
200        93.33     
180        82.22     
160        71.11     
140        60.00     
120        48.89     
100        37.78     
80         26.67     
60         15.56     
40         4.44      
20         -6.67     
0          -17.78    
==================================================
kelvin     celsius   
0          -273.15   
20         -253.15   
40         -233.15   
60         -213.15   
80         -193.15   
100        -173.15   
120        -153.15   
140        -133.15   
160        -113.15   
180        -93.15    
200        -73.15    
220        -53.15    
240        -33.15    
260        -13.15    
280        6.85      
300        26.85