Upcoming SlideShare
Loading in...5
×

Like this presentation? Why not share!

# Ch04 final ismail

## on Jun 13, 2012

• 348 views

### Views

Total Views
348
Views on SlideShare
313
Embed Views
35

Likes
0
Downloads
13
Comments
0

### 2 Embeds35

 http://cs215.wikispaces.com 24 https://cs215.wikispaces.com 11

### Upload Details

Uploaded via as Microsoft PowerPoint

### Usage Rights

© All Rights Reserved

### Report content

• Comment goes here.
Are you sure you want to
Your message goes here
Edit your comment

## Ch04 final ismailPresentation Transcript

• Chapter Four: Loops C++ for Everyone by Cay HorstmannSlides by Evan Gallagher Copyright © 2012 by John Wiley & Sons. All rights reserved
• The Three Loops in C++ C++ has these three looping statements: while for do C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved
• The while Loop while (condition) { statements } The condition is some kind of test (the same as it was in the if statement) C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved
• The while Loop while (condition) { statements } The statements are repeatedly executed until the condition is false C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved
• Using a Loop to Solve the Investment Problem. Here is the complete while statement: while (balance < TARGET) { year++; double interest = balance * RATE / 100; balance = balance + interest; } C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved
• Flowchart of the Investment Calculation’s while Loop C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved
• Example of Normal Execution while loop to hand-trace What is the output? i = 5; while (i > 0) { cout << i << " "; i--; } C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved
• When i is 0, the Loop Condition is false, and the LoopEnds while loop The output i = 5; 5 4 3 2 1 while (i > 0) { cout << i << " "; i--; } C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved
• Example of a Problem – An Infinite Loop while loop to hand-trace What is the output? i = 5; while (i > 0) { cout << i << " "; i++; } C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved
• A Very Difficult Error to Find (especially after looking for it for hours and hours!) while loop to hand-trace What is the output? i = 5; while (i > 0); { cout << i << " "; i--; } C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved
• A Very Difficult Error to Find (especially after looking for it for hours and hours!) Another infinite loop – caused by a single character: ; That semicolon causes the while loop to have an “empty body” which is executed forever. The i in (i > 0) is never changed. while loop There is no output! i = 5; while (i > 0); { cout << i << " "; i--; } C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved
• Common Error – Infinite Loops • Forgetting to update the variable used in the condition is common. • In the investment program, it might look like this. year = 1; while (year <= 20) { balance = balance * (1 + RATE / 100); } • The variable year is not updated in the body C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved
• The for Loop Often you will need to execute a sequence of statements a given number of times. You could use a while loop for this. counter = 1; // Initialize the counter while (counter <= 10) // Check the counter { cout << counter << endl; counter++; // Update the counter } C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved
• The for Loop C++ has a statement custom made for this sort of processing: the for loop. for (counter = 1; counter <= 10; counter++) { cout << counter << endl; } C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved
• The for Loop Is Better than while for Doing Certain Things Doing something a certain number of times or causing a variable to take on a sequence of values is so common, C++ has a statement just for that: for (int count = 1; count <= 10; count++) { cout << count << endl; } initialization condition statements update C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved
• The for Loop for (initialization; condition; update) { statements } C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved
• The for Statement The variable i is defined only in this for loop. C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved
• Example of Normal Execution for loop to hand-trace What is the output? for (int i = 0; i <= 5; i++) cout << i << " "; C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved
• Example of Normal Execution for loop The output for (int i = 0; 0 1 2 3 4 5 i <= 5; i++) cout << i << " "; Note that the output statement is executed six times, not five C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved
• Example of Normal Execution – Going in the Other Direction for loop to hand-trace What is the output? for (int i = 5; i <= 0; i--) cout << i << " "; C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved
• Example of Normal Execution – Going in the Other Direction Again six executions of the output statement occur. for loop The output for (int i = 5; 5 4 3 2 1 0 i <= 0; i--) cout << i << " "; C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved
• Infinite Loops Can Occur in for Statements The danger of using == and/or != for loop to hand-trace What is the output? for (int i = 0; i != 9; i += 2) cout << i << " "; C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved
• Infinite Loops Can Occur in for Statements = = and != are best avoided in the check of a for statement for loop The output never ends for (int i = 0; 0 2 4 6 8 10 12… i != 9; i += 2) cout << i << " "; C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved
• Example of Normal Execution – Taking Even Bigger Steps for loop to hand-trace What is the output? for (int i = 1; i <= 20; i *= 2) cout << i << " "; The update can be any expression C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved
• Example of Normal Execution – Taking Even Bigger Steps for loop The output for (int i = 1; 1 2 4 8 16 i <= 20; i *= 2) cout << i << " "; The “step” can be multiplicative or any valid expression C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved
• The do Loop The while loop’s condition test is the first thing that occurs in its execution. The do loop (or do-while loop) has its condition tested only after at least one execution of the statements. do { statements } while (condition); C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved
• More Nested Loop Examples The loop variables can have a value relationship. In this example the inner loop depends on the value of the outer loop. for (i = 1; i <= 4; i++) for (j = 1; j <= i; j++) cout << "*"; cout << endl; The output will be: * ** *** **** C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved
• The rand Function The C++ library has a random number generator: rand() C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved
• Seeding the rand Function You can “seed” the random generator to indicate where it should start in the pseudorandom sequence Calling srand sets where rand starts srand is defined in the cstdlib header C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved
• Seeding the rand Function Calling srand sets where rand starts. Calling time(0) gets the current time. So, to set up for “really, really random” random numbers on each program run: srand(time(0)); // seed rand() (Well, as “really random” as we can hope for.) C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved
• Modeling Using the rand Function (b - a + 1) a b Given two endpoints, a and b, recall there are (b - a + 1) values between a and b, (including the bounds themselves). C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved
• Modeling Using the rand Function (b - a + 1) a b rand() % (b - a + 1) Obtain a random value between 0 and b – a by using the rand() function C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved
• Modeling Using the rand Function (b - a + 1) a b Start at a C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved
• Modeling Using the rand Function (b - a + 1) a b rand() % (b - a + 1) Add that random value to a and you have:int d = rand() % (b - a + 1) + a; C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved
• Modeling Using the rand Function (b - a + 1) a b rand() % (b - a + 1) a random value in the range.int d = rand() % (b - a + 1) + a; C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved
• Modeling Using the rand Function Using 1 and 6 as the bounds and modeling for two dice, running for 10 tries, we have: C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved
• Modeling Using the rand Function #include <iostream> #include <string> ch04/dice.cpp #include <cstdlib> #include <ctime> using namespace std; One of many different int main() Program Runs: 5 1 { 2 1 srand(time(0)); 1 2 5 1 for (i = 1; i <= 10; i++) 1 2 { 6 4 int d1 = rand() % 6 + 1; 4 4 int d2 = rand() % 6 + 1; 6 1 cout << d1 << " " << d2 << endl; 6 3 } 5 2 cout << endl; return 0; } C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved
• The Monte Carlo Method The Monte Carlo method is a method for finding approximate solutions to problems that cannot be π precisely solved. Here is an example: compute This is difficult. C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved
• The Monte Carlo Method Consider placing the round dartboard inside an exactly fitting square π? C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved
• The Monte Carlo Method As we toss darts at the target, if we are able to just hit the target – at all – it’s a hit. π? (no wonder this is such a pathetic casino) C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved
• The Monte Carlo Method and a miss is a miss. π? C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved
• The Monte Carlo Method The (x,y) coordinate of a hit is when (x2 + y2) ≤ 1. In code: if (x * x + y * y <= 1) { hits++; } C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved
• The Monte Carlo Method Our coded random shots will give a ratio of hits/tries that is approximately equal to the ratio of the areas of the circle and the square: π /4 C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved
• The Monte Carlo Method Multiply by 4 and we have an estimate for π ! π = 4 * hits/tries; The longer we run our program, the more random numbers we generate, the better the estimate. C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved
• The Monte Carlo Method For the x and y coordinates within the circle, we need random x and y values between -1 and 1. That’s a range of (-1 + 1 + 1) or 2. As before, we want to add some random portion of this range to the low endpoint, -1. But we will want a floating point value, not an integer. C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved
• The Monte Carlo Method We must use rand with double values to obtain that random portion. double r = rand() * 1.0 / RAND_MAX; The value r is a random floating-point value between 0 and 1. You can think of this as a percentage if you like. (Use 1.0 to make the / operator not do integer division) C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved
• The Monte Carlo Method The computation: double x = -1 + 2 * r; 2 is the length of the range from -1 to 1 C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved
• The Monte Carlo Method The computation: double x = -1 + 2 * r; 2 is the length of the range from -1 to 1 r is some random value between 0.0 and 1.0 C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved
• The Monte Carlo Method The computation: double x = -1 + 2 * r; 2 is the length of the range from -1 to 1 r is some random value between 0.0 and 1.0 so (2 * r) is some portion of that range C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved
• The Monte Carlo Method The computation: double x = -1 + 2 * r; 2 is the length of the range from -1 to 1 r is some random value between 0.0 and 1.0 so (2 * r) is some portion of that range We will add this portion to the left hand end of the range, -1 C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved
• The Monte Carlo Method The computation: double x = -1 + 2 * r; 2 is the length of the range from -1 to 1 r is some random value between 0.0 and 1.0 so (2 * r) is some portion of that range Adding this portion to the left hand end of the range gives us: x randomly within the range -1 and 1. C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved
• The Monte Carlo Method for Approximating PI #include <iostream> #include <cstdlib> #include <cmath> ch04/montecarlo.cpp #include <ctime> using namespace std; int main() { const int TRIES = 10000; srand(time(0)); int hits = 0; for (int i = 1; i <= TRIES; i++) { double r = rand() * 1.0 / RAND_MAX; // Between 0 and 1 double x = -1 + 2 * r; // Between –1 and 1 r = rand() * 1.0 / RAND_MAX; double y = -1 + 2 * r; if (x * x + y * y <= 1) { hits++; } } double pi_estimate = 4.0 * hits / TRIES; cout << "Estimate for pi: “ << pi_estimate << endl; return 0; C++ for Everyone by Cay Horstmann } Copyright © 2012 by John Wiley & Sons. All rights reserved
• Chapter Summary C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved
• Chapter Summary C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved
• Chapter Summary C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved
• End Chapter Four C++ for Everyone by Cay HorstmannSlides by Evan Gallagher Copyright © 2012 by John Wiley & Sons. All rights reserved