COSC 181 Foundations of Computer Programming Class 22 6 7 Case Study Random Number Generation Cont Scaling and shifting random numbers To obtain random numbers in a desired range use a statement like number shiftingValue rand scalingFactor shiftingValue is equal to the first number in the desired range of consecutive integers scalingFactor is equal to the width of the desired range of consecutive integers number of consecutive integers in the range 2 In Class Exercise Use a loop to read in a series of numbers from the user Each time the user enters a number your program should generate a random number chosen from the range x to x 10 where x is the current user entered number The program should print out the randomly selected number before asking for the next user entered number 3 6 8 Case Study Game of Chance and Introducing enum Enumeration A set of integer constants represented by identifiers The values of enumeration constants start at 0 unless specified otherwise and increment by 1 The identifiers in an enum must be unique but separate enumeration constants can have the same integer value Defining an enumeration Keyword enum A type name Comma separated list of identifier names enclosed in braces Example enum Months JAN 1 FEB MAR APR 4 1 Fig 6 11 f ig06 11 cpp 2 3 inc lude iostream 4 us ing std cout 5 us ing std endl Outline Craps s imulat ion 6 7 inc lude cstdlib contains prototypes for functions srand and rand 8 us ing std rand 9 us ing std srand 1 cpp 10 11 inc lude ctime contains prototype for function time 12 us ing std time 13 fig06 1 include and using for function time 1 of 4 14 int rollDice rolls dice calculates amd displays sum 15 16 int main 17 18 enumeration with constants that represent the game status 19 enum Status CONTINUE WON LOST all caps in constants 20 Enumeration to keep track of the game status 21 int myPoint point if no win or loss on first roll 22 Status gameStatus can contain CONTINUE WON or LOST 23 24 Declaring a variable randomize random number generator using current time 25 srand time 0 26 27 Seeding the random int sumOfDice rollDice first roll of the dice of the user defined enumeration type number generator with the current time 5 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 determ ine game status and point i f needed based on f ir s t ro l l switch sumOfDice case 7 win with 7 on first roll case 11 win with 11 on first roll gameStatus WON break Assigning an enumeration case 2 lose with 2 on first roll case 3 lose with 3 on first roll case 12 lose with 12 on first roll gameStatus LOST break default did not win or lose so remember point gameStatus CONTINUE game is not over myPoint sumOfDice remember the point cout Po int is myPoint endl break optional at end of switch end switch while game is not complete while gameStatus CONTINUE not WON or LOST sumOfDice rollDice roll dice again Outline fig06 1 constant to gameStatus 1 cpp 2 of 4 Comparing a variable of an enumeration type to an enumeration constant 6 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 determine game status if sumOfDice myPoint win by making point gameStatus WON else if sumOfDice 7 lose by rolling 7 before point gameStatus LOST end while display won or lost message if gameStatus WON cout P layer wins endl else cout P layer loses endl Outline fig06 1 1 cpp 3 of 4 return 0 indicates successful termination end main roll dice calculate sum and display results int rollDice Function pick random die values int die1 1 rand 6 first die roll int die2 1 rand 6 second die roll that performs the task of rolling the dice int sum die1 die2 compute sum of die values 7 78 79 80 cout Player rolled die1 die2 81 sum endl 82 disp la y resu lt s of th is ro l l Outline returnsum end function rollDice 83 end funct ion rollD ice Player rolled 2 5 7 Player wins Player rolled 6 6 12 Player loses Player rolled Point is 6 Player rolled Player rolled Player rolled Player rolled Player wins 3 3 6 Player rolled Point is 4 Player rolled Player rolled Player rolled Player rolled Player rolled Player rolled Player rolled Player rolled Player loses 1 3 4 5 4 2 1 4 2 6 2 2 1 4 4 3 5 1 5 6 4 4 3 4 1 4 3 fig06 1 1 cpp 4 of 4 8 9 3 6 10 6 10 5 6 2 8 7 8 In Class Exercise Write a Hello World program The main function should call a function called printHello printHello should return no value and accept no parameters Its job is to output the text Hello World and then return control to main 9 Good Programming Practice 6 1 Capitalize the first letter of an identifier used as a user defined type name 10 Good Programming Practice 6 2 Use only uppercase letters in the names of enumeration constants This makes these constants stand out in a program and reminds the programmer that enumeration constants are not variables 11 Good Programming Practice 6 3 Using enumerations rather than integer constants can make programs clearer and more maintainable You can set the value of an enumeration constant once in the enumeration declaration 12 Common Programming Error 6 9 Assigning the integer equivalent of an enumeration constant to a variable of the enumeration type is a compilation error 13 Common Programming Error 6 10 After an enumeration constant has been defined attempting to assign another value to the enumeration constant is a compilation error 14 Review Time 15
View Full Document