Download presentation
Presentation is loading. Please wait.
1
1 Session-23 CSIT 121 Spring 2006 Revision Ch 7: Reference Parameters Revision Ch 7: Reference Parameters Chapter 8: Value Returning Functions Chapter 8: Value Returning Functions Demo of writing a value returning function in a program Demo of writing a value returning function in a program
2
2 Random Number Generation You may generate random numbers by calling the rand() function in the cstdlib library You may generate random numbers by calling the rand() function in the cstdlib library However, it only generates pseudorandom numbers However, it only generates pseudorandom numbers Check out the code on the next slide Check out the code on the next slide
3
3 Program Generates Random Numbers #include #include using namespace std; using namespace std; void main() void main() { int number; int number; for (int i=0; i<10; i++) for (int i=0; i<10; i++) { number = rand(); number = rand(); cout<<number<<endl; cout<<number<<endl; } }
4
4 Scaling the Numbers The numbers generated are too big The numbers generated are too big We may scale them down to a limited range We may scale them down to a limited range How can we make the program generate random numbers between 1 and 20? How can we make the program generate random numbers between 1 and 20?
5
5 Generating Truly Random Numbers In order to generate truly random numbers, use the srand function to provide a seed to the rand() function 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 This seed can also be randomized by using the current time as the seed Include, and use srand(time(0)) before calling the rand() function Include, and use srand(time(0)) before calling the rand() function
6
6 Lab Demo Due by email Fri Apr 28 Design the Hi~Lo game with functions. The program should call a function to generate and return a random number below 100. Player’s guess will be sent to a second function to decide if it is too high or too low or just right. Player’s statistics are recorded in a file including the guessed number followed by total number of attempts. See next two slides Design the Hi~Lo game with functions. The program should call a function to generate and return a random number below 100. Player’s guess will be sent to a second function to decide if it is too high or too low or just right. Player’s statistics are recorded in a file including the guessed number followed by total number of attempts. See next two slides
7
7 Sample Contents of Stats File Number Attempts Number Attempts 5 7 5 7 6 8 6 8 7 1 7 1 3 2 3 2 =========== =========== Mean 4 Mean 4
8
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. 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
9 Practice Problems Exam Prep Exercise 15 page 353 Exam Prep Exercise 15 page 353 Programming Warm up Exercise 16 page 354 Programming Warm up Exercise 16 page 354 Programming Warm up Exercise 7 page 413 Programming Warm up Exercise 7 page 413 Programming Warm up Exercise 14 page 414 Programming Warm up Exercise 14 page 414
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.