Reading on Nested Loops
This reading discusses approaches to problem solving that involve multiple loops. In particular, many applications can be tackled by placing one loop inside another—a programing control structure called a nested loop.
Printing a Feet/Inches to Centimers Table
Consider the task of printing a table to convert feet and inches to centimeters, using the conversion 1 inch = 2.54 centimeters. In particular, consider writing a program that will print the following table:
Table of feet and inches to centimeter equivalents inches feet 0 1 2 3 4 5 6 7 8 9 10 11 0 0.0 2.5 5.1 7.6 10.2 12.7 15.2 17.8 20.3 22.9 25.4 27.9 1 30.5 33.0 35.6 38.1 40.6 43.2 45.7 48.3 50.8 53.3 55.9 58.4 2 61.0 63.5 66.0 68.6 71.1 73.7 76.2 78.7 81.3 83.8 86.4 88.9 3 91.4 94.0 96.5 99.1 101.6 104.1 106.7 109.2 111.8 114.3 116.8 119.4 4 121.9 124.5 127.0 129.5 132.1 134.6 137.2 139.7 142.2 144.8 147.3 149.9 5 152.4 154.9 157.5 160.0 162.6 165.1 167.6 170.2 172.7 175.3 177.8 180.3 6 182.9 185.4 188.0 190.5 193.0 195.6 198.1 200.7 203.2 205.7 208.3 210.8 7 213.4 215.9 218.4 221.0 223.5 226.1 228.6 231.1 233.7 236.2 238.8 241.3 8 243.8 246.4 248.9 251.5 254.0 256.5 259.1 261.6 264.2 266.7 269.2 271.8 9 274.3 276.9 279.4 281.9 284.5 287.0 289.6 292.1 294.6 297.2 299.7 302.3 10 304.8 307.3 309.9 312.4 315.0 317.5 320.0 322.6 325.1 327.7 330.2 332.7
To interpret this output, note that 8 feet, 4 inches corresponds to 100 inches, and this translates to 254.0 centimeters. (To find this table entry, first find the row for 8 feet. Then move across the row to the column for 4 inches. This row/column entry shows 254.0 centimeters.)
Printing the Lines of the header
Printing the first two lines of the header can be accomplished directly, using two printf statements:
// print table header printf ("Table of feet and inches to centimeter equivalents\n"); printf (" inches\n");
A similar approach could be used for the header containing the columns "feet 0 1 ...". However, getting spacing even and correct can be tedious and error prone. A better approach involves several short steps:
- Print the word "feet" with one printf statement. Omitting "\n" in the format string tells tne computer NOT to go to a new line in what follows, but rather to continue on the same line with the next printing.
- Use a loop to print the integers 0 through 11, again omitting "\n" in the format statement.
- Print a new line character "\n" to complete the output line.
The complete code for this line of the header is:
printf ("feet"); for (inch = 0; inch <= 11; inch++) { printf ("%8d", inch); } printf ("\n");
Printing the Body of the Table
To print the body of the table, we use the table's format to guide the construction of our code. There are at least two approaches,
Approach 1: Bottum-up (or details first)
A bottum-up approach first focuses upon a typical line of the output, such as:
4 121.9 124.5 127.0 129.5 132.1 134.6 137.2 139.7 142.2 144.8 147.3 149.9
In examining this line,
- all work is related to a number of feet, so we will need a variable (e.g., ft). Before starting the processing of this line, we will assume ft has been assigned the relevant value for the line.
- with ft known, the first part of the output displays this value, so a printf statement is needed (again with no "\n" in the format'
-
successive numbers on this line depend on values of inches between 0 and 11,
suggesting a loop for inches.
- within this loop, the program will need to compute the number of centimeters for the given number of feet and inches.
- the program will need to print the number of centimeters (not moving to a new line).
- print "\n" when the line is completed
Together, this discussion yields the following code:
ft = 4; // inserted as a typical row value. printf ("%3d ", ft); for (inch = 0; inch <= 11; inch++) { centi = (12 * ft + inch) * 2.54; printf ("%8.1lf", centi); } printf ("\n");
Note with a value of gat specified , this code could be typed, compiled, run, and checked for correctness.
Once the code for a single line works properly, we need to repeat that code for each row, so the number of feet can iterate over 0, 1, 2, ... . This approach yields the following code for the entire body of the table:
// print body of table for (ft = 0; ft <= 10; ft++) { printf ("%3d ", ft); for (inch = 0; inch <= 11; inch++) { centi = (12 * ft + inch) * 2.54; printf ("%8.1lf", centi); } printf ("\n"); }
Approach 2: Top-down (or high-level structure first)
A second approach for creating the overall table first looks at the overall structure. From this perspective, the body of the table focuses on the number of feet:
1 2 3 4 5 6 7 8 9 10
Within this basic structure, lines contain additional information, but at a high level, the body of the table highlights feet. Thus, a slightly more complete view of the high level structure of the table body might be
1 some details 2 some details 3 some details 4 some details 5 some details 6 some details 7 some details 8 some details 9 some details 10 some details
Translating this framework to code yields the following code
for (ft = 0; ft <= 10; ft++) { printf ("%3d ", ft); printf ("some details"); printf ("\n"); }
As shown, this code segment will compile and run, and thus the overall structure of the tabel can be coded, compiled, and run (and debugged as needed).
With this high-level perspective completed, the next step is to replace "some details" with code to compute and print values related to 0, 1, 2, ..., 11 inches. Writing that code follows a similar process to what was discussed in Approach 1, and the resulting code in Aproach 2 is identical to the code in Approach 1.
A Short Reflection
As illustrated in the feet/inches/centimeters example, programs involving multiple loops can be developed in numerous ways. In any development, it can be helpful to focus on just a few elements at a time—trying to keep track of all program elements at once can be a daunting task, as well as being error prone!
To control the amount of complexity in program development, a good strategy often is either
- to focus on some details first and then place those details within a broader structure, or
- to identify the overall structure of a solution first and then add details.
As illustrated in the example, either approach can work well, and sometimes it is useful to experiment with one approach to a problem, and then to try the other approach if troubles arise.
created December 13 2021 by Henry M. Walker |
![]() ![]() |
For more information, please contact Henry M. Walker at walker@cs.grinnell.edu. |