23
Overview • Reference parameters • Documenting functions • A game of craps. Design, code, test and document

Overview Reference parameters Documenting functions A game of craps. Design, code, test and document

Embed Size (px)

Citation preview

Overview

• Reference parameters

• Documenting functions

• A game of craps. Design, code, test and document

reference parameters

What do you do if you do to get two outputs from a function?

GetMinAndMaxint (smallest value)

int (largest value)

Have caller provide address of memory locations in which function may place data.

reference parameters

int main () { int minimum = 0, maximum = 0; GetMinAndMax (minimum, maximum); }

void GetMinAndMax (int &min, int &max) { min = INT_MIN; max = INT_MAX; }

Walk through this. variables

int main() { int minimum = 0, maximum = 0; GetMinAndMax (minimum, maximum); cout << "Minimum value is " << minimum << endl; cout << "Maximum value is " << maximum << endl; return 0;}

void GetMinAndMax (int &min, int &max) { int input; min = INT_MAX; max = INT_MIN; for (int i = 0; i <= 5; i++) { cout << “Enter an integer: “; cin >> input; if (min > input) min = input; if (max < input) max = input; } } // Walk through this. Variables.

ExerciseChange the GetMinAndMax function so that it validates theuser’s input. Only integers between 0 and 1000 are allowed (including 0 and 1000).

If invalid input is detected, GetMinAndMax stops processing input and returns false to the caller.

If GetMinAndMax successfully processes the input and calculates the minimum and maximum values, it returns true to the caller.

GetMinAndMaxint (smallest value)

int (largest value)

bool true if smallest and largest contain valid data false if smallest and largest not valid

More Exercises1) Change the main function so that it calls your new version of GetMinAndMax properly

2) Fix the error in the following program segment void sum (int n) {

if (0 == n) return 0; else n = n + n;}

3) Rewrite the following function prototype to return the result as a parameter instead of as a return value int Square (int y);

Documentation StandardEach function should begin with a comment of the following form:

/* * <function name> * * PRE: <list of pre-conditions, usually stated in

terms of * input arguments> * POST: <list of post-conditions, usually stated in

terms * of output arguments> * */

pre-conditions are conditions that must hold prior to the invocation of the function. post-conditions are those conditions that are true after the function returns.

Documentation Example

Cubey int

/* * Cube * * PRE: Input parameter integer y. Function will use this as * the base for the mathematical calculation. * POST: Return value is base y to the 3rd power. * */

y 3

int

Documentation Example

/* * IsDigit - Function will determine if the input character ch is * is a digit (0 - 9) or not. * * PRE: Input parameter - char ch. * * POST: Return value true if input character ch is a digit from * 0 - 9 * false if input character ch is not a digit * from 0 - 9. */

IsDigit bool true if ch is 0 to 9 false if ch is not 0 to 9

ch char

Exercises1) Produce the documentation header for the GetMinAndMaxfunction

2) Write a C++ function that satisfies the following pre and postconditions: /* * IsUppercase * * PRE: Input character ch. Function will determine if this * is an uppercase letter or not. * POST: Return value true if input character ch is an upper * case letter in the range A - Z. * false if input character ch is not an upper * case letter in the range A - Z. */

Game of craps

• Design it

• Code it

• Test it

• Document it

Game of crapsProblem statement - Write a C++ program that simulates playinga game of craps.

Rules of the game. A player makes a bet then rolls the dice. If the sum is - 7 or 11 They win! - 2 or 3 or 12 They lose! - other. This is the player’s point. They roll again until: - they match the point. If so, They win! - they roll a 7. They lose!

All bets are even odds. If they bet $10.00 and win, they win $10.00.If they bet $10.00 and lose they lose $10.00.

Top level designFind out how much money the player has to bet with. Get the player’s bet for this game. Play the game. If they win the game add their bet to the money they have left to play with, otherwise subtract their bet from the money they have left to play with. If they have any money left ask them if they want to play again otherwise say Goodbye! Thanks for the loot!

Functions:GetMoneyLeftGetBetPlayGameAskPlayAgain

Find out how much money the player has to bet with. int GetMoneyLeft() prompts user for the amount of money they have to play with. Validates number provided to make sure it is > 0. Get the player’s bet for this game. int GetBet (int moneyLeft) prompts user for the amount of money they want to bet on this game. Validates number provided to make sure it is less than or equal to moneyLeft. Play the game. bool PlayGame (); returns true if user wins, false if they loseIf they win the game add their bet to the money they have left to play with, otherwise subtract their bet from the money they have left to play with. If they have any money left ask them if they want to play again otherwise say Goodbye! Thanks for the loot! bool AskPlayAgain(); returns true if they want to play again.

Detailed design of PlayGamebool PlayGame (); returns true if user wins, false if they lose

rolls the dice. If the sum is - 7 or 11 They win! - 2 or 3 or 12 They lose! - other. This is the player’s point. They roll again until: - they match the point. If so, They win! - they roll a 7. They lose!

Functions needed: int RollDice () - returns the sum of two randomly generated numbers, each in the range 1 to 6.

Next step

• code main program using stubs for the functions

• test it with stubs

AskPlayAgain code and testbool AskPlayAgain () { char answer; while (true) { cout << “Do you want to play again? “; cin >> answer; if ((answer == ‘y’) || (answer == ‘Y’)) return true; if ((answer == ‘n’) || (answer == ‘N’)) return false; }}Test cases: N, n, Y, y, a, B

RollDice - code

int RollDice() { int die1, die2; die1 = 1 + rand() % 6; die2 = 1 + rand() % 6; cout << “Player rolled a “ << die1 + die2 << endl; return (die1 + die2); }Testing: cout statement should always print a numberless than or equal to 12;

PlayGamebool PlayGame() { Test Cases int sum, point; First roll (7, 11), (2, 3, 12), (point) switch (sum) { Point roll (7, match) case 7: case 11: In main: return true; - make sure moneyLeft is case 2: case 3: case 12: calculated properly based return false; on win or loss. default: - make sure user is never point = sum; offered the opportunity of while (true) { playing if moneyLeft is 0 sum = RollDice(); if (sum == 7) return false; if (sum == point) return true; } } // end switch}

GetMoneyLeftint GetMoneyLeft(){ int money; while (true) { cout << “How much would money do you have to play with? “; cin >> money; if (money > 0) return money; cout << “Must enter a whole number > 0” << endl; } }

Test cases: -1, 0, 1000

GetBetint GetBet (int moneyLeft){ int bet; while (true) { cout << “How much do you want to bet? “; cin >> bet; if (bet == 0) cout << “Only bets > $0 allowed” << endl; else if (bet <= moneyLeft) return bet; else cout << “You only have “ << moneyLeft << “ left” << endl;

Test cases: number < moneyLeft, number = moneyLeft, number > moneyLeft, Should not allow a bet of 0

Documentation and cleanup

• cleanup - delete couts used for debugging

• comments at top of file, variables

• comments ahead of complicated sections of code.

• pre and post conditions for GetMoneyLeft, GetBet, PlayGame, RollDice, AskPlayAgain.

Documentation for GetBet /* * GetBet - Prompts the player for the amount they * would like to wager on the next game * * PRE: Input parameter. int moneyLeft. The total * amount of money the player has left to wager. * Assumes this number is a non-negative * number > 0. * * POST: Return value is the amount the player would * like to wager on the next game. Will be greater * than 0 and less than or equal to the total * amount the player has left to wager. */