Title: Nested Loops, and Miscellaneous Loop Techniques
1Nested Loops, and Miscellaneous Loop Techniques
- Venkatesh Ramamoorthy
- 16-March-2005
2Exercise 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
3Pythagorean Triplets
- The triplet (a, b, c) is called a Pythagorean
Triplet - Examples
- (3,4,5)
- (5,12,13)
- (15,8,17)
- (20,21,29)
4Goal
- 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
5How?
- 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
6Remember 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
7Trace
- a b c Is a2b2c2?
- 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
8Pseudocode 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
9More on Nesting Nested IFs
- if (condition-1)
- if (condition-2)
- if (condition-3)
- statement-set-1
-
- else
- statement-set-2
-
-
- else
- statement-set-3
-
10More on NestingNested while-loops
- while (condition-1)
- statement-1
- while (condition-2)
- statement-2
- for (index1 indexltn index)
- statement-set-1
-
-
11Exercise
- 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?
12Displaying decimal values
- Use stdfixed
- To display float and double decimal numbers in
fixed-point format - This is opposed to a scientific format
- Use stdsetprecision(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!
13Set the floating-point number format
- By using a separate cout, this can be done
- cout ltlt fixed ltlt 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 ltlt fixed ltlt setprecision(d)
14The header files / namespaces to use for such
displaying
- using stdfixed
- include ltiomanipgt
- using stdsetprecision
- Note the above order in which these statements
are used!
15Example
- What does the following program segment do?
- using namespace stdfixed
- include ltiomanipgt
- using namespace stdsetprecision
- double result
- result 2
- cout ltlt fixed ltlt setprecision(2)
- cout ltlt result ltlt \n
16Example modified
- Now I change result to 3.1415926
- What does the following program segment do?
- using namespace stdfixed
- include ltiomanipgt
- using namespace stdsetprecision
- double result
- result 3.1415926
- cout ltlt fixed ltlt setprecision(2)
- cout ltlt result ltlt \n
17Another modification
- Now I change the precision from 2 to 4!
- What does the following program segment do?
- using namespace stdfixed
- include ltiomanipgt
- using namespace stdsetprecision
- double result
- result 3.1415926
- cout ltlt fixed ltlt setprecision(4)
- cout ltlt result ltlt \n
18Example modified
- Now I change the content of result!
- What does the following program segment do?
- using namespace stdfixed
- include ltiomanipgt
- using namespace stdsetprecision
- double result
- result 3.14
- cout ltlt fixed ltlt setprecision(4)
- cout ltlt result ltlt \n
19An interesting problem
- What does the following program segment do?
- using namespace stdfixed
- include ltiomanipgt
- using namespace stdsetprecision
- double term1, term2, sum
- cout ltlt fixed ltlt setprecision(2)
- term1 14.275
- cout ltlt term1 ltlt term1 ltlt \n
- term2 18.675
- cout ltlt term2 ltlt term2 ltlt \n
- result term1 term2
- cout ltlt result ltlt \n
20Loops 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
21Problem Analysis
- Are we repeatedly going to compute xn and the
factorial of n, and use them in each term? - Certainly not!
- Heres where the following technique is very
useful
22Problem 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
23Current and Next terms
24The relation
25The 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/(n1)
- Increment n by 1
26When will the loop terminate?
- When the current term becomes zero!
- Why?
- What should be the value of the current term for
the first-time?
27The 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