CPS 6: Lab #4

Summer 1999

Debugging and Graphics: Balloon Race

6 points

Due: Tuesday, June 8 - 11:59pm

Change into your "cps6" directory using the "cd" command and create another directory called "lab4" using the "mkdir" command. Change into the "lab4" directory. If you did this correctly, when you type "pwd" you should see a long path name that ends with "/cps6/lab4"

In order to do this lab, you need to copy some files using the following "cp" (for "copy") command (don't forget the trailing period, or dot):

       cp  ~dr/cps6/lab4/*  .

If you type "ls" to list the files now in your lab4 directory, you should see the following files: Makefile, arith2.cc, and race.cc.

You'll need to set up a path to an animator by performing the following steps:

  1. Go to your home directory by typing cd
  2. Type ls and see if there is a directory called bin. If you find it, skip to step 4.
  3. Type mkdir bin to create a bin directory.
  4. Type cd bin to got to your bin directory.
  5. Type ln -s /afs/acpub/project/cps/bin/samba
    This sets up a convenient way to execute an animator named "samba".
  6. Type ln -s ~dr/bin/submit6
    This sets up a more convenient way to execute the submit program.
  7. Type rehash
  8. Now go back to your lab4 directory to do the rest of the lab.

For each of the programming problems that follow, you should use the style rules discussed in class, which includes indentation, meaningful variable names and comments at the top of the file and for each function.

Use the "make" command to compile a C++ program. In order to use this command, there must be a file called "Makefile" in the same directory as the program. This file should already be in your lab4 directory. For example, to compile the program called race.cc type: make race

Problem 1: Arithmetic and Debugging

When your program compiles but doesn't work correctly, you can use a program called a debugger to help you locate the errors. We'll be using a debugger called ddd. The debugger will allow you to pause the execution of the program at certain points, allowing you to look at the current values of variables to see if they have the correct values.

NOTE: ddd requires a X-terminal. Unless you are a Unix Hacker, you should run it from the Unix Clusters.

Compile the program arith2.cc by typing: make arith2

This program does not work correctly. Do not modify the program yet. Run the program to see that it does not add and multiply correctly.

Now we'll run the program using the debugger; type the following command:

      ddd arith2 &

After arith2 is loaded in ddd, you will see two new windows:

To run your program in ddd, just press the run button in the lower left corner of the Program Window. In the Command Window, you will see the prompt to enter 2 integers. Enter the numbers and watch the output. You will get a message that the program exited normally, but you will notice the the output is flawed. We will now look at the major commands of ddd:

Break

Run

Display

Step & Next

Note: You may get error messages about not being able to find stream.cc files, this is okay. Just ignore them.

Setup for the TA:

  1. Set a breakpoint for the Mult function.
  2. Run the program.
  3. Display all of the variables in Mult. Remember the variable names are duplicated from Add, but they are different variables (think scope).

Show the TA:

  1. Step through each line of Mult with the TA.
  2. Explain what is happening to the TA.

Problem 3: Balloon Race

For this part of the lab, you'll simulate a hot air balloon race, and use the Samba animator to use graphics to view the race. You'll use the ColorBalloon class, which allows you to create balloons of different color, similar to the Balloon class in the book, but with graphical output instead of textual.

First let's compile the program race.cc and learn how to run it using the animator. Then you can start the modifications to race.cc. Currently, the program race.cc creates one balloon, and raises it to 40 meters.

Compiling race.cc and using the animator

You should have already copied this Makefile at the beginning of this lab. To compile this program, type:

make race
We will be using the Samba animator to display the balloons. To run your program, pipe all output into the animator by typing:
race | samba  
The "|" is on the same key as the "\" and is just above the "backspace" key. A Samba window and Polka Control Panel window should appear You may need to move the Samba window so you can see it, the Polka Control Panel window and the xterm window at the same time. The column in the center of the Polka Control Panel window is a speed control bar which is currently set on the highest speed. You may want to slow the animation down so you can see things move. Move the rectangle in this column to the left to slow the animation down. To start the animation, click on the start button. To quit at any time, click on the quit button.

IMPORTANT NOTE: All output is being sent to the animator. If you want to print anything to the screen (for debugging purposes or otherwise) you'll need to print the word comment at the beginning of each output line and put an endl at the end of each line of output. That is, each cout statement in your program should start and end with:

    cout << "comment " << .... << endl;

Problem 3: The assignment

The ColorBalloon class has the following public functions available to you:

class ColorBalloon
{
  public:
    ColorBalloon();                // constructor
    void Ascend(int);              // ascend so altitude >= parameter
    void Descend(int);             // descend so altitude <= parameter
    void Cruise(int, string);      // cruise for parameter time-steps
                                   // either "right" or "left"   
    int GetLocation();             // returns distance travelled
    int GetAltitude();             // returns altitude
    void Flash(int);               // flash on and off 
    bool PassedFinish();           // true if passed the FinishLine
        
  private:
    // not shown
};

Note the new functions: GetLocation returns the distance the balloon has traveled, GetAltitude returns the current altitude of the balloon, Flash will flash the balloon on and off for the specified number of time steps, and PassedFinish returns true if a balloon has passed the finish line. In addition, the function Cruise has been modified so you can indicate the direction to travel, "right" or "left". For example, wally.Cruise(5,"left") moves balloon wally 5 steps to the left.

Currently, the program race.cc creates one balloon, and raises it to 40 meters. Modify the program race.cc to do the following (but not all at once!!! Implement a step, then recompile and see that it is working. Only then go on to the next step.):

  1. Write at least one useful function that has a balloon as a parameter. See the note below about passing balloons to functions.
  2. Create two additional balloons and ascend these balloons to height 60 meters and 80 meters. There may be some windshear and the balloons may not reach the exact specified height, that's ok. The race is ready to begin.
  3. Repeat the following until one of the balloons has passed over the Finish line. Note: Windshear may bump the balloons up or down.
  4. The race is over. There is exactly one winner, the first balloon to cross over the finish line. Note that since this is a simulation and each balloon moves one at a time, there will be exactly one winner.
  5. Of the two losing balloons, first move the one with the lower altitude down to the ground, then move the losing balloon with the higher altitude down to the ground.
  6. The winner gets a victory lap. Move the winning balloon back to the start line.
  7. Flash the winning balloon 50 times, and then move it to the ground.

Make these modifications in the file race.cc. Once you've made the changes to race.cc, see the above section for compiling and running this program using the animator.

An Example Note that the movement of the balloons will be different each time you run this lab.

Note: Passing Balloons to Functions. If you want to pass a balloon to a function, you must add the symbol & in the declaration of the function and in any function prototypes, but it is not used when the function is called. For example, consider the function DoSomething shown below.

  int DoSomething(ColorBalloon & b, int num);

This function passes a ColorBalloon named fritz and an integer num. It might be called as shown below:

   ColorBalloon fritz;

   fritz.Cruise(6,"right");  // move fritz 6 steps to the right
   int x = DoSomething(fritz, 8);  

Submission of programs:

When your programs for problems 1 and 3 compile and produce the correct output, create a README file. This file should include your name, section number, the date, how long you worked on these programs, and anyone you received significant help from. You can then turn everything in by typing:

        submit6 lab4 README arith2.cc race.cc 

You should receive a message telling you that the programs were submitted correctly.