C++ Programming Lecture 10 Functions – Part II

Slides:



Advertisements
Similar presentations
Chapter 5 C Functions The best way to develop and maintain a large program is to divide it into several smaller program modules, each of which is more.
Advertisements

 2000 Prentice Hall, Inc. All rights reserved. Chapter 5 - Functions Outline 5.1Introduction 5.2Program Modules in C 5.3Math Library Functions 5.4Functions.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 5 - Functions Outline 5.1Introduction 5.2Program.
 2000 Prentice Hall, Inc. All rights reserved. 1 Chapter 3 - Functions Outline 3.1Introduction 3.2Program Components in C++ 3.3Math Library Functions.
 2007 Pearson Education, Inc. All rights reserved C Functions.
 2007 Pearson Education, Inc. All rights reserved C Functions.
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 12 – Craps Game Application: Introducing Random.
1 Review (Week1) C++_ the unit of programming is the class from which objects are eventually instantiated. C++ classes contain functions that implement.
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.
C Lecture Notes Functions (Cont...). C Lecture Notes 5.8Calling Functions: Call by Value and Call by Reference Used when invoking functions Call by value.
Functions. 3Introduction Divide and conquer –Construct a program from smaller pieces or components –Each piece more manageable than the original program.
1 Functions Modules: functions and classes Programs use new and “prepackaged” modules –New: programmer-defined functions, classes –Prepackaged: from the.
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 Lecture 3 Part 1 Functions with math and randomness.
Function. Introduction Library function New defined function Random number generator Scope Inline function Function overload Function Function.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 3 - Functions Outline 3.1Introduction 3.2Program Components in C++ 3.3Math Library Functions.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. C How To Program - 4th edition Deitels Class 05 University.
Iterative Constructs Review l What are named constants? Why are they needed? l What is a block? What is special about declaring a variable inside a block?
Today’s Lecture Predefined Functions. Introduction to Functions  Reuse Issue  Building Blocks of Programs  Two types of functions  Predefined  Programmer.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 6 September 17, 2009.
CSIS 113A Lecture 5 Random Numbers, while, do-while.
Chapter 5 - Functions Outline 5.1Introduction 5.2Program Modules in C 5.3Math Library Functions 5.4Functions 5.5Function Definitions 5.6Function Prototypes.
 2008 Pearson Education, Inc. All rights reserved Case Study: Random Number Generation C++ Standard Library function rand – Introduces the element.
 2000 Prentice Hall, Inc. All rights reserved. 1 Chapter 3 - Functions Outline 3.1Introduction 3.2Program Components in C++ 3.3Math Library Functions.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 6 - Functions.
 2003 Prentice Hall, Inc. All rights reserved. 1 Arrays Outline Introduction Arrays Declaring Arrays Examples Using Arrays.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Functions (Header files and Library Functions) Outline.
C++ Programming Lecture 11 Functions – Part III By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 5 - Functions Outline 5.1Introduction 5.2Program.
C++ Programming Lecture 9 Functions – Part I By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department.
1 Chapter 3 - Functions Outline 3.1Introduction 3.2Program Components in C++ 3.3Math Library Functions 3.4Functions 3.5Function Definitions 3.6Function.
EPSII 59:006 Spring Call-by-value example #include void increment(int); //prototype for increment function int main(void) { int a=1; printf("Value.
C++ Programming Lecture 7 Control Structure I (Selection) – Part II The Hashemite University Computer Engineering Department.
C++ Programming Lecture 17 Pointers – Part I The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
C++ Programming Lecture 3 C++ Basics – Part I The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
C++ Programming Lecture 13 Functions – Part V The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
C++ Programming Lecture 19 Strings The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
 2000 Prentice Hall, Inc. All rights reserved. 1 Chapter 3 - Functions.
C++ Programming Lecture 14 Arrays – Part I The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
 2000 Prentice Hall, Inc. All rights reserved Introduction Divide and conquer –Construct a program from smaller pieces or components –Each piece.
C++ Programming Lecture 12 Functions – Part IV
C++ Programming Lecture 16 Arrays – Part III The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
CHAPTER 4 FUNCTIONS Dr. Shady Yehia Elmashad. Outline 1.Introduction 2.Program Components in C++ 3.Math Library Functions 4.Functions 5.Function Definitions.
1 Lecture 4: Part1 Arrays Introduction Arrays  Structures of related data items  Static entity (same size throughout program)
 2000 Prentice Hall, Inc. All rights reserved Program Components in C++ Function definitions –Only written once –These statements are hidden from.
1 Generating Random Numbers Textbook ch.6, pg
C++ Programming Lecture 13 Functions – Part V By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department.
Switch Case, Enums, and Random Numbers CS 242 Tarik Booker California State University, Los Angeles.
Chapter INTRODUCTION Data Types and Arithmetic Calculations.
 2003 Prentice Hall, Inc. All rights reserved. 1 Functions and Recursion Outline Introduction Program Components in C++ Math Library Functions Functions.
