+ All Categories
Home > Health & Medicine > 2621008 - C++ 3

2621008 - C++ 3

Date post: 17-Nov-2014
Category:
Upload: sali-sadegh-zadeh
View: 220 times
Download: 1 times
Share this document with a friend
Description:
 
Popular Tags:
107
Slide 1 Lecture 03
Transcript
Page 1: 2621008 -  C++ 3

Slide 1

Lecture 03

Page 2: 2621008 -  C++ 3

Slide 2

Loop Basics

Page 3: 2621008 -  C++ 3

Slide 3

Loop Types

Sentinel while do-while

Counting for

Page 4: 2621008 -  C++ 3

Slide 4

Sentinel Loops

Page 5: 2621008 -  C++ 3

Slide 5

while Syntax

while(test condition){    statement}

Page 6: 2621008 -  C++ 3

Slide 6

When a program encounters a while loop, the test condition is evaluated first. If the condition is TRUE, the program executes the body of the loop. The program then returns to the test condition and reevaluates. If the condition is still TRUE, the body executes again.

while Syntax

Page 7: 2621008 -  C++ 3

Slide 7

Example #1

while (7)    cout << “hello” << endl;

Page 8: 2621008 -  C++ 3

Slide 8

Example #2#include<iostream>using namespace std;int main(){int input;cout <<"enter number between 5 and 89, inclusive: ";cin >> input;while (input < 5 || input > 89){cout << "that value is unacceptable... try again: ";cin >> input;}cout << "the value"<<input<< "is in the interval[5, 89]"<< endl; cin.get();//system("PAUSE");return 0;}

Page 9: 2621008 -  C++ 3

Slide 9

Example #3#include<iostream>using namespace std;int main(){long sum = 0;int counter = 1; while (counter<=100){sum += counter;counter++ ;}cout << "sum of first 100 integers is "<< sum ;cin.get();system("PAUSE");return 0;}

Page 10: 2621008 -  C++ 3

do-while Syntax

Do{    statement}while (test condition);

Page 11: 2621008 -  C++ 3

Copyright © 2003 Pearson Education, Inc. Slide 11

The do-while loop is similar to the while loop, except that the test condition occurs at the end of the loop.The do-while loop is an exit-condition loop. This means that the body of the loop is always executed first. Then, the test condition is evaluated. If the test condition is TRUE, the program executes the body of the loop again. If the test condition is FALSE, the loop terminates and program execution continues with the statement following the while.

do-while Syntax

Page 12: 2621008 -  C++ 3

Slide 12

Example #1

do{cout << "Enter an integer between 2 and 174 (inclusive)"<< "that is a multiple of 3: ";cin >> input;} while (!(input >= 2 && input <= 174 && input%3 == 0));

Page 13: 2621008 -  C++ 3

Slide 13

Counting Loops

Page 14: 2621008 -  C++ 3

Slide 14

for Syntax

for (startExpression; testExpression; countExpression)}    statement }

Page 15: 2621008 -  C++ 3

Slide 15

• The startExpression is evaluated before the loop begins. It is acceptable to declare and assign in the startExpression(such as int x = 1;).

• This startExpression is evaluated only once at the beginning of the loop.

• The testExpression will evaluate to TRUE (nonzero) or FALSE (zero). While TRUE, the body of the loop repeats. When the testExpression becomes FALSE, the looping stops and the program continues with the statement immediately following the for loop body in the program code.

• The countExpression executes after each trip through the loop. The count may increase/decrease by an increment of 1 or of some other value.

• Braces are not required if the body of the for loop consists of only ONE statement. Please indent the body of the loop for readability.

for Syntax

Page 16: 2621008 -  C++ 3

Slide 16

Example #1

int max;float average;long sum = 0;short i = 0;cout << "enter positive integer: ";cin >> max;for(i=0; i <= max; i++)sum += i;average = sum/max;cout << "average is " << average << endl;

Page 17: 2621008 -  C++ 3

Slide 17

Goal Output* * * * * * * * ** * ** * *

Example #2

Page 18: 2621008 -  C++ 3

Slide 18

