Programming Assignment #3 CS-2301, B-Term Programming Assignment #3 User-defined Functions Due, November 18, 11:59 PM (Assignment adapted from C: How to Program, 5 th and 6 th editions, by Deitel and Deitel) Heads up:– Next week, the Programming Assignment will be due on Tuesday, November 24!
Programming Assignment #3 CS-2301, B-Term Objectives Modify someone else’s code Create and use your own functions Read mixed data from input Use enumerated types See handout (.doc,.html)dochtml
Programming Assignment #3 CS-2301, B-Term Approach Download craps.c – a program that “plays” the game of Crapscraps.c Using a random number generator to simulate the rolls of a pair of dice Modify as follows:– Maintain “bank” balance for player Ask player if he/she wants to play another game Accept a wager; test for validity Play the game, update the balance, etc.
Programming Assignment #3 CS-2301, B-Term User-defined Functions Define and implement four functions:– validWager playGame – adapted from existing code adjustBalance getYesOrNo Write pre- and post-conditions for all functions
Programming Assignment #3 CS-2301, B-Term Action of the Game Print current balance If > 0, ask user if he/she wants to play another game Otherwise, print a summary and exit If yes, ask for wager Must be <= balance Play the game Update the balance Repeat
Programming Assignment #3 CS-2301, B-Term Things to Know Mixed numeric and character input See handout Especially for draining lines, dealing with ends of lines Function getchar() More useful than scanf() in many cases
Programming Assignment #3 CS-2301, B-Term Random Number Generators Random numbers are needed frequently in engineering & scientific computations Simulations, arrival times, etc. Exercising other code Analyzing system performance Definition: Random Number Generator A function that returns a seemingly random number each time it is called (Usually) within a specified range Repeated calls yield a random sequence!
Programming Assignment #3 CS-2301, B-Term Random Number Generators (continued) Algorithmic –Retain information in static variables –Scramble numbers to get something that “looks” random on each call Entire mathematical theory about them –Evaluating the quality of the randomness
Programming Assignment #3 CS-2301, B-Term Problem with Random Number Generators Don’t give the same answer each time! Difficult to get reproducible behavior when debugging! Solution:– the seed A numeric value for initializing the internal state So that the generator produces the same sequence each time
Programming Assignment #3 CS-2301, B-Term Linux Random Number Generators There are many! Use rand() Returns values in range rand() % r returns values in range 0.. (r-1) Seeding:– srand(unsigned int seed) –Default 1 srand(time(NULL)) –Seeds to current time (measured in seconds since the beginning) See handout, Appendix B
Programming Assignment #3 CS-2301, B-Term Questions?