Laboratory Exercise to Introduce Program Organization
This lab provides preliminary practice with a reasonably structured framework that utlizes include files and simple procedures. The project for this module will provide more extensive practice within a more creative context.
Basic tasks
For this lab, consider the following tasks:
-
Tune_forward: A sequence of beeps, involving at least 6 notes, each of which has a specific frequency and duration. That is, tune_forward will be a sequence of at least 6 rBeep commands.
-
Tune_backward: The same sequence of rBeep commands used for tune_forward, except in the reverse order.
-
Move_clockwise: A sequence of Scribbler 2 movements, using a combination of at least 3 rForward and at least 3 rTurnRight commands.
-
Move_counter-clockwise: The same sequence of movement commands used for move_clockwise, except that each rTurnRight command is replaced by the corresponding rTurnLeft command.
Work Started in Class
Notes on these tasks
-
Although some tasks are related, each is largely independent from the others.
-
Each task can be described clearly and concisely.
-
One can think of each task conceptually at a high level, or one can delve into the details behind each task.
-
Copy scale-notes.h to your program directory.
-
Write a program procedure-practice-1.c with these features:
- The program includes MyroC.h, eSpeakPackage.h and scale-notes.h.
- The program contains stubs for separate procedures (tune_forward, tune_backward, move_clockwise, and move_counter_clockwise) for the basic tasks identified at the start of this lab.
- The main program should call each task procedure at least once and at least two task procedures twice.
In writing the task procedures, be sure to write the header comments before filling in the details.
In writing the main, write the comments first to outline the work to be done. Then add the procedure calls.
-
Add details to the task procedures in procedure-practice-1.c, one procedure at a time — illustrating the problem-solving approach of iterative program development.
Homework
Placing details for a task within a procedure supports two levels of abstraction (the high-level view and the details) Further levels of abstraction are possible, when one procedure itself utilizes more detailed procedures.
-
Define procedures tune_forw_move_clock, tune_forw_move_counter, tune_back_move_clock, and tune_back_move_counter. Each procedure:
-
calls one previously-defined tune procedure,
-
calls one previously-define movement procedure, and
-
uses a printf procedure to print in a terminal window the high-level description of the work being done.
Possible code for tune_forw_move_clock might be
/* procedure that plays the tune forward and then moves the robot in a counter-clockwise pattern */ void tune_forw_move_clock () { printf ("tune forward and move clockwise\n"); tune_forward (); move_clockwise (); }
Be sure each procedure has descriptive comments!
-
-
Write a main program procedure-practice-2.c that calls various combinations of all of these procedures. Of course, the overall outline of the work should be included within comments before the procedure calls are inserted!
Although these tasks and procedures are reasonably elementary, be sure to follow the approaches described in reading for today's lab, including
-
placing a high-level outline within comments,
- writing stubs for procedures at an early stage of program development,
- writing code details after the high-level comments are completed,
- adding details for one procedure at a time — not all at once.
- writing stubs for procedures at an early stage of program development,