Outputting 5 Lines

for (int i = 1; i <= 5; i++) {  cout << endl;}

Page 19: 2621008 -  C++ 3

Slide 19

Outputting 5 Stars

for (int i = 1; i <= 5; i++) {  cout << “* ”;}

Page 20: 2621008 -  C++ 3

Slide 20

Combining

for (int i = 1; i <= 5; i++){   for (int j = 1; j <= 5; j++)        cout << "* ";                      

    cout << endl;}

output

i=1j=?

variable values

Page 21: 2621008 -  C++ 3

Slide 21

Combiningfor (int i = 1; i <= 5; i++){   for (int j = 1; j <= 5; j++)        cout << "* ";                      

    cout << endl;}

output

i=1j=?

variable values

Page 22: 2621008 -  C++ 3

Slide 22

Combining

for (int i = 1; i <= 5; i++){    for (int j = 1; j <= 5; j++)          cout << "* ";                      

    cout << endl;}

output

i=1j=1

variable values

Page 23: 2621008 -  C++ 3

Slide 23

Combining

for (int i = 1; i <= 5; i++){    for (int j = 1; j <= 5; j++)          cout << "* ";                      

    cout << endl;}

*

output

i=1j=1

variable values

Page 24: 2621008 -  C++ 3

Slide 24

Combining

for (int i = 1; i <= 5; i++){    for (int j = 1; j <= 5; j++)          cout << "* ";                      

    cout << endl;}

*

output

i=1j=2

variable values

Page 25: 2621008 -  C++ 3

Slide 25

Combining

for (int i = 1; i <= 5; i++){    for (int j = 1; j <= 5; j++)          cout << "* ";                      

    cout << endl;}

* *

output

i=1j=2

variable values

Page 26: 2621008 -  C++ 3

Slide 26

Combining

for (int i = 1; i <= 5; i++){    for (int j = 1; j <= 5; j++)          cout << "* ";                      

    cout << endl;}

* *

output

i=1j=3

variable values

Page 27: 2621008 -  C++ 3

Slide 27

Combining

for (int i = 1; i <= 5; i++){    for (int j = 1; j <= 5; j++)          cout << "* ";                      

    cout << endl;}

* * *

output

i=1j=3

variable values

Page 28: 2621008 -  C++ 3

Slide 28

Combining

for (int i = 1; i <= 5; i++){    for (int j = 1; j <= 5; j++)          cout << "* ";                      

    cout << endl;}

* * *

output

i=1j=4

variable values

Page 29: 2621008 -  C++ 3

Slide 29

Combining

for (int i = 1; i <= 5; i++){    for (int j = 1; j <= 5; j++)          cout << "* ";                      

    cout << endl;}

* * * *

output

i=1j=4

variable values

Page 30: 2621008 -  C++ 3

Slide 30

Combining

for (int i = 1; i <= 5; i++){    for (int j = 1; j <= 5; j++)          cout << "* ";                      

    cout << endl;}

* * * *

output

i=1j=5

variable values

Page 31: 2621008 -  C++ 3

Slide 31

Combining

for (int i = 1; i <= 5; i++){    for (int j = 1; j <= 5; j++)          cout << "* ";                      

    cout << endl;}

* * * * *

output

i=1j=5

variable values

Page 32: 2621008 -  C++ 3

Slide 32

Combining

for (int i = 1; i <= 5; i++){    for (int j = 1; j <= 5; j++)          cout << "* ";                      

    cout << endl;}

* * * * *

output

i=1j=6

variable values

Page 33: 2621008 -  C++ 3

Slide 33

Combining

for (int i = 1; i <= 5; i++){    for (int j = 1; j <= 5; j++)          cout << "* ";                      

    cout << endl;}

* * * * *

output

i=1j=6

variable values

Page 34: 2621008 -  C++ 3

Slide 34

Combining

for (int i = 1; i <= 5; i++){    for (int j = 1; j <= 5; j++)          cout << "* ";                      

    cout << endl;}

* * * * *

output

i=2j=?

variable values

Page 35: 2621008 -  C++ 3

Slide 35

Combining

