|
|
|
|
|
More on function |
|
Pass parameters by reference |
|
Flow of control |
|
Repetition – for statement |
|
Format output |
|
|
|
|
|
|
|
|
Flow of control |
|
Repetition – for statement |
|
Nested loop |
|
Random Number generating |
|
Quiz |
|
Practice Questions |
|
|
|
|
|
|
|
In many situations, we need to use a loop
contained within another loop |
|
Such loops are called nested loop. |
|
Usefulness |
|
2-dimensional array |
|
Compute the average grade for each student in a
class of 20 students. Each student has taken 3 exams during a quarter. |
|
Print 2-dimensional pattern |
|
|
|
|
|
|
|
|
for (int i = 1; i < 4; i++) { //
start of outer loop |
|
cout << "\n\n i is now :
" << i << endl; |
|
for (int j = 1; j < 5; j++) //
start of inner loop |
|
cout << " j = " << j; // end of inner loop |
|
} // end of outer
loop |
|
The first loop, controlled by i, is called outer
loop |
|
The second loop, controlled by j, is called
inner loop |
|
|
|
All statements in the inner loop are contained
the boundaries of the outer loop and we use a different variable for each
loop |
|
For each single trip through the outer loop, the
inner loop runs through its entire sequence. |
|
In other words, each time the i counter
increases by 1, the inner for loop executes completely |
|
|
|
|
|
* |
|
* * |
|
* * * |
|
* * * * |
|
* * * * * |
|
One counting variable (i) to record how many
lines |
|
Another counting variable (j) to record how many
stars in each line |
|
|
|
|
|
|
|
|
Flow of control |
|
Repetition – for statement |
|
Nested loop |
|
Random Number generating |
|
Quiz |
|
Practice Questions |
|
|
|
|
|
|
|
There are many mathematical and simulation
problem in which probability must be considered or statistical sampling
techniques must be used. |
|
All of these statistical models requires the
generation of random numbers |
|
A series of numbers whose order cannot be
predicted. |
|
|
|
|
|
|
|
|
|
In practice, there are no truly random numbers. |
|
Dice are never perfect |
|
Cards are never shuffled completely randomly |
|
Computer can handle numbers only within a finite
range and with limited precision. |
|
The best one can do is : generate pseudorandom
numbers, which are sufficiently random for the task at hand. |
|
|
|
|
|
The functions provided by C++ are: |
|
rand() for generating random numbers, and |
|
srand() for setting initial random “seed”
values. |
|
The srand() function sets the starting point for
generating a series of pseudorandom integers. |
|
If srand() or some other equivalent “seeding”
techniques is not used, rand() will always produce the same series of
random number. |
|
The rand() function produces a series of random
numbers in the range of 0£ rand()£ RAND_MAX |
|
RAND_MAX is defined in the stdlib.h header file. |
|
|
|
|
|
srand(time(NULL)); |
|
The argument to the srand() is a call to the time()
with a NULL argument. |
|
With this argument, time() reads the computer’s
internal clock time in seconds. |
|
srand() then uses this time, converted to an
unsigned integer to initialize the random number generator function rand(). |
|
Random numbers will differ each time the program
is executed |
|
Computer time is different |
|
|
|
|
|
The method for adjusting the random number
produced by a random number generator to reside within a specified range is
called scaling. |
|
Scaling random numbers to reside within the
range 0.0 to 1.0 is easily accomplished by dividing the return value of
rand() by RAND_MAX. |
|
(rand()*1.0)/RAND_MAX |
|
cout << setw(10) <<
(rand()*1.0)/RAND_MAX; |
|
|
|
|
|
To produce a random integer between the numbers
a and b |
|
a + rand() % (b + 1 - a) |
|
To produce a random integer between the numbers
0 and N (where a = 0, b = N) |
|
rand() % (N + 1) |
|
To produce a random integer between the numbers
1 and N (where a = 1, b = N) |
|
1 + rand() % N |
|
|
|
|
|
|
|
SetRandomSeed() |
|
sets the seed for random number generation |
|
the seed is chosen based on an internal clock
value |
|
long RandomLong(long a, long b); |
|
returns a random integer in the range a .. b |
|
a and b are included |
|
double RandomDouble(double a, double b); |
|
returns a random double in the range a .. B |
|
a and b are included |
|
|
|
|
cout << setiosflags(ios::fixed) <<
setprecision(3); |
|
SetRandomSeed(); |
|
cout << "\n 5 random numbers between
0 and 1: " << endl; |
|
for (int i = 0; i < 5; i++) { |
|
cout
<< setw(5) << RandomDouble(0.0, 1.0) << endl; |
|
} |
|
cout << "\n 5 random numbers between
10 and 100: " << endl; |
|
for (i = 0; i < 5; i++) { |
|
cout << setw(5) << RandomLong(10,
100) << endl; |
|
} |
|
|
|
|
|
|
Flow of control |
|
Repetition – for statement |
|
Nested loop |
|
Random Number generating |
|
Quiz |
|
Practice Questions |
|
|
|
|
|
|
It will be 30 ~ 45 min. in class test (1:50PM ~
2:35PM) |
|
Close book, notes and packages |
|
|
|
Part 1: Simple computation |
|
Part 2: Simple programming |
|
Part 3: Function |
|
|
|
No parameters passed by reference |
|
No formatted output |
|
No C++ random number generation |
|
|
|
|
|
|
Part 1: Simple computation |
|
Purpose : Understand program |
|
Involves : |
|
Variable declaration, initialization, assignment
and use |
|
Simple computation: the order of evaluating |
|
Simple cout statement |
|
Increment/decrement operators (++ / --) |
|
if statement |
|
for loop |
|
|
|
|
|
|
|
|
|
|
Part 2: Simple programming |
|
Purpose : Write program |
|
Involves : |
|
Variable declaration, initialization and use |
|
Basic data type : int, double |
|
Simple cin statement |
|
Simple graphic functions |
|
SetForeColor(R, G, B); |
|
White, Black, Red, Green, Blue |
|
FrameRect(x1,y1,x2,y2); |
|
PaintRect(x1,y1,x2,y2); |
|
FrameCircle(x1,y1,r); |
|
PaintCircle(x1,y1,r); |
|
if statement |
|
|
|
|
|
|
Part 3: Function |
|
Purpose : handle function |
|
Involves : |
|
Function signature (declaration) |
|
Function definition (implementation) |
|
Function call (use) |
|
Only pass parameters by value |
|
|
|
|
|
|
int main() { |
|
int x = 7; // variable
declaration and initialization |
|
int y = 23; // variable
declaration and initialization |
|
y = x + y; |
|
cout << "x=" << x
<< " y =" << y << endl; |
|
x = y / x; |
|
cout << "x=" << x
<< " y =" << y << endl; |
|
x = 4; // variable assignment |
|
y = 23; // variable assignment |
|
cout << "y % x = "
<< y % x << endl; |
|
cout << "x= " << x
<< " y=" << y << endl; |
|
return 0; |
|
} |
|
|
|
|
Read (x1,y1) and (x2,y2) the coordinates of the
two opposite corners of a rectangle. Paint the rectangle in black and
invert an oval inside the rectangle boundaries in red. |
|
|
|
|
Write a function that will take two double
arguments and will return the minimum value of the two numbers. |
|
|
|
|
|
|
Flow of control |
|
Repetition – for statement |
|
Nested loop |
|
Random Number generating |
|
Quiz |
|
Practice Questions |
|
|
|
|
|
|
|
Flow of control |
|
Repetition – while statement |
|
Quiz during lecture time |
|
Email me in advance if you can not make it |
|
|
|
|
|
|
|