Functions.
CISC181 Introduction to Computer Science Dr
Chapter 3 - Functions Outline 3.1 Introduction
TMF1414 Introduction to Programming
Number guessing game Pick a random number between 1 and 10
Iterative Constructs Review
CSC113: Computer Programming (Theory = 03, Lab = 01)
Chapter 5 - Functions Outline 5.1 Introduction
Chapter 5 - Functions Outline 5.1 Introduction
Chapter 5 - Functions Outline 5.1 Introduction
توابع در C++ قسمت اول اصول كامپيوتر 1.
Chapter 6 - Functions Outline 5.1 Introduction
C++ Programming Lecture 16 Arrays – Part III
CS150 Introduction to Computer Science 1
C++ Programming Lecture 3 C++ Basics – Part I
C++ Programming Lecture 17 Pointers – Part I
C++ Programming Lecture 20 Strings
6 Functions.
Chapter 3 - Functions Outline 3.1 Introduction
C++ Programming Lecture 7 Control Structure I (Selection) – Part II
Presentation transcript:

C++ Programming Lecture 10 Functions – Part II By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department

The Hashemite University Outline Introduction. Random numbers generation in C++. enum data type identification and usage. Examples. The Hashemite University

Random Number Generation I Random number generation is used mainly in simulation and game playing based application. The rand() function is used in C++ to generate random integer numbers between 0 and a maximum specified value. rand() function takes nothing (i.e. void) as its arguments and returns an unsigned integer. In order to use this function you must Load <cstdlib> or <stdlib.h> The Hashemite University

Random Number Generation II rand function syntax: int i = rand(); Generates a pseudorandom number between 0 and RAND_MAX (usually 32767) RAND_MAX is a symbolic constant defined in the stdlib header file. 0 <= rand() <= RAND_MAX. A pseudorandom number is a preset sequence of "random" numbers. The same sequence is generated upon every program execution, is this preferred?. This repeated behavior is essentially in programming debug and verification in simulation and other random-based applications. The Hashemite University

Random Numbers Seeding I srand function Jumps to a seeded location in a "random" sequence. Similar to rand() function, srand function is defined in the <stdlib.h> library. Takes an unsigned integer as a seed (i.e. as an argument). It does not return any value (returns void), it just change the random sequence (randomizing the rand() function). Can be called more than once within the same program. Still you need to use the rand() function to get the random numbers. The Hashemite University

Random Numbers Seeding II srand syntax: srand( seed ); seed can be any unsigned integer entered manually be the user or initialized through the program. If the same seed is used every time the program is run we will get the same random sequence (i.e. the same without seed). The Hashemite University

Random Numbers Seeding III To initialize seed value automatically use the following syntax: srand( time( 0 ) ); time( 0 ) Returns the current calendar time in seconds. time() function takes a pointer as an argument and returns unsigned integer. Changes the seed every time the program is run, thereby allowing rand() to generate random numbers. So, it is much better than manual seeding. Need to include the <ctime> or <time.h> library to use the time() function. The Hashemite University

Changing Random Numbers Range I Scaling and shifting Reduces random number to a certain range Modulus ( % ) operator Reduces number between 0 and RAND_MAX to a number between 0 and the scaling factor: Number = offset (shift value) + rand() % scaling_factor Example i = rand() % 6 + 1; Generates a number between 1 and 6 The Hashemite University

Changing Random Numbers Range II Scaling and shifting equations: Lets assume the range is [min, max], then: Offset = min. Scaling factor = max – min + 1 Lets assume the range is [min, max)  [min, max-1], then: Scaling factor = max – min Lets assume the range is (min, max][min+1, max], then: Offset = min + 1. Continue for other possibilities ..... The Hashemite University

Changing Random Numbers Range III All the previous equations are used to generate integers but what about floating point random numbers? First: convert the floating point range into an integer range by multiplying the range by 10^n where n is at least the number of digits after the decimal point. Second: find the values of both the offset and scaling factor based on the equations in the previous slide. Third: divide the resulting random number on 10^n to get a floating point random number in the specified range. Note that n controls the difference value (lets call it the step width) between the generated random numbers. The Hashemite University

The Hashemite University Examples Generate random numbers in the following ranges: 100 <= n <= 200  int n = 100 + rand()%101 100 <= n < 500  int n = 100 + rand()%400 50 < n <= 200  int n = 51 + rand()%150 100 < n < 200  int n = 101 + rand()%99 0.01 <= n <= 0.08  double n = (1 + rand()%8)/100 -- with step width = 0.01 0.02 <= n <= 0.9  double n = (20 + rand()%821)/1000 -- with step width = 0.001 The Hashemite University