for (int i = 1; i <= 5; i++){    for (int j = 1; j <= 5; j++)          cout << "* ";                      

    cout << endl;}

* * * * *

output

i=2j=1

variable values

Page 36: 2621008 -  C++ 3

Slide 36

Combining

for (int i = 1; i <= 5; i++){    for (int j = 1; j <= 5; j++)          cout << "* ";                      

    cout << endl;}

* * * * **

output

i=2j=1

variable values

Page 37: 2621008 -  C++ 3

Slide 37

Combining

for (int i = 1; i <= 5; i++){    for (int j = 1; j <= 5; j++)          cout << "* ";                      

    cout << endl;}

* * * * **

output

i=2j=2

variable values

Page 38: 2621008 -  C++ 3

Slide 38

Combining

for (int i = 1; i <= 5; i++){    for (int j = 1; j <= 5; j++)          cout << "* ";                      

    cout << endl;}

* * * * ** *

output

i=2j=2

variable values

Page 39: 2621008 -  C++ 3

Slide 39

Combining

for (int i = 1; i <= 5; i++){    for (int j = 1; j <= 5; j++)          cout << "* ";                      

    cout << endl;}

* * * * ** *

output

i=2j=3

variable values

Page 40: 2621008 -  C++ 3

Slide 40

Combining

for (int i = 1; i <= 5; i++){    for (int j = 1; j <= 5; j++)          cout << "* ";                      

    cout << endl;}

* * * * ** * *

output

i=2j=3

variable values

Page 41: 2621008 -  C++ 3

Slide 41

Combining

for (int i = 1; i <= 5; i++){    for (int j = 1; j <= 5; j++)          cout << "* ";                      

    cout << endl;}

* * * * ** * *

output

i=2j=4

variable values

Page 42: 2621008 -  C++ 3

Slide 42

Combining

for (int i = 1; i <= 5; i++){    for (int j = 1; j <= 5; j++)          cout << "* ";                      

    cout << endl;}

* * * * ** * * *

output

i=2j=4

variable values

Page 43: 2621008 -  C++ 3

Slide 43

Combining

for (int i = 1; i <= 5; i++){    for (int j = 1; j <= 5; j++)          cout << "* ";                      

    cout << endl;}

* * * * ** * * *

output

i=2j=5

variable values

Page 44: 2621008 -  C++ 3

Slide 44

Combining

for (int i = 1; i <= 5; i++){    for (int j = 1; j <= 5; j++)          cout << "* ";                      

    cout << endl;}

* * * * ** * * * *

output

i=2j=5

variable values

oops!!!

Page 45: 2621008 -  C++ 3

Slide 45

Goal vs Actual

Goal* * * * * * * * ** * ** * *

Actual* * * * * * * * * ** * * * ** * * * ** * * * *

j=1

j=2

j=3

j=4

j=5

i=1

i=2

i=3

i=4

i=5

Page 46: 2621008 -  C++ 3

Slide 46

Fixed

for (int i=1; i <= 5; i++){    for (int j=1; j <= 5 – i + 1; j++)          cout << "* ";                      

    cout << endl;}

output

i=1j=?

variable values

Page 47: 2621008 -  C++ 3

Slide 47

Fixed

for (int i=1; i <= 5; i++){    for (int j=1; j <= 5 – i + 1; j++)          cout << "* ";                      

    cout << endl;}

output

i=1j=1

variable values

Page 48: 2621008 -  C++ 3

Slide 48

Fixed

for (int i=1; i <= 5; i++){    for (int j=1; j <= 5 – i + 1; j++)          cout << "* ";                      

    cout << endl;}

*

output

i=1j=1

variable values

Page 49: 2621008 -  C++ 3

Slide 49

Fixed

for (int i=1; i <= 5; i++){    for (int j=1; j <= 5 – i + 1; j++)          cout << "* ";                      

    cout << endl;}

*

output

i=1j=2

variable values

Page 50: 2621008 -  C++ 3

Slide 50

Fixed

for (int i=1; i <= 5; i++){    for (int j=1; j <= 5 – i + 1; j++)          cout << "* ";                      

    cout << endl;}

