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 Arrays

Goals

This lab provides practice with arrays as data structures and as parameters to functions.

Work Started in Class

An array is used to arrange data in memory. A real-life example to this kind of data structure lockers in a locker room or storage rooms within a long-term storage facility. There are boxes next to each with limited amounts of space in each. The following exercises illustrate the use of arrays within C.

Array Indices

  1. Program max-min.c is discussed in today's reading on arrays. The program computes the maximum, minimum, and average of an array of numbers and also prints values in main memory in or near an array.

    1. Copy the program max-min.c and compile and run it. Provide your own explanation of what the program does.
    2. Change #define n 10 to #define n 12, but do not change any other parts of the program. Compile and run the program, and explain what happens.
    3. Change #define n 10 to #define n 7, but do not change any other parts of the program. Again, try to compile and run the program, and explain what happens.

Consider the following declaration within the C program array-scale.c

  int notes[8] = {523, 587, 659, 698, 783, 880, 987, 1048};

C uses this declaration in at least two ways:

To clarify, the variable notes identifies the start of the the array. Thus, notes[0] specifies the first value in the block of memory, notes[1] specifies the second value in the block of memory, etc. This interpretation of the array has two consequences when using functions.

  1. Copy the program, array-scale.c to your account.

    1. Write a paragraph explaining what this program does.
    2. Near the end of the program, the index variable is set to 5. What happens if this index value is set to an inappropriate value? You may want to try reasonably small errors (e.g., index = 8, or index = -1) as well as ridiculous indices (e.g., index = 123456789). Explain the results that you get.

Multiple Arrays

  1. Program, array-move.c, makes the Scribbler move at specified speeds for certain times, using one array for speed and a second array for time.

    1. Explain how this program is similar to and different from the program array-scale.c from the previous question.
    2. Change the loop in the program so that it counts down, instead of up.
    3. Try changing the initialization numMoves, which tells the number of moves(it is now 8) to different numbers and see what happens.
      • Try a number smaller than 8.
      • Try a number greater than 8.

A Robot Journey

  1. One might describe a journey by the robot as having a series of steps. For each step,

    • the robot moves forward at a specified speed for a certain time
    • the robot turns at a given speed for a specified time

    As you know, speeds for the Scribbler 2 are numbers between -1.0 and 1.0 (or between 0.0 and 1.0, if the robot only moves forward). Also, speed parameters for either rTurnRight or rTurnLeft are between -1.0 and 1.0.

    Set up an appropriate set of arrays to guide the robot through a journey of at least 10 steps. (The main loop should go through array values, first calling rForward and then one of the turn commands.) (See the MyroC.h header for details of these robot commands.)


Taking Pictures

The following notes outline the steps needed to utilize the Scribbler 2 to take a picture.



Here,


Example program photographer.c illustrates the difference of how blocking and non-blocking commands work.

  1. photographer.c will take a picture, turn then show it to you, then repeat this twice more.

    1. First uncomment the first commented out part , then run the program and observe.
    2. Next, comment back out rDisplayPicture part and uncomment the second part of the code.

    What is the difference of the functions for these two commands?


Making a Reverse Movie

You can make an array of Pictures just like any other variable type. This is because Picture is a "type" just like double and int, but it is for storing Scribbler pictures.

  1. Write a program that tells the Scribbler to take and store 6 photos by turning in a circle, and showing them in reverse order. Taking the pictures and showing them should be done with two separate loops.


Arrays and Functions

  1. Program max-array.c computes the maximum of an array of numbers using a separate function. Copy this program and be sure you know how they work.

    1. Initialization in find_max uses the statement max = arr[0]; . Why do you think this is done, rather than setting max to a large number (e.g., 1500) and starting the loop index at 0 rather than 1?
    2. Using max-array.c as an example, add separate functions, find_min and find_avg, that compute the minimum and the average of the values in an array.
    3. Write an additional procedure find_max_min_avg that computes the maximum, minimum, and average values within one function. Since multiple values are to be computed and used by the main program, this function must pass values back using reference parameters. The procedure signature should be:
        void find_max_min_avg (double arr [], int sizeOfArray,
                               double * max, double * min, double * avg);
      

Homework

Robot Hoedown

  1. In program max-array.c from Step 7, modify find_max so that it adds 3 to each array element after computing the maximum. That is, the loop in find_max should be changed to

      for (j = 1; j < sizeOfArray; j++)
           { if (arr[j] > max)
               max = arr[j];
             arr[j] += 3.0;
           }
    

    Explain what happens when the array is printed in the main program and why.

  2. Write a function, swings, with the following signature:

    void swing (double speeds[], double durations[], int sizeOfArrays)
          

    This function should have the following properties:

    • sizeOfArrays identifies the number of elements stored in the speeds array and in the durations array.

    • For each speeds[i], durations[i] pair, the Scribbler should turn with the associated speed and duration

    • If the current index of the command is an even number, it should turn right

    • If the current index of the command is an odd number, it should turn left

    • Hint: Consider using the % operator to determine whether the index is even or odd.

    Be sure to test your function for correctness. It is suggested that you use at least four different durations and speeds.

  3. Copy the following function into your program, and make sure you understand what it does:

    void divide_swings (double times[], int args)
    {
     int i;
                      
     for (i = 0; i < args; i++)
      times[i] /= 3;
    } // divide_swings
          
    1. Predict what will happen if you make the following calls in your main method, in this order:

      swing (speeds, times, num_moves);
      divide_swings (times, num_moves);
      sleep (3);
      swing (speeds, times, num_moves);
            
    2. Now test it out and explain what happened and why this is possible.



created 19 July 2011 by Dilan Ustek
revised 18 September 2011 by Henry Walker
revised 15 October 2011 by Dilan Ustek
modest editing 16 October 2011 by Henry Walker
minor editing 22 October 2013 by Henry Walker
reorganized with module 26 January 204 by Henry Walker
readings added 19 September 2014 by Henry M. Walker
reformatted, expanded, and connected to new readings 9 August 2016 by Henry M. Walker
Valid HTML 4.01! Valid CSS!
For more information, please contact Henry M. Walker at walker@cs.grinnell.edu.