Presentation is loading. Please wait.

Presentation is loading. Please wait.

Programming Assignment #3 CS-2301, B-Term 20091 Programming Assignment #3 User-defined Functions Due, November 18, 11:59 PM (Assignment adapted from C:

Similar presentations


Presentation on theme: "Programming Assignment #3 CS-2301, B-Term 20091 Programming Assignment #3 User-defined Functions Due, November 18, 11:59 PM (Assignment adapted from C:"— Presentation transcript:

1 Programming Assignment #3 CS-2301, B-Term 20091 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!

2 Programming Assignment #3 CS-2301, B-Term 20092 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

3 Programming Assignment #3 CS-2301, B-Term 20093 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.

4 Programming Assignment #3 CS-2301, B-Term 20094 User-defined Functions Define and implement four functions:– validWager playGame – adapted from existing code adjustBalance getYesOrNo Write pre- and post-conditions for all functions

5 Programming Assignment #3 CS-2301, B-Term 20095 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

6 Programming Assignment #3 CS-2301, B-Term 20096 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

7 Programming Assignment #3 CS-2301, B-Term 20097 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!

8 Programming Assignment #3 CS-2301, B-Term 20098 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

9 Programming Assignment #3 CS-2301, B-Term 20099 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

10 Programming Assignment #3 CS-2301, B-Term 200910 Linux Random Number Generators There are many! Use rand() Returns values in range 0.. 2 15 -1 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

11 Programming Assignment #3 CS-2301, B-Term 200911 Questions?


Download ppt "Programming Assignment #3 CS-2301, B-Term 20091 Programming Assignment #3 User-defined Functions Due, November 18, 11:59 PM (Assignment adapted from C:"

Similar presentations


Ads by Google