Session-23 CSIT 121 Spring 2006 - PowerPoint PPT Presentation

About This Presentation
Title:

Session-23 CSIT 121 Spring 2006

Description:

Player's statistics are recorded in a file including the guessed number followed ... Exam Prep Exercise 15 page 353. Programming Warm up Exercise 16 page 354 ... – PowerPoint PPT presentation

Number of Views:13
Avg rating:3.0/5.0
Slides: 10
Provided by: sunyfr
Category:

less

Transcript and Presenter's Notes

Title: Session-23 CSIT 121 Spring 2006


1
Session-23 CSIT 121 Spring 2006
  • Revision Ch 7 Reference Parameters
  • Chapter 8 Value Returning Functions
  • Demo of writing a value returning function in a
    program

2
Random Number Generation
  • You may generate random numbers by calling the
    rand() function in the cstdlib library
  • However, it only generates pseudorandom numbers
  • Check out the code on the next slide

3
Program Generates Random Numbers
  • include ltiostreamgt
  • include ltcstdlibgt
  • using namespace std
  • void main()
  • int number
  • for (int i0 ilt10 i)
  • number rand()
  • coutltltnumberltltendl

4
Scaling the Numbers
  • The numbers generated are too big
  • We may scale them down to a limited range
  • How can we make the program generate random
    numbers between 1 and 20?

5
Generating Truly Random Numbers
  • In order to generate truly random numbers, use
    the srand function to provide a seed to the
    rand() function
  • This seed can also be randomized by using the
    current time as the seed
  • Include ltctimegt, and use srand(time(0)) before
    calling the rand() function

6
Lab Demo Due by emailFri Apr 28
  • Design the HiLo game with functions. The
    program should call a function to generate and
    return a random number below 100. Players guess
    will be sent to a second function to decide if it
    is too high or too low or just right. Players
    statistics are recorded in a file including the
    guessed number followed by total number of
    attempts. See next two slides

7
Sample Contents of Stats File
  • Number Attempts
  • 5 7
  • 6 8
  • 7 1
  • 3 2
  • Mean 4

8
Bonus Points
  • Bonus 50 extra points are awarded for
    introducing a new feature into the program. The
    new feature would allow a player to give up by
    entering a special value (for example 1).
    When the player gives up, that specific number
    and attempts are NOT recorded in the stats file.

9
Practice Problems
  • Exam Prep Exercise 15 page 353
  • Programming Warm up Exercise 16 page 354
  • Programming Warm up Exercise 7 page 413
  • Programming Warm up Exercise 14 page 414
Write a Comment
User Comments (0)
About PowerShow.com