CSC 161 Grinnell College Spring, 2012
 
Imperative Problem Solving and Data Structures
 
 

Laboratory Exercise on Loops

Goals

The goal of this lab is to introduce loops in C programming to students and increase familiarity with different types of loops.

Exercises

In this lab, you will gain practice with several types of loops. Before beginning the lab, move to the directory you created for this lab, copy the file loops.c to your directory, and open the file in emacs. You may wish to comment out each exercise after you are done with it.

Simple Repetition

  1. One of the simplest uses of a loop is to perform an action a specific number of times. In loops.c, beep twelve times in a row, using only one beep command.

    • Here is an example of a for loop which iterates 10 times:

      int i;
      for (i = 0; i < 10; i++)
      {
       your code here;
      }

Rising Pitch Loop

  1. The purpose of a loop is to repeat a set of actions, and loops can modify actions in every iteration. In loops.c, write a for loop that beeps once at 500 Hz, then increases by 20 Hz every beep for twelve beeps.

  2. Now change this loop into a while loop.

    • Here is the for loop from before translated into a while loop:

      int i = 0;
      while (i < 10)
      {
       your code here;
       i++;
      }
    • How is this different from the for loop? What are some advantages and disadvantages of each?

  3. Now change this loop into the final type of loop, the do ... while loop. This should be of the following format:

    do
    {
     body of your loop here;
    } while (condition);
    • How is this different from the for and while loops? What are some advantages and disadvantages of each?

Nested Loops

  1. You've seen that loops can statically repeat an action, or change the action it performs once per loop. One way to combine both functions is to have nested loops; that is, execute an action in the one loop, and have the other loop modify the action performed. In loops.c, write a program that consecutively beeps more times in a row, until seven beeps in a row are reached. So, the robot would beep once and sleep for one second, then beep twice and sleep for one second, then three times and sleep for one second, and so on.

Infinite Loops

  1. One characteristic of loops is that there must be an "end" condition, or the loop will never stop. In loops.c, write a loop using while that beeps for one second every iteration, and make the continue condition 1 (true). What happens?

    Hint: The key combination ctrl-c ends a program in the terminal. Turning the Scribbler 2 off or pressing the reset button resets the robot.

Comparing do ... while and while loops

  1. Change the loop from the previous exercise by making the continue condition 0 (false). What happens?

  2. Try writing a do ... while loop that has the continue condition 0. What happens? Is this different from the while loop?

Reflecting on Loops

  1. Write two sentences for each of the three types of loops, describing the advantages and disadvantages of each type of loop.

Loops in Songs

  1. In a new file, write a program that plays a song (or use one of the songs from the last module), and write a loop that plays the song three times in a row.

Drawing a Table

  1. In Module 0, you saw a program (quarts.c), that converted quarts to liters. Write a program that prints a table listing the conversions from one to twelve quarts into liters.

    • Use the line printf("%4d%16.4lf\n", quarts, liters); to keep proper spacing.

    Example Output:

    Table of quart and liter equivalents
    Quarts           Liters
       1             0.9463
       2             1.8927 
       3             2.8390 
       4             3.7853 
       5             4.7317 
       6             5.6780 
       7             6.6243 
       8             7.5707 
       9             8.5170 
      10             9.4633 
      11            10.4097 
      12            11.3560 
    

For those with extra time

The Family Problem

  1. Write a program which determines how many children, on average, a family will need to have before they have at least one girl and at least one boy. Make it simulate a reasonably large number of families so you can get a good average. Also, keep track of the minimum number of children for a couple and the maximum. You can assume that the probability for a family having a boy or girl is 50%.

Feedback Welcome

Development of laboratory exercises is an interative process. Prof. Walker welcomes your feedback! Feel free to talk to him during class or stop by his office.