K\&R C Exercise 1-5: Reverse Temperature Table (300 to 0)

Exercise 1-5. Modify the temperature conversion program to print the table in reverse order, that is, from 300 degrees to 0.

Approach

The original K&R temperature program counts up: it starts fahr at 0, adds the step each iteration, and stops when fahr > 300. Reversing the table means changing exactly three things: (1) start fahr at upper (300) instead of lower, (2) subtract the step instead of adding it, and (3) flip the loop condition from fahr <= upper to fahr >= lower. Those three choices — initial value, step direction, and comparison — fully determine which way a loop counts. Get any one of them wrong and the loop either runs forever or never executes at all. This exercise trains you to think about all three together.

Solution

/* K&R Exercise 1-5: print Fahrenheit-Celsius table in reverse order
   Compile: gcc -ansi -Wall ex1-5.c -o ex1-5 */

#include <stdio.h>

int main(void)
{
    float fahr, celsius;
    int lower, upper, step;

    lower = 0;
    upper = 300;
    step  = 20;

    printf("Fahr\tCelsius\n");
    fahr = upper;                        /* start at 300, not 0 */
    while (fahr >= lower) {             /* stop when we pass below 0 */
        celsius = (5.0 / 9.0) * (fahr - 32.0);
        printf("%3.0f\t%6.1f\n", fahr, celsius);
        fahr = fahr - step;             /* count down, not up */
    }
    return 0;
}

The body of the loop is identical to the original program — only the three loop-control lines changed. This is a good reminder that the formula for conversion has nothing to do with the order we visit temperatures.

Compile and Run

gcc -ansi -Wall ex1-5.c -o ex1-5
./ex1-5

Sample Output

Fahr    Celsius
300     148.9
280     137.8
260     126.7
240     115.6
220     104.4
200      93.3
180      82.2
160      71.1
140      60.0
120      48.9
100      37.8
 80      26.7
 60      15.6
 40       4.4
 20      -6.7
  0     -17.8

Bonus: For-Loop Version

Once you reach Section 3.5 of K&R, you will meet the for loop, which puts all three loop-control pieces — initialisation, condition, and update — on a single line. The same reverse table written as a for loop is noticeably more concise:

/* Bonus: for-loop version of Exercise 1-5
   Compile: gcc -ansi -Wall ex1-5-for.c -o ex1-5-for */

#include <stdio.h>

int main(void)
{
    int fahr;

    printf("Fahr\tCelsius\n");
    for (fahr = 300; fahr >= 0; fahr = fahr - 20)
        printf("%3d\t%6.1f\n", fahr, (5.0 / 9.0) * (fahr - 32));

    return 0;
}

The output is identical. The for loop is preferred in practice because a reader can see at a glance that this counts from 300 down to 0 in steps of 20, without having to scan the loop body for the update. K&R use this for-loop form as the idiomatic temperature-table program from Chapter 1.3 onwards.

What This Exercise Teaches

  • Loop direction is a choice of three things — initial value, step sign, and comparison operator. Changing any one without the others produces a broken loop.
  • Counting down with a while loop — subtract the step and use >= instead of <=; the pattern is symmetric with counting up.
  • Separating computation from iteration order — the Fahrenheit-to-Celsius formula does not care whether we visit temperatures in ascending or descending order; only the loop machinery changes.
  • The for loop as syntactic sugar — both versions produce identical machine code; the for form is preferred because it co-locates all three loop-control expressions.

Set Up Your C Environment

To compile and run this solution you need GCC. If you have not set up C development on your machine yet, these guides walk you through it step by step:

← Exercise 1-4  | 
Chapter 1 Solutions  | 
Exercise 1-6 →

Book:

The C Programming Language, 2nd Ed — Kernighan & Ritchie

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>