Download presentation

Presentation is loading. Please wait.

Published byChristopher Roche Modified over 4 years ago

1
Lecture 1 -- 1Computer Science I - Martin Hardwick How do Calculators Computer Square Roots? rWhen you enter 29 into your calculator and push the square root button, how does the calculator compute ? r Consider the function x 2 – 29 = 0. l it crosses the x-axis when x = l so, to compute the we need to compute exactly where the function x 2 – 29 crosses the x-axis r We will use an iterative method l we know the function crosses the x -axis somewhere between 0 and 29 l compute the midpoint of the interval 0 to 29 l decide whether the function crosses the x -axis before the midpoint or after the midpoint l repeat for the half of the interval in which the function crosses the x -axis l do this until the interval is very small (0.00000001) – the midpoint of the final interval is an approximation to the square root of 29

2
Lecture 1 -- 2Computer Science I - Martin Hardwick Computing Square Roots (1) #include using namespace std; double f(double x, double num) { return (x*x - num); } rGoal – find the square root of a number using the iterative Bisection Method. l root is where x 2 - number = 0 rWe need a function that computes x 2 – number every time that we need it l this is the job of function f(x) rFunction f(x): l has two arguments, which are type double l produces a result which is type double

3
Lecture 1 -- 3Computer Science I - Martin Hardwick Computing Square Roots (2) int main () { // number to find root of doublenum; // end points for search doubleleft, right; // mid point of interval doublemidpoint; // error bound doubleepsilon; // size of interval doubleinterval; // flag to terminate iteration booldone; // Get number to find root of cout << "Enter number to find "; cout << "square root of: "; cin >> num; // Initialize for iteration left = 0.0; right = num; epsilon = 5E-11; //calculator has 10 digits rSearch for a root between left and right end points that keep moving closer together. rSince the root is always within the interval, when the size of the interval gets sufficient small (i.e., less than epsilon) we can stop. root left right interval size

4
Lecture 1 -- 4Computer Science I - Martin Hardwick Root Finding (2) // Initialize for the iteration loop interval = right - left; done = false; // Iterate to find square root while ((interval > epsilon) && !done) { midpoint = (left + right)/2; if (f(midpoint, num) == 0.0) { done = true; } else if (f(midpoint, num)*f(left, num) < 0.0) { right = midpoint; } else { left = midpoint; } interval = right – left; } // Display square root that was found cout << Root is: " << midpoint << endl; return 0; } rCompute the midpoint of the current interval. l if the midpoint is not the root, then the root is either in the left half or the right half of the original interval l if f(midpoint) and f(left) have different signs, then the root is in the left half left rightmidpoint f(midpoint) f(left)

5
Lecture 1 -- 5Computer Science I - Martin Hardwick While Loops rAlternative to the For loop for situations when you do not know before the loop begins how many iterations of the loop will be needed. rSyntax: while (condition) { statement; … } body of loop any valid logical expression with relational and logic operators condition statement … true start done false

6
Lecture 1 -- 6Computer Science I - Martin Hardwick What Is Displayed By Each Code Segment ? pWhat is displayed when the user types the numbers 1, 2, …, 10 ? cin >> num; while (num < 10) { cout << num*num << endl; cin >> num; } rWhat is displayed when the user types the numbers 2, 4, 6, 0 cin >> num; while (num > 0) { for (i = 1; i <= num; i++) { cout << num; } cout << endl; cin >> num; } 1 4 9 16 25 36 49 64 81 22 4444 666666

7
Lecture 1 -- 7Computer Science I - Martin Hardwick Infinite Loops rConsider the following loop; what happens when it is executed? // sum the odd numbers up to 100 sum = 0; n = 1; while (n != 100) { sum = sum + n; n = n + 2; } This is an example of an infinite loop that never terminates or terminates only when an error condition occurs. click on the window close button (i.e., the button in the upper right corner of a window with an x in it) Rule of Thumb: dont use != conditions in a while loop, use, = instead.

8
Lecture 1 -- 8Computer Science I - Martin Hardwick For loop or While loop? rWe do not need both types of loops rThis For loop for(expr1 ; expr2 ; expr3) {... } rTranslates into this while loop expr1; while (expr2) { {… } expr3; } rWhat matters is writing code that is clear and easy to read. rSo we use a For loop when the limit is relatively fixed (e.g 5 or N) rAnd we use a While loop when it depends on convergence, or data entered by the user

9
Lecture 1 -- 9Computer Science I - Martin Hardwick Debugging rThe square root program is quite complex – suppose it is wrong rHow do we debug rMethod 1 l Add code to the program to print out values as it executes l This is a good way to look at trends in the code l You can look at the values and try to find something unexpected l If you are quick you can start and stop the code using ctrl s and ctrl q rMethod 2 l Use the debugger l Put break points into the code l We will look at the debugger next week

10
Lecture 1 -- 10Computer Science I - Martin Hardwick while ((interval > epsilon) && !done) { midpoint = (left + right)/2; if (f(midpoint, num) == 0.0) { done = true; } else if (f(midpoint, num)*f(left, num) < 0.0) { right = midpoint; } else { left = midpoint; } interval = right – left; // debugging code cout << left = << left << endl; cout << right = << right << endl; cout << interval = << interval << endl; } Debugging – Method 1 This debugging method is simple but effective Works with any compiler and machine Programmers often leave the debugging code in the program and comment it out while it is not being used. Be careful because bad or sloppy debugging code can waste time Finding the wrong errors Finding errors in the debug code itself Sloppy debugging code will make other programmers distrust your programs. Good debugging code helps someone else understand your program

Similar presentations

OK

Computer Science 1620 Math Library. Remember this program? suppose that I invest $25000 into a mutual fund that returns 8% per year. Write a program to.

Computer Science 1620 Math Library. Remember this program? suppose that I invest $25000 into a mutual fund that returns 8% per year. Write a program to.

© 2018 SlidePlayer.com Inc.

All rights reserved.

To make this website work, we log user data and share it with processors. To use this website, you must agree to our Privacy Policy, including cookie policy.

Ads by Google