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:
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
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:
gdb. You can run gdb by
itself but ddd provides a much nicer interface.
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
arith2.cc:13 in the little box in the
lower left corner of the Program Window.
Break()
button. This will place a little Stop Sign to the left of
the line numbers. This tells the debugger to break (stop
running) at this point of the program.
Run
Run button.
Display
Display().
Step & Next
Step button. Step will step
into functions; that is show you a line by line run
of the function. This can be useful, but is often
unnecessary.
Next moves to the next line in the
current function. It steps over functions and
doesn't show a line by line run of them.
Note: You may get error messages about not being able to find stream.cc files, this is okay. Just ignore them.
Mult function.
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.
You should have already copied this Makefile at the beginning of this lab. To compile this program, type:
make raceWe will be using the Samba animator to display the balloons. To run your program, pipe all output into the animator by typing:
race | sambaThe "|" 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;
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.):
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.