Nested Loops, and Miscellaneous Loop Techniques - PowerPoint PPT Presentation

1 / 27
About This Presentation
Title:

Nested Loops, and Miscellaneous Loop Techniques

Description:

Nested Loops, and Miscellaneous Loop Techniques. Venkatesh Ramamoorthy. 16 ... the sides of a right-angled triangle, with c the length of the hypotenuse, then ... – PowerPoint PPT presentation

Number of Views:154
Avg rating:3.0/5.0
Slides: 28
Provided by: venkateshr
Category:

less

Transcript and Presenter's Notes

Title: Nested Loops, and Miscellaneous Loop Techniques


1
Nested Loops, and Miscellaneous Loop Techniques
  • Venkatesh Ramamoorthy
  • 16-March-2005

2
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

3
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)

4
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

5
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

6
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
7
Trace
  • 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

8
Pseudocode Nested Loops!!
  1. for a 1 to 100 in steps of 1, repeat
  2. for b 1 to 100 in steps of 1, repeat
  3. for c 1 to 100 in steps of 1, repeat
  4. if (a2 b2 equals c2) then
  5. Display a, b, c
  6. end-if
  7. end-for
  8. end-for
  9. end-for

9
More on Nesting Nested IFs
  • if (condition-1)
  • if (condition-2)
  • if (condition-3)
  • statement-set-1
  • else
  • statement-set-2
  • else
  • statement-set-3

10
More on NestingNested while-loops
  • while (condition-1)
  • statement-1
  • while (condition-2)
  • statement-2
  • for (index1 indexltn index)
  • statement-set-1

11
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?

12
Displaying 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!

13
Set 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)

14
The header files / namespaces to use for such
displaying
  • using stdfixed
  • include ltiomanipgt
  • using stdsetprecision
  • Note the above order in which these statements
    are used!

15
Example
  • 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

16
Example 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

17
Another 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

18
Example 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

19
An 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

20
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

21
Problem 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

22
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

23
Current and Next terms
  • Current term
  • Next term

24
The relation
  • Therefore,
  • In other words,

25
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/(n1)
  • Increment n by 1

26
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?

27
The Pseudo-code
  1. Input x and d
  2. Set current_term 1, sum 0
  3. Set n 1
  4. While (current_term is not equal to zero),
    repeat
  5. sum sum current_term
  6. current_term (current_term x) / (n 1)
  7. n n 1
  8. End-while
  9. Display sum
  10. Stop
Write a Comment
User Comments (0)
About PowerShow.com