Notes
Outline
COM1100
Fundamentals of Computer Science –Winter 2000
Lecture 9
01/27/99
Previous lectures
More on function
Pass parameters by reference
Flow of control
Repetition – for statement
Format output
Today’s lecture
Flow of control
Repetition – for statement
Nested loop
Random Number generating
Quiz
Practice Questions
Nested Loop
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
Nested loop –
First Example
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
For each i, j loop
Nested loop –
Another Example
*
* *
* * *
* * * *
* * * * *
One counting variable (i) to record how many lines
Another counting variable (j) to record how many stars in each line
Today’s lecture
Flow of control
Repetition – for statement
Nested loop
Random Number generating
Quiz
Practice Questions
Why random number?
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.
No perfect random number
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.
C++ way of generating random
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.
Seed Selection
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
Scaling -- 1
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;
Scaling -- 2
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
Random generation with CoreTools
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
Random generation with CoreTools -- Examples
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;
}
Today’s lecture
Flow of control
Repetition – for statement
Nested loop
Random Number generating
Quiz
Practice Questions
Quiz  -- 3th, Feb. (Thursday)
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
Quiz – Part 1
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
Quiz – Part 2
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
Quiz – Part 3
Part 3: Function
Purpose : handle function
Involves :
Function signature (declaration)
Function definition (implementation)
Function call (use)
Only pass parameters by value
Practice Question 1–
Simple computation
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;
}
Practice Question 2–
Simple programming
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.
Practice Question 3–
Function
Write a function that will take two double arguments and will return the minimum value of the two numbers.
Today’s lecture
Flow of control
Repetition – for statement
Nested loop
Random Number generating
Quiz
Practice Questions
Lecture next week
Flow of control
Repetition – while statement
Quiz during lecture time
Email me in advance if you can not make it