* *

output

i=1j=2

variable values

Page 51: 2621008 -  C++ 3

Slide 51

Fixed

for (int i=1; i <= 5; i++){    for (int j=1; j <= 5 – i + 1; j++)          cout << "* ";                      

    cout << endl;}

* *

output

i=1j=3

variable values

Page 52: 2621008 -  C++ 3

Slide 52

Fixed

for (int i=1; i <= 5; i++){    for (int j=1; j <= 5 – i + 1; j++)          cout << "* ";                      

    cout << endl;}

* * *

output

i=1j=3

variable values

Page 53: 2621008 -  C++ 3

Slide 53

Fixed

for (int i=1; i <= 5; i++){    for (int j=1; j <= 5 – i + 1; j++)          cout << "* ";                      

    cout << endl;}

* * *

output

i=1j=4

variable values

Page 54: 2621008 -  C++ 3

Slide 54

Fixed

for (int i=1; i <= 5; i++){    for (int j=1; j <= 5 – i + 1; j++)          cout << "* ";                      

    cout << endl;}

* * * *

output

i=1j=4

variable values

Page 55: 2621008 -  C++ 3

Slide 55

Fixed

for (int i=1; i <= 5; i++){    for (int j=1; j <= 5 – i + 1; j++)          cout << "* ";                      

    cout << endl;}

* * * * *

output

i=1j=5

variable values

Page 56: 2621008 -  C++ 3

Slide 56

Fixed

for (int i=1; i <= 5; i++){    for (int j=1; j <= 5 – i + 1; j++)          cout << "* ";                      

    cout << endl;}

* * * * *

output

i=1j=6

variable values

Page 57: 2621008 -  C++ 3

Slide 57

Fixed

for (int i=1; i <= 5; i++){    for (int j=1; j <= 5 – i + 1; j++)          cout << "* ";                      

    cout << endl;}

* * * * *

output

i=1j=6

variable values

Page 58: 2621008 -  C++ 3

Slide 58

Fixed

for (int i=1; i <= 5; i++){    for (int j=1; j <= 5 – i + 1; j++)          cout << "* ";                      

    cout << endl;}

* * * * *

output

i=2j=?

variable values

Page 59: 2621008 -  C++ 3

Slide 59

Fixed

for (int i=1; i <= 5; i++){    for (int j=1; j <= 5 – i + 1; j++)          cout << "* ";                      

    cout << endl;}

* * * * *

output

i=2j=1

variable values

Page 60: 2621008 -  C++ 3

Slide 60

Fixed

for (int i=1; i <= 5; i++){    for (int j=1; j <= 5 – i + 1; j++)          cout << "* ";                      

    cout << endl;}

* * * * **

output

i=2j=1

variable values

Page 61: 2621008 -  C++ 3

Slide 61

Fixed

for (int i=1; i <= 5; i++){    for (int j=1; j <= 5 – i + 1; j++)          cout << "* ";                      

    cout << endl;}

* * * * **

output

i=2j=2

variable values

Page 62: 2621008 -  C++ 3

Slide 62

Fixed

for (int i=1; i <= 5; i++){    for (int j=1; j <= 5 – i + 1; j++)          cout << "* ";                      

    cout << endl;}

* * * * ** *

output

i=2j=2

variable values

Page 63: 2621008 -  C++ 3

Slide 63

Fixed

for (int i=1; i <= 5; i++){    for (int j=1; j <= 5 – i + 1; j++)          cout << "* ";                      

    cout << endl;}

* * * * ** *

output

i=2j=3

variable values

Page 64: 2621008 -  C++ 3

Slide 64

Fixed

for (int i=1; i <= 5; i++){    for (int j=1; j <= 5 – i + 1; j++)          cout << "* ";                      

    cout << endl;}

* * * * ** * *

output

i=2j=3

variable values

Page 65: 2621008 -  C++ 3

Slide 65

Fixed

for (int i=1; i <= 5; i++){    for (int j=1; j <= 5 – i + 1; j++)          cout << "* ";                      

    cout << endl;}

