1 Session-23 CSIT 121 Spring 2006 Revision Ch 7: Reference Parameters Revision Ch 7: Reference Parameters Chapter 8: Value Returning Functions Chapter.

Slides:



Advertisements
Similar presentations
Savitch N/A Randomness In Programming Simulations
Advertisements

Factorial Preparatory Exercise #include using namespace std; double fact(double); int fact(int); int main(void) { const int n=20; ofstream my_file("results.txt");
Chapter 6 Advanced Function Features Pass by Value Pass by Reference Const parameters Overloaded functions.
Inheritance - Case Study TCP1201: 2013/2014. Case Study – Guessing Game Listed below is the code to play a guessing game using procedural programming.
Building Java Programs
Tinaliah, S. Kom.. * * * * * * * * * * * * * * * * * #include using namespace std; void main () { for (int i = 1; i
Triana Elizabeth, S.Kom. #include using namespace std; void main () { for (int i = 1; i
1 Random number generation Using srand(), rand() & time(0) Searching and Sorting Demo Making searching & sorting more generic Overloading the functions.
Programming Functions: Passing Parameters by Reference.
 Monday, 10/28/02, Slide #1 CS106 Introduction to CS1 Monday, 10/28/02  QUESTIONS on HW 03??  Today: Generating random numbers  Reading & exercises:
An Introduction to Programming with C++ Fifth Edition
1 Lab Session-1 CSIT221 Fall 2002 b Refresher slides b Practice Problem b Lab Exercise (Demo Required)
1 Session-12 CSIT 121 Spring 2006 Test-1 is on March 9 th ; Demo-5 due date extended to March 7 Test-1 is on March 9 th ; Demo-5 due date extended to March.
1 Session-16 CSIT 121 Spring 2006 Demo for switch-case due now Demo for switch-case due now String comparison String comparison Slide 23; sample case.
1 Lab Session-8 CSIT-121 Fall 2003 w Call by Reference w Lab Exercise 1 w Lab Exercise for Demo w Practice Problems.
COMP102 – Programming Fundamentals I LA2B (Mon 5-7pm) LA2E (Fri 3-5pm) LA2F (Fri 5-7pm) TA: Jackie Lo.
1 Lab Session-9 CSIT221 Fall 2002 Lab Exercise Based on operator overloading (Demo Required)
1 Lab Session-1 CSIT221 Spring 2003 b Group Programming Challenge b Individual Lab Exercise (Demo Required)
1 Lab Session-3 CSIT221 Spring 2003 b Group Worksheet 3 Exercise (Demo Required) b No new lab demo will be assigned to allow you to focus on HW#1.
1 Session-7 CSIT 121 Spring 2006 Lab Demo of NiMo Lesson 3-2 Exercises 1,2,3,4,5,6 (Arithmetic Operators with Program ‘Convert.cpp’) Q&A about operators.
1 Session-14 CSIT 121 Spring 2006 Demo due today; demo hint was sent to your campus accounts Demo due today; demo hint was sent to your campus .
Programming Assignment 8 CS113 Spring Programming Assignment 8 Implement the sorting routine of your choice. Compare average performance of your.
1 Lab Session-4 CSIT121 Fall 2004 Scope of Variables Top Down Design Problem The Solution Lab Exercise for Demo.
Random (1) Random class contains a method to generate random numbers of integer and double type Note: before using Random class, you should add following.
1 Lab Session-3 CSIT121 Fall 2003 Some Questions Scope of Variables Top Down Design Problem The Solution Lab Exercise for Demo.
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 12 – Craps Game Application: Introducing Random.
1 Lab Session-VII CSIT-121 Fall Revising Previous Lab and performing ASCII chart printing experiment (DEMO) 4 Visual Studio Features 4 The While.
1 Lab Session-9 CSIT-121 Fall 2003 w Random Number Generation w Designing a Game.
1 Lab Session-3 CSIT 121 Spring’05 Division rules Operator precedence rules Lab Exercise.
1 Random numbers Random  completely unpredictable. No way to determine in advance what value will be chosen from a set of equally probable elements. Impossible.
1 Lab Session-11 CSIT 121 Fall 2003 Using arrays in functions Programming Exercise.
1 Session-9 CSIT 121 Spring 2006 Lab Demo (first 20 minutes) The correct way to do the previous lab sheet Function calls Calling void functions Calling.
1 Session-13 CSIT 121 Spring 2006 Test-1 is on March 9 th ; Demo-5 due date extended to March 7 Test-1 is on March 9 th ; Demo-5 due date extended to.
1 Lab Session-VIII CSIT-121 Spring 2002 w Formatted Output w Call by Reference w Lab Exercises w File Handling w Lab Exercises.
Random Number Generator. Using Software We can generate random number by: 1- table 2- hardware 3-- software Function to generate RN by SW is: 1-rand 2-
Craps!. Example: A Game of Chance Craps simulator Rules – Roll two dice 7 or 11 on first throw, player wins 2, 3, or 12 on first throw, player loses 4,
1 Lab Session-8 CSIT-121 Spring 2005 Call by Reference Lab Exercise for Demo Practice Problems.
Implementation of the Hangman Game in C++
CMSC 1041 Functions II Functions that return a value.
Today’s Lecture Predefined Functions. Introduction to Functions  Reuse Issue  Building Blocks of Programs  Two types of functions  Predefined  Programmer.
 2008 Pearson Education, Inc. All rights reserved Case Study: Random Number Generation C++ Standard Library function rand – Introduces the element.
CS1101: Programming Methodology Aaron Tan.
Loops Wrap Up 10/21/13. Topics *Sentinel Loops *Nested Loops *Random Numbers.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Functions (Header files and Library Functions) Outline.
CS221 Random Numbers. Random numbers are often very important in programming Suppose you are writing a program to play the game of roulette The numbers.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 5 - Functions Outline 5.1Introduction 5.2Program.
WEEK 6 Class Activities Lecturer’s slides.
Computer Science: A Structured Programming Approach Using C1 4-5 Standard Functions C provides a rich collection of standard functions whose definitions.
Random numbers in C++ Nobody knows what’s next....
CS Class 15 Today  More practice with arrays  Introduction to Multi-dimensional arrays Announcements  Programming project #4 due 10/23 by midnight.
UNIT 11 Random Numbers.
Copyright © 2012 Pearson Addison-Wesley. All rights reserved. Chapter 4 Functions.
1 Generating Random Numbers Textbook ch.6, pg
MR. CRONE Generating Random Numbers. Random Numbers Many programs require the computer to generate random numbers Random numbers are used in many applications.
Revision. Generating a pseudo-random number Necessary libraries: and Seeding: srand(time(NULL))  We set the seed based on the current time  NB! Time-dependent.
Chapter INTRODUCTION Data Types and Arithmetic Calculations.
Why they aren't really random
Auburn University COMP 3000 Object-Oriented Programming for Engineers and Scientists Function Basics TBC=15 Dr. Xiao Qin.
Number guessing game Pick a random number between 1 and 10
Iterative Constructs Review
CMPT 201 Functions.
Deitel- C:How to Program (5ed)
CS1010 Programming Methodology
Lab Session-9 CSIT-121 Spring 2005
توابع در C++ قسمت اول اصول كامپيوتر 1.
When I want to execute the subroutine I just give the command Write()
Random Number Generation
CS150 Introduction to Computer Science 1
Revision.
Accuracy of Averages.
Presentation transcript:

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 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 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 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 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 Lab Demo Due by 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 Sample Contents of Stats File Number Attempts Number Attempts =========== =========== Mean 4 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. 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 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