## 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

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 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 ```
1. In your first version of the program, implement the loop using a `for` construct.
2. In your second version of the program, implement the loop using a `while` construct.

## Loops with a Scribbler 2 Robot

1. Simple Motion Commands: Write one or more programs that have the Scribbler 2 robot move in various ways:
1. A `for` loop should move the scribbler forward 5 times.
2. A `while` loop should move the Scribbler in some direction for increasing amounts of time.
3. A `for` loop should move the Scribbler some number of times at changing speeds.
4. A `while` loop should change both speed and time in the same loop.
2. 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 `for` loop based on an integer variable `iter`:
``````for (iter = 0; iter <= 12; iter++)
{
int freq = /* compute frequency based on iter here */
rBeep (1.0, freq);
} // for iter``````
3. Nested Loop: Modify the loop from Step 3, so that the program beeps `iter` times 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

1. 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.)

## 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`, and `rBackward`. 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 `rMotors` and `rStop`. Once any of these commands is given, the robot will continue to follow that motion until a subsequent command is given.
1. 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);``````
1. 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)?
2. Include this code segment in a program, and observe what happens. How did your prediction agree or disagree with the resulting behavior?
3. Change the duration in `rForward` from `5.0` to `-5.0`; repeat parts a and b.
In your own words, describe what it means for a command to be blocking versus nonblocking.

## 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.

1. 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., `rTurnLeft` or `rTurnRight`), 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.

1. Write a simple program that moves the robot forward until it sees an obstacle.
2. 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.
3. 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.