* * * * ** * *

output

i=2j=4

variable values

Page 66: 2621008 -  C++ 3

Slide 66

Fixed

for (int i=1; i <= 5; i++){    for (int j=1; j <= 5 – i + 1; j++)          cout << "* ";                      

    cout << endl;}

* * * * ** * * *

output

i=2j=4

variable values

Page 67: 2621008 -  C++ 3

Slide 67

Fixed

for (int i=1; i <= 5; i++){    for (int j=1; j <= 5 – i + 1; j++)          cout << "* ";                      

    cout << endl;}

* * * * ** * * *

output

i=2j=5

variable values

Page 68: 2621008 -  C++ 3

Slide 68

Fixed

for (int i=1; i <= 5; i++){    for (int j=1; j <= 5 – i + 1; j++)          cout << "* ";                      

    cout << endl;}

* * * * ** * * *

output

i=2j=5

variable values

Page 69: 2621008 -  C++ 3

Slide 69

Fixed

for (int i=1; i <= 5; i++){    for (int j=1; j <= 5 – i + 1; j++)          cout << "* ";                      

    cout << endl;}

* * * * ** * * *

output

i=3j=?

variable values

Page 70: 2621008 -  C++ 3

Slide 70

Fixed

for (int i=1; i <= 5; i++){    for (int j=1; j <= 5 – i + 1; j++)          cout << "* ";                      

    cout << endl;}

* * * * ** * * *

output

i=3j=1

variable values

Page 71: 2621008 -  C++ 3

Slide 71

Fixed

for (int i=1; i <= 5; i++){    for (int j=1; j <= 5 – i + 1; j++)          cout << "* ";                      

    cout << endl;}

* * * * ** * * **

output

i=3j=1

variable values

Page 72: 2621008 -  C++ 3

Slide 72

Fixed

for (int i=1; i <= 5; i++){    for (int j=1; j <= 5 – i + 1; j++)          cout << "* ";                      

    cout << endl;}

* * * * ** * * **

output

i=3j=2

variable values

Page 73: 2621008 -  C++ 3

Fixed

for (int i=1; i <= 5; i++){    for (int j=1; j <= 5 – i + 1; j++)          cout << "* ";                      

    cout << endl;}

* * * * ** * * ** *

output

i=3j=2

variable values

Page 74: 2621008 -  C++ 3

Slide 74

Fixed

for (int i=1; i <= 5; i++){    for (int j=1; j <= 5 – i + 1; j++)          cout << "* ";                      

    cout << endl;}

* * * * ** * * ** *

output

i=3j=3

variable values

Page 75: 2621008 -  C++ 3

Slide 75

Fixed

for (int i=1; i <= 5; i++){    for (int j=1; j <= 5 – i + 1; j++)          cout << "* ";                      

    cout << endl;}

* * * * ** * * ** * *

output

i=3j=3

variable values

Page 76: 2621008 -  C++ 3

Slide 76

Fixed

for (int i=1; i <= 5; i++){    for (int j=1; j <= 5 – i + 1; j++)          cout << "* ";                      

    cout << endl;}

* * * * ** * * ** * *

output

i=3j=4

variable values

Page 77: 2621008 -  C++ 3

Slide 77

Fixed

for (int i=1; i <= 5; i++){    for (int j=1; j <= 5 – i + 1; j++)          cout << "* ";                      

    cout << endl;}

* * * * ** * * ** * *

output

i=3j=4

variable values

Page 78: 2621008 -  C++ 3

Slide 78

Fixed

for (int i=1; i <= 5; i++){    for (int j=1; j <= 5 – i + 1; j++)          cout << "* ";                      

    cout << endl;}

* * * * ** * * ** * *

output

i=4j=?

variable values

Page 79: 2621008 -  C++ 3

Slide 79

Fixed

for (int i=1; i <= 5; i++){    for (int j=1; j <= 5 – i + 1; j++)          cout << "* ";                      

    cout << endl;}

* * * * ** * * ** * *

output

i=4j=1

variable values

Page 80: 2621008 -  C++ 3

Fixed

