CS 1005, C Term 2000
Techniques of Programming
Lab 3 (Feb. 2)
Instructions
This lab assignment provides additional practice in
iteration constructs within a client program for a
C++ class, as needed for example in HW2.
You will write a program that simulates random rolls of an 80-sided die
and reports the results of these rolls by printing an appropriate number
of asterisks on the console screen. The total number of rolls is
specified by the user.
A run of your program should look something like
this.
- Sign in with the TA. You should both print your name
and sign the sheet.
- Listen to the TA's mini-lecture.
- Do the problems listed below. Feel free to ask the TA
questions about the problems. Don't worry if you can't
finish all of the problems before the lab session is over.
You can finish afterwards. However, actively working
on the lab assignment during the lab session is required.
Problems
-
Log onto your CCC Unix account.
Change the working directory to your CS1005 directory.
Create a subdirectory named Lab3 and
change the working directory to Lab3.
Copy the files dice.h, dice.cpp, randgen.h, randgen.cpp
from the directory named /cs/cs1005/samples/lab3/ to your
Lab3 directory.
-
In a new text file named lab3.cxx, place the standard outline
of a C++ program. You will need #include directives for the
libraries iostream and string, and one for the header file dice.h.
Start the body of the main() function with a declaration for
an 80-sided Dice object named starcounter, as follows:
Dice starcounter(80);
Add a return 0; statement and attempt to compile this tiny program
using the following command line:
g++ -o lab3prog lab3.cxx dice.cpp randgen.cpp
If there are errors at this stage, fix them before proceeding further.
-
Add two functions to lab3.cxx named PrintStars and GetInteger
according to the specifications that appear below. Notice that
PrintStars is the function from Test 1.
void PrintStars(int n) {
// prints n '*' characters in a row and then endl
}
int GetInteger(string prompt) {
// prints the prompt string, waits for the user to enter an integer,
// and returns this integer
}
-
Now fill in the main() function so that the user is prompted to
enter the number of rolls, then have a loop roll the Dice object
named starcounter as many times as indicated by the user.
On each pass of the loop,
use PrintStars to print as many stars as the result of the roll in
that pass. Compile and debug your program as needed.
Try out your program for different numbers of rolls.
-
Use any remaining time to continue working on
HW2.