Loops
The goal of this lab is to introduce loops in C programming to students and increase familiarity with different types of loops.
Printing a Table
-
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 statement
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
-
In your first version of the program, implement the loop
using a
forconstruct. -
In your second version of the program, implement the loop
using a
whileconstruct.
-
In your first version of the program, implement the loop
using a
Loops with a Scribbler 2 Robot
-
Simple Motion Commands: Write one or more programs that
have the Scribbler 2 robot move in various ways:
-
A
forloop should move the scribbler forward 5 times. -
A
whileloop should move the Scribbler in some direction for increasing amounts of time. -
A
forloop should move the Scribbler some number of times at changing speeds. -
A
whileloop should change both speed and time in the same loop.
-
A
-
Rising Pitch: A program is supposed to beep once at 800
Hz, then increase by 20 Hz every beep for another twelve beeps. Write this
program using the following template for a
forloop based on an integer variableiter:for (int iter = 0; iter <= 12; iter++) { int freq = /* compute frequency based on iter here */ rBeep (1.0, freq); } // for iter -
Nested Loop: Modify the loop from Step 3, so that the
program beeps
itertimes at the given frequency, rather than just once, inside the loop. Thus, the resulting program should beep once at 800 Hz, then twice at 820 Hz, then three times at 840 Hz, etc.
Step 4 illustrates how loops may be nested inside one another. In this case, the outside loop systematically works through a sequence of main steps. The inside loop then supplies the processing for each particular element of the main sequence.
Another Nested Loop
-
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.
Note that the C standard library includes a function called
sleep. It is featured in chapter 3 of the manual. That means you can access information about it by typingman 3 sleep
in the terminal. You can use the ↑ and ↓ arrows to move up and down line by line, space to move from one page to the next, and finally type q to exit (quit) the man page.
Blocking and Nonblocking Commands
The reading on Scribbler 2 motion provides numerous details regarding motion commands. The Scribbler 2 movement commands may be organized into two basic groups:
- Limited Duration Commands
-
A duration parameter is part of the
commands
rTurnLeft,rTurnRight,rForward, andrBackward. For each of these commands, the robot follows the movement for a specified amount of time and then stops or in the non-blocking situation the movement will keep going until another motion command is given. - Ongoing Duration Commands:
-
No duration parameter is given for the
commands
rMotorsandrStop. Once any of these commands is given, the robot will continue to follow that motion until a subsequent command is given.
-
Consider the following code segment which includes a movement command and
the sounding of three notes.
rForward (1.0, 5.0); rBeep (1.0, 880); rBeep (1.0, 1280); rBeep (1.0, 1760);- What do you predict will happen with this code? When the motion starts, do the beeps sound as the robot moves (nonblocking movement), or do the beeps sound after the robot movement has finished (blocking movement)?
- Include this code segment in a program, and observe what happens. How did your prediction agree or disagree with the resulting behavior?
-
Change the duration in
rForwardfrom5.0to-5.0; repeat parts a and b.
Spiral Motion
Consider how to make the robot move spirally. Spirals begin from a center point, with the line moving in a circular motion, with a gradually increasing distance from the origin. There are two straightforward ways that move the Scribbler 2 in a spiral motion for at least ten seconds.
-
Write a program that makes the Scribbler 2 robot behave like
turtles in CSC 151. So, the robot would move forward, then turn
(e.g.,
rTurnLeftorrTurnRight), then move forward again, but a little further, then turn, and so on in a spiral shape. Just a single loop is needed here.
Motion With Obstacles
While blind motion can be interesting, sensing obstacles is where motion becomes more compelling.
- Write a simple program that moves the robot forward until it sees an obstacle.
- Now modify your program so that it moves the robot forward until it sees an obstacle, then turns right, then moves forward again until it sees an obstacle.
- Finally, generalize your program so that your robot moves forward until it sees an obstacle, then turns right, moves forward until it sees an obstacle, turns right, moves forward until obstacle, turns right, etc. It should continue to do this until the program is terminated.