for (int i=1; i <= 5; i++){    for (int j=1; j <= 5 – i + 1; j++)          cout << "* ";                      

    cout << endl;}

* * * * ** * * ** * * *

output

i=4j=1

variable values

Page 81: 2621008 -  C++ 3

Slide 81

Fixed

for (int i=1; i <= 5; i++){    for (int j=1; j <= 5 – i + 1; j++)          cout << "* ";                      

    cout << endl;}

* * * * ** * * ** * * *

output

i=4j=2

variable values

Page 82: 2621008 -  C++ 3

Slide 82

Fixed

for (int i=1; i <= 5; i++){    for (int j=1; j <= 5 – i + 1; j++)          cout << "* ";                      

    cout << endl;}

* * * * ** * * ** * * * *

output

i=4j=2

variable values

Page 83: 2621008 -  C++ 3

Slide 83

Fixed

for (int i=1; i <= 5; i++){    for (int j=1; j <= 5 – i + 1; j++)          cout << "* ";                      

    cout << endl;}

* * * * ** * * ** * * * *

output

i=4j=3

variable values

Page 84: 2621008 -  C++ 3

Slide 84

Fixed

for (int i=1; i <= 5; i++){    for (int j=1; j <= 5 – i + 1; j++)          cout << "* ";                      

    cout << endl;}

* * * * ** * * ** * * * *

output

i=4j=3

variable values

Page 85: 2621008 -  C++ 3

Slide 85

Fixed

for (int i=1; i <= 5; i++){    for (int j=1; j <= 5 – i + 1; j++)          cout << "* ";                      

    cout << endl;}

* * * * ** * * ** * * * *

output

i=5j=?

variable values

Page 86: 2621008 -  C++ 3

Slide 86

Fixed

for (int i=1; i <= 5; i++){    for (int j=1; j <= 5 – i + 1; j++)          cout << "* ";                      

    cout << endl;}

* * * * ** * * ** * * * *

output

i=5j=1

variable values

Page 87: 2621008 -  C++ 3

Slide 87

Fixed

for (int i=1; i <= 5; i++){    for (int j=1; j <= 5 – i + 1; j++)          cout << "* ";                      

    cout << endl;}

* * * * ** * * ** * * * **

output

i=5j=1

variable values

Page 88: 2621008 -  C++ 3

Slide 88

Fixed

for (int i=1; i <= 5; i++){    for (int j=1; j <= 5 – i + 1; j++)          cout << "* ";                      

    cout << endl;}

* * * * ** * * ** * * * **

output

i=5j=2

variable values

Page 89: 2621008 -  C++ 3

Slide 89

Fixedfor (int i=1; i <= 5; i++){    for (int j=1; j <= 5 – i + 1; j++)          cout << "* ";                      

    cout << endl;}

* * * * ** * * ** * * * **

output

i=5j=2

variable values

Page 90: 2621008 -  C++ 3

Slide 90

Fixedfor (int i=1; i <= 5; i++){    for (int j=1; j <= 5 – i + 1; j++)          cout << "* ";                      

    cout << endl;}

* * * * ** * * ** * * * **

output

i=6j=?

variable values

Page 91: 2621008 -  C++ 3

Slide 91

Fixedfor (int i=1; i <= 5; i++){    for (int j=1; j <= 5 – i + 1; j++)          cout << "* ";                      

    cout << endl;}

* * * * ** * * ** * * * **

output

i=6j=?

variable values

Page 92: 2621008 -  C++ 3

Slide 92

* * * * * * * * * * * * * * *

Page 93: 2621008 -  C++ 3

Slide 93

Solution Rows

for (short i = 0; i < 5; i++){     for (short j = 1; j <= i; j++)        cout << " ";

    for (short j = i; j < 5; j++)        cout << "* ";

    cout << endl;}

* * * * *

* * * * * * * * *

*

Page 94: 2621008 -  C++ 3

Slide 94

Solution Spaces

for (short i = 0; i < 5; i++){     for (short j = 1; j <= i; j++)        cout << " ";

    for (short j = i; j < 5; j++)        cout << "* ";

    cout << endl;}

