270 likes | 423 Views
Nested Loops, and Miscellaneous Loop Techniques. Venkatesh Ramamoorthy 16-March-2005. Exercise – Theorem of Pythagoras. If a , b and c are the sides of a right-angled triangle, with c the length of the hypotenuse, then c 2 = a 2 + b 2 Some examples are: a = 3, b = 4, c = 5
E N D
Nested Loops, and Miscellaneous Loop Techniques Venkatesh Ramamoorthy 16-March-2005
Exercise – Theorem of Pythagoras • If a, b and c are the sides of a right-angled triangle, with c the length of the hypotenuse, then c2 = a2 + b2 • Some examples are: • a = 3, b = 4, c = 5 • a = 5, b = 12, c = 13 • a = 15, b = 8, c = 17 • a = 20, b = 21, c = 29
Pythagorean Triplets • The triplet (a, b, c) is called a Pythagorean Triplet • Examples • (3,4,5) • (5,12,13) • (15,8,17) • (20,21,29)
Goal • To enumerate the first several Pythagorean Triplets until a = 100, b = 100 and c = 100 • a = 3, b = 4, c = 5 • a = 4, b = 3, c = 5 • a = 5, b = 12, c = 13 • a = 6, b = 8, c = 10 • a = 7, b = 24, c = 25 • Etc, etc • a = 100,b = 100, c = 25
How? • Run through all the values of a from 1 through 100 • For each of those values of a, run through all the values of b from 1 through 100 • For each of those values of b, run through all the values of c from 1 through 100 • For all the above combinations of a, b and c, check if the following equation is satisfied: a2 + b2 = c2
Remember the 3-digit odometer? Hundreds Tens Ones 0 0 0 0 0 1 0 0 2 0 0 9 0 1 0 0 1 1 0 1 2 .. .. .. 0 9 0 0 9 1 .. .. .. 0 9 9 1 0 0 1 0 1 .. .. .. 9 9 9 In this odometer, the rightmost digit varies most frequently, followed by the middle, while the leftmost digit varies least frequently
Trace a b c Is a2+b2=c2? 1 1 1 No 1 1 2 No 1 1 3 No .. .. .. .. 1 1 100 No 1 2 1 No 1 2 2 No 1 2 3 No .. .. .. .. 1 2 100 No 1 3 1 No .. .. .. .. 1 3 100 No .. .. .. .. 1 100 100 No 2 1 1 No 2 1 2 No .. .. .. No 2 100 100 No 3 1 1 No .. .. .. .. 100 100 100 No
Pseudocode: Nested Loops!! • for a = 1 to 100 in steps of 1, repeat • for b = 1 to 100 in steps of 1, repeat • for c = 1 to 100 in steps of 1, repeat • if (a2 + b2 equals c2) then • Display a, b, c • end-if • end-for • end-for • end-for
More on Nesting – Nested IF’s if (condition-1) if (condition-2) if (condition-3) { statement-set-1 } else { statement-set-2 } } else { statement-set-3 } }
More on Nesting:Nested while-loops while (condition-1) { statement-1 ; while (condition-2) { statement-2 ; for (index=1; index<=n; index++) { statement-set-1 ; } } }
Exercise • Modify the Pythagorean Triplet program to print the first 100 triplets • Increment a counter whenever a triplet is found and displayed. • If the counter exceeds 100, abnormally terminate the loop. • Remember the break statement?
Displaying decimal values • Use std::fixed • To display float and double decimal numbers in fixed-point format • This is opposed to a scientific format • Use std::setprecision(d) • To display float and double decimal numbers correct to d decimal places • Note that these only display the number inside the variable in the specified manner • The variable still contains the original!
Set the floating-point number format • By using a separate cout, this can be done cout << fixed << setprecision(2) ; • You can also set to display in d decimal places, where d is an input • Ensure that d has been separately validated cout << fixed << setprecision(d) ;
The header files / namespaces to use for such displaying using std::fixed ; #include <iomanip> using std::setprecision ; • Note the above order in which these statements are used!
Example • What does the following program segment do? using namespace std::fixed ; #include <iomanip> using namespace std::setprecision ; double result ; result = 2 ; cout << fixed << setprecision(2) ; cout << result << “\n” ;
Example modified • Now I change result to 3.1415926 • What does the following program segment do? using namespace std::fixed ; #include <iomanip> using namespace std::setprecision ; double result ; result = 3.1415926 ; cout << fixed << setprecision(2) ; cout << result << “\n” ;
Another modification • Now I change the precision from 2 to 4! • What does the following program segment do? using namespace std::fixed ; #include <iomanip> using namespace std::setprecision ; double result ; result = 3.1415926 ; cout << fixed << setprecision(4) ; cout << result << “\n” ;
Example modified • Now I change the content of result! • What does the following program segment do? using namespace std::fixed ; #include <iomanip> using namespace std::setprecision ; double result ; result = 3.14 ; cout << fixed << setprecision(4) ; cout << result << “\n” ;
An interesting problem • What does the following program segment do? using namespace std::fixed ; #include <iomanip> using namespace std::setprecision ; double term1, term2, sum ; cout << fixed << setprecision(2) ; term1 = 14.275 ; cout << “term1 = ” << term1 << “\n” ; term2 = 18.675 ; cout << “term2 = ” << term2 << “\n” ; result = term1 + term2 ; cout << result << “\n” ;
Loops – Summation of Infinite Series • Using the infinite series below, determine the value of ex for an input number x correct to d decimal places, where d is another input
Problem Analysis • Are we repeatedly going to compute xn and the factorial of n, and use them in each term? • Certainly not! • Here’s where the following technique is very useful
Problem analysis • To sum such series, • Always try to relate the previous term with the current term • Or, always try to relate the current term with the next term • In other words, try to relate two consecutive terms
Current and Next terms • Current term • Next term
The relation • Therefore, • In other words,
The loop • Repeatedly keep on accumulating the current term into a variable “sum” • Then, determine the “next term” from the current term • By multiplying the current term with x/(n+1) • Increment n by 1
When will the loop terminate? • When the current term becomes zero! • Why? • What should be the value of the current term for the first-time?
The Pseudo-code • Input x and d • Set current_term = 1, sum = 0 • Set n = 1 • While (current_term is not equal to zero), repeat: • sum = sum + current_term • current_term = (current_term * x) / (n + 1) • n = n + 1 • End-while • Display sum • Stop