Week 14 Lab
Q&A on Material
- Questions regarding homework
- Debugging: the most important skill?
- Debugging compiler errors: learning to read the compiler
- Debugging with cout/print statements
- Debugging with gdb: break, run, continue, step, next, print
- Review for Final
Sign in to lab!
HW Check in
Check in as a group to see if everyone understand the basic premise of the homework and what you are expected to do, including the extra credit option. Ask any general or conceptual questions and discuss the answers. Take care to do your own work, but the TA/CPs can help you individually.
Debugging Exercises
Walk through this exercises and complete them as a group (if time allows):
Dice Game of ‘Craps’ Debugging
Below is a program to simulate how many rolls an average player’s turn will last in the casino game of craps. It will also determine the expected monetary return on betting 1 dollar on the “pass line” (which is the only part of the game we simulate).
Craps rules
- The player rolls 2 dice.
- If the sum of the dice is 7 or 11 the player wins their bet and the turn continues (go back to step 1).
- If the sum of the dice is 2, 3, or 12 the player loses their bet, but the turn continues (go back to step 1).
- If the sum is any other number (besides 2, 3, 7, 11, or 12) then that value is known as the point number and play continues.
- The player rolls the dice until…
- The sum of the dice is 7 in which case the player loses their bet and the turn ENDS
- The sum of the dice is the same as the point value in which case the player wins their bet and the turn continues, starting over at step 1.
Debug the program by copy/pasting the code into onlinegdb.com, setting breakpoints and examining the operation. When you run it for debugging use a small number of simulations (1-3) so you can step through the code and see if it behaves as expected for the variable values. Only 2 lines are in error.
When you think it works, you can use 100,000-500,000 simulations to see if you get the desired averages (or close to it). The seed number can be any positive integer. If you enter the same seed on multiple program executions, it will cause the same random numbers to be generated. Different seeds should lead to different random numbers on each program run.
// Program to simulate how many rolls an average player's turn
// will last in the casino game of craps.
// Also determines the expected monetary return on betting 1 dollar
// on the pass line.
//
// Craps rules:
//
// 1. The player rolls 2 dice.
// 2. If the sum of the dice is 7 or 11 the player wins their bet
// and the turn continues (go back to step 1).
// 3. If the sum of the dice is 2, 3, or 12 the player loses their bet,
// but the turn continues (go back to step 1).
// 4. If the sum is any other number (besides 2, 3, 7, 11, or 12)
// then that value is known as the point number and play continues.
// 5. The player rolls the dice until...
// a. The sum of the dice is 7 in which case the player loses their
// bet and the turn ENDS
// b. The sum of the dice is the same as the point value in which case
// the player wins their bet and the turn continues, starting
// over at step 1.
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
int main()
{
int numsims, seed;
int totalrolls = 0, numrolls = 0, numgames=0;
int dollars = 0;
cout << "Enter the number of simulations and a seed:" << endl;
cin >> numsims >> seed;
srand(time(0));
dollars = 0;
for(int i = 0; i < numsims; i++){
// start a player's turn
bool done = false;
numrolls = 0;
while( !done ){
// First roll
int d1 = 1+rand()%6;
int d2 = 1+rand()%6;
int r1 = d1+d2;
numrolls++;
// 7 or 11 wins but play continues
if(r1 == 7 || r1 == 11){
dollars++;
}
// 2, 3, or 12 loses but play continues
else if(r1 == 2 || r1 == 3 || r1 == 12){
dollars--;
}
else {
// a "point" number has been set
do {
d1 = 1+rand()%6;
d2 = 1+rand()%6;
numrolls++;
} while( r1 != d1+d2 && r1 != 7);
// Determine why the game ended
if(r1 == 7){
dollars--;
// if a loss, the turn is over
done = true;
}
else {
// If a win, play continues
dollars++;
}
} // else
numgames++;
} // while
totalrolls += numrolls;
} // for
cout << "Average rolls: " << totalrolls / (double) numsims << endl;
cout << "\tExpected: approx 8.525" << endl;
cout << "Average earnings: " << dollars / (double) numgames << endl;
cout << "\tExpected: approx -.0141" << endl;
return 0;
}
Complete any exercises in the Vocareum In-Class - Debugging-2
Exercises not performed in class (i.e. primes
and sumpairs
)
Review for the Final
The final is technically cumulative but will focus more on the content since the 2nd midterm (arrays, functions, strings). It will involve some general questions and tracing and some questions where you have to write code. See the final info page.
Exercises
Walk through these exercises and complete them as a group.
Practice with Strings
Deriving Arithmetic Relationships
Open-ended Help
Any students with questions may stay after to get help. Anyone else may leave at this stage.