* * * * *

* * * * * * * * *

*

Page 95: 2621008 -  C++ 3

Slide 95

Solution Stars

for (short i = 0; i < 5; i++){     for (short j = 1; j <= i; j++)        cout << " ";

    for (short j = i; j < 5; j++)        cout << "* ";

    cout << endl;}

* * * * *

* * * * * * * * *

*

Page 96: 2621008 -  C++ 3

Slide 96

Random Number Generation

Page 97: 2621008 -  C++ 3

Slide 97

rand()

#include <iostream>

using namespace std;

int main()

{

  for(int i = 1; i <= 10; i++)

cout << rand() << endl;

return 0;

}

Page 98: 2621008 -  C++ 3

Slide 98

rand()#include <iostream>

using namespace std;

int main()

{

  for(int i = 1; i <= 10; i++)

cout << rand() << endl;

return 0;

}

3845773388773849487724339944523740082314611833432237844

First Execution

Page 99: 2621008 -  C++ 3

Slide 99

rand()#include <iostream>

using namespace std;

int main()

{

  for(int i = 1; i <= 10; i++)

cout << rand() << endl;

return 0;

}

3845773388773849487724339944523740082314611833432237844

First Execution

3845773388773849487724339944523740082314611833432237844

Second Execution

Page 100: 2621008 -  C++ 3

Slide 100

Pseudo-Random Generation

The numbers aren't actually random -- they're generated by a formula,

so they're just pseudorandom.

Page 101: 2621008 -  C++ 3

Slide 101

The pseudo-random number generator is initialized using the argument passed as seed.For every different seed value used in a call to srandsrand, the pseudo-random pseudo-random number generator number generator can be expected to generate a different succession of results in the subsequent calls to rand.

Two different initializations with the same seed will generate the same succession of results in subsequent calls to rand.

In order to generate random-like numbers, srand srand is usually initialized to some distinctive runtime value, like the value returned by function time (declared in header <ctime><ctime>). This is distinctive enough for most trivial randomization needs.

Page 102: 2621008 -  C++ 3

Slide 102

srand()

#include <iostream>#include <ctime> #include <cstdlib>using namespace std;int main(){ srand(time(NULL));  for(int i = 1; i <= 10; i++) cout << rand() << endl; return 0;}

Page 103: 2621008 -  C++ 3

Slide 103

srand()#include <iostream>#include <ctime>#include <cstdlib>using namespace std;int main(){ srand(time(NULL));  for(int i = 1; i <= 10; i++) cout << rand() << endl; return 0;}

46453735342236578889937237165742245778614

First Execution

Page 104: 2621008 -  C++ 3

Slide 104

srand()#include <iostream>#include <ctime>#include <cstdlib>using namespace std;int main(){ srand(time(NULL));  for(int i = 1; i <= 10; i++) cout << rand() << endl; return 0;}

46453735342236578889937237165742245778614

First Execution

6877245768215576187851179738346117511735257868

Second Execution

Page 105: 2621008 -  C++ 3

Slide 105

limiting range: [0, MAX_RAND]

#include <iostream>#include <ctime>#include <cstdlib>

using namespace std;int main(){  long next_value;  srand(time(NULL));  for (int i = 1; i <= 100000; i++)    if (rand()%100 <= 34)      cout << "hello" << endl; // ~35,000 times    else      cout << "goodbye" << endl; // ~65,000 times  return 0;}

Page 106: 2621008 -  C++ 3

Slide 106

#include <iostream>using namespace std;void main (){int x, y;double z;if(typeid(x) == typeid(y))cout << "x and y are of same type."<<endl;elsecout<< "x and y are of different type. " <<endl;cout << "The type of z is "<< typeid(z).name()<<endl;cout<< "The type of y is "<< typeid(y).name() << endl;}

typeid ()

The expected output of the program is given below.x and y are of same type.The type of z is doubleThe type of y is int

The operator typeid () identifies the type of variable. Here we only show the use of the operator. It returns reference to type of its argument. It is coded as:

typeid(object);

Page 107: 2621008 -  C++ 3

Slide 107


Recommended