| CSC 161 | Grinnell College | Fall, 2011 |
| Imperative Problem Solving and Data Structures | ||
The goal of this lab is to introduce loops in C programming to students and increase familiarity with different types of loops.
In this lab, you will gain practice with several types of loops. Before
beginning the lab, move to the directory you created for labs and copy the
file loops.c
to your directory and open the file in emacs. You may wish to comment out
each loop after the exercise, but that is up to you.
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.
Hint: use a for loop.
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
loop that beeps once at 500 Hz, then increases by 20 Hz every beep for
twelve beeps.
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.
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 true. What
happens?
Hint: The key combination ctrl-c ends a program in the terminal. Turning the Scribbler 2 off or pressing the reset butt resets the robot.
do ... while
and while loops
Change the loop from the previous exercise, and make the continue
condition false. What happens?
Try writing a do ... while loop that has the continue
condition false. What happens? Is this different from
the while loop?
Write two sentences for each type of loop, describing the advantages and disadvantages of each type of loop.
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.
quarts.c),
that converted quarts to liters. Write a program
that prints a table listing the conversions from one to twelve quarts
into liters.
Hint: 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
When you have finished this lab, be sure to fill out its evaluation form in the "Lab Evaluation" section for CSC 161 on Pioneer Web
This document is available on the World Wide Web as
http://www.walker.cs.grinnell.edu/courses/161.fa11/modules/module-cond-loops-testing/loops-lab.shtml
|
created July 20 2011 by April O'Neill last full revision July 29 2011 by April O'Neill minor editing 24 August 2011 by Henry M. Walker |
|
| For more information, please contact Henry M. Walker at walker@cs.grinnell.edu. |