CSC 115.005/006 Sonoma State University Spring 2022
Scribbler 2
CSC 115.005/006:
Programming I
Scribbler 2
Instructor: Henry M. Walker

Lecturer, Sonoma State University
Professor Emeritus of Computer Science and Mathematics, Grinnell College


Course Home References Course Details: Syllabus, Schedule, Deadlines, Topic organization MyroC Documentation Project Scope/
Acknowledgments

Notes:

Laboratory Exercise on Nested Loops

This laboratory exercise provides practice with several combinations of loops.


Work Started in Class

Printing a Table

  1. In the previous lab, you wrote a program that converted quarts to liters, producing a table.

    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 
    

    Extend this program to print a table that convers gallons and quarts to liters, yielding the following table:

    Table of gallon/quart to liter equivalents
                         quarts
    gallons     0       1       2       3
       0      0.000   0.946   1.893   2.839
       1      3.785   4.732   5.678   6.624
       2      7.571   8.517   9.463  10.410
       3     11.356  12.302  13.249  14.195
       4     15.141  16.088  17.034  17.980
       5     18.927  19.873  20.819  21.766
       6     22.712  23.658  24.605  25.551
       7     26.497  27.444  28.390  29.336
       8     30.283  31.229  32.175  33.122
       9     34.068  35.014  35.961  36.907
      10     37.853  38.800  39.746  40.692
      11     41.639  42.585  43.531  44.478
      12     45.424  46.370  47.317  48.263
    

    Interpreting this table, to compute the liter equivalent for 2 gallons 1 quart, one moves along the row for 2 gallons to the column for 1 quart to find the corresponding value (8.517).

  2. A Fahrenheit-Celsius Table: Program fahrenheit-celsius-errors.c is a program that is supposed to create a table of fahrenheit-celsius equivalents, as follows:

    table of fahrenheit and celsius equlvalents
                        fahrenheit tempheratures
    tens      0      1      2      3      4      5      6      7      8      9
      0    -17.8  -17.2  -16.7  -16.1  -15.6  -15.0  -14.4  -13.9  -13.3  -12.8
     10    -12.2  -11.7  -11.1  -10.6  -10.0   -9.4   -8.9   -8.3   -7.8   -7.2
     20     -6.7   -6.1   -5.6   -5.0   -4.4   -3.9   -3.3   -2.8   -2.2   -1.7
     30     -1.1   -0.6    0.0    0.6    1.1    1.7    2.2    2.8    3.3    3.9
     40      4.4    5.0    5.6    6.1    6.7    7.2    7.8    8.3    8.9    9.4
     50     10.0   10.6   11.1   11.7   12.2   12.8   13.3   13.9   14.4   15.0
     60     15.6   16.1   16.7   17.2   17.8   18.3   18.9   19.4   20.0   20.6
     70     21.1   21.7   22.2   22.8   23.3   23.9   24.4   25.0   25.6   26.1
     80     26.7   27.2   27.8   28.3   28.9   29.4   30.0   30.6   31.1   31.7
     90     32.2   32.8   33.3   33.9   34.4   35.0   35.6   36.1   36.7   37.2
    100     37.8   38.3   38.9   39.4   40.0   40.6   41.1   41.7   42.2   42.8
    

    However, the program contains four simple errors. Identify and correct these errors to obtain a program that prints the correct table.

Loops with a Scribbler 2 Robot

  1. Rising Pitch: (Review from the previous lab) 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 i:

    for (i = 0; i <= 12; i++)
    {
       int freq = /* compute frequency here */
       rBeep (1.0, freq);
    }
    
  2. Nested Loop: Modify the loop from Step 3, so that the program beeps i 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.

  3. More Beeping: 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.


Homework


  1. Counting Obstacles with Beeps: In the previous lab, you wrote a program that repeatedly moved forward until it detected an obstacle, then turned right. This sequence (moving forward and turning right) continued until the program was terminated.

    Modify this program so that the program beeps after each turn right. In particular,

    • After encountering the first obstacle, the robot beeps once.
    • After encountering the second obstacle, the robot beeps twice.
    • After encountering the third obstacle, the robot beesps three times.
    • Etc.

    Hint: The program likely will need a variable that keeps track of how many obstacles are detected. Then, after encountering an obstacle, the program likely will need a loop to beep the proper number of times.

  2. Drawing a "Box": Write a program that declares and initializes integer variables width and height. The program then should draw a box (outlined with asterisks) with the width and height specified by these variables.

    For example, if the program begins

          int width = 10;
          int height = 5;
        

    then the program should generate the following output

          **********
          *        *
          *        *
          *        *
          **********
        

    Programming Hints:

    • The horizontal line of asterisks might be produced by a loop with a printf statement that prints one "*" at a time.
    • The middle of the box might be considered a sequence of the proper number of boxes with spaces in the middle.
    • A line with spaces in the middle might be produced by printing an asterisk, a sequence of space characters (counting the proper number), and another asterisk.


created December 13 2021 by Henry M. Walker Valid HTML 4.01! Valid CSS!
For more information, please contact Henry M. Walker at walker@cs.grinnell.edu.