The Hashemite University 1 // Fig. 3.7: fig03_07.cpp 2 // Shifted, scaled integers produced by 1 + rand() % 6 3 #include <iostream> 4 5 using std::cout; 6 using std::endl; 7 8 #include <iomanip> 9 10 using std::setw; 11 12 #include <cstdlib> 13 14 int main() 15 { 16 for ( int i = 1; i <= 20; i++ ) { 17 cout << setw( 10 ) << ( 1 + rand() % 6 ); 18 19 if ( i % 5 == 0 ) 20 cout << endl; 21 } 22 23 return 0; 24 } Notice rand() % 6 . This returns a number between 0 and 5 (scaling). Add 1 to get a number between 1 and 6. Executing the program again gives the same "random" dice rolls. 5 5 3 5 5 2 4 2 5 5 5 3 2 2 1 5 1 4 6 4 The Hashemite University

The Hashemite University 1 // Fig. 3.9: fig03_09.cpp 2 // Randomizing die-rolling program 3 #include <iostream> 4 5 using std::cout; 6 using std::cin; 7 using std::endl; 8 9 #include <iomanip> 10 11 using std::setw; 12 13 #include <cstdlib> 14 15 int main() 16 { 17 unsigned seed; 18 19 cout << "Enter seed: "; 20 cin >> seed; 21 srand( seed ); 22 23 for ( int i = 1; i <= 10; i++ ) { 24 cout << setw( 10 ) << 1 + rand() % 6; 25 26 if ( i % 5 == 0 ) 27 cout << endl; 28 } 29 30 return 0; 31 } The Hashemite University

The Hashemite University Enter seed: 67 1 6 5 1 4 5 6 3 1 2 Enter seed: 432 4 2 6 4 3 2 5 1 4 4 Notice how the die rolls change with the seed. Program Output The Hashemite University

The Hashemite University Enumeration I It is the counterpart of symbolic constants found in C (to assign integer values to symbolic constants). The main difference is that enum can define a new data type within the program. Enumeration - set of integers with identifiers enum typeName {constant1, constant2…}; Constants start at 0 (default), incremented by 1 Unique constant names The Hashemite University

The Hashemite University Enumeration II If you don't specify values for enum constants, the values start at zero and increase by one with each move down the list. E.g: enum MyEnumType { ALPHA, BETA, GAMMA }; ALPHA has a value of 0, BETA has a value of 1, and GAMMA has a value of 2. If you want, you may provide explicit values for enum constants, as in enum Size { SMALL = 10, MEDIUM = 100, LARGE = 1000 }; Or enum Size { SMALL = 10, MEDIUM, LARGE }; //here MEDIUM will have the value of 11 and LARGE will have the value of 12 You can assign positive and negative integer values to enum constants (but not floating point values which will be a syntax error). The Hashemite University

The Hashemite University Enumeration III There are two kinds of enum type declarations. One kind creates a named type, as in enum MyEnumType { ALPHA, BETA, GAMMA }; If you give an enum type a name, you can use that type for variables, function arguments and return values, and so on: E.g: enum MyEnumType x; /* legal in both C and C++ */ MyEnumType y; // legal only in C++ The other kind creates an unnamed type. This is used when you want names for constants but don't plan to use the type to declare variables, function arguments, etc. For example, you can write enum { HOMER, MARGE, BART, LISA, MAGGIE }; The Hashemite University

C++ enum type conversion rules There is an implicit conversion from any enum type to int. E.g: enum MyEnumType { ALPHA, BETA, GAMMA }; Then the following lines are legal: int i = BETA; // give i a value of 1 int j = 3 + GAMMA; // give j a value of 5 On the other hand, there is not an implicit conversion from int to an enum type: MyEnumType x = 2; // syntax error MyEnumType y = 123; // syntax error The Hashemite University

The Hashemite University Enumeration Example I #include<iostream.h> int main() { enum cars{BMW, MAZDA, KIA, BENZ}; cars mycar; int money, i, BMW_price = 1000, MAZDA_price = 100, KIA_price = 500, BENZ_price = 2000; cout << "Enter the car type (BMW = 0, MAZDA = 1, KIA = 2, BENZ = 3):\n"; cin >> i; mycar = (cars)i; cout << "Enter the money balance:\n"; cin >> money; The Hashemite University

Enumeration Example II switch(mycar) { case BMW: if(money >= BMW_price) cout << "You cannot bye car " << mycar << endl; else break; case MAZDA: if(money >= MAZDA_price) The Hashemite University

Enumeration Example II case KIA: if(money >= KIA_price) cout << "You cannot bye car " << mycar << endl; else break; case BENZ: if(money >= BENZ_price) } return 0; The Hashemite University

The Hashemite University Example Output Try it and find the output by your self. The Hashemite University

The Hashemite University Additional Notes This lecture covers the following material from the textbook: Fourth Edition: Chapter 3: Sections 3.8 and 3.9 The Hashemite University