Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Overview Reference parameters Documenting functions A game of craps. Design, code, test and document."— Presentation transcript:

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

2 reference parameters What do you do if you do to get two outputs from a function? GetMinAndMax int (smallest value) int (largest value) Have caller provide address of memory locations in which function may place data.

3 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

4 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.

5 Exercise Change the GetMinAndMax function so that it validates the user’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. GetMinAndMax int (smallest value) int (largest value) bool true if smallest and largest contain valid data false if smallest and largest not valid

6 More Exercises 1) 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);

7 Documentation Standard Each function should begin with a comment of the following form: /* * * 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.

8 Documentation Example Cube y 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

9 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

10 Exercises 1) Produce the documentation header for the GetMinAndMax function 2) Write a C++ function that satisfies the following pre and post conditions: /* * 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. */

11 Game of craps Design it Code it Test it Document it

12 Game of craps Problem statement - Write a C++ program that simulates playing a 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.

13 Top level design Find 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: GetMoneyLeft GetBet PlayGame AskPlayAgain

14 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 lose 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! bool AskPlayAgain(); returns true if they want to play again.

15 Detailed design of PlayGame bool 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.

16 Next step code main program using stubs for the functions test it with stubs

17 AskPlayAgain code and test bool 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

18 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 number less than or equal to 12;

19 PlayGame bool 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 }

20 GetMoneyLeft int GetMoneyLeft() { int money; while (true) { cout << “How much would money do you have to play with? “; cin >> money; if (money > 0) return money; cout 0” << endl; } Test cases: -1, 0, 1000

21 GetBet int GetBet (int moneyLeft) { int bet; while (true) { cout << “How much do you want to bet? “; cin >> bet; if (bet == 0) cout $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

22 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.

23 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. */


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

Similar presentations


Ads by Google