LOOPS IN ALGORITHMS Computers are particularly well suited to applications in which, operations are repeated many times.
. If the same task is repeated over and over again a loop can be used to reduce program size and complexity Example 8: Write an algorithm and draw a flowchart to calculate 24 . Algorithm: Step Step 2: Step 3: Step 4: Step 5: Step 6: Flowchart: 1: Input Base (2), Power (4) Product= Base Product = Product * Base Product = Product * Base Product = Product * Base Print Product
START Input Base (2),Power (4)power(4)
Product = Base Product = Product * Base Product = Product * Base Product = Product * Base
Print Product
STOP
1
Question: What happens if you want to calculate 2 to the power of 1000 (21000) Answer: Use a LOOP (repeated execution of the same set of instructions)
Example 9: Write an algorithm and draw a flowchart to calculate 24 using a loop approach? Verify your result by a trace table. Algorithm: Flowchart:
START
Base
Input
Base(2), Power(4)
Product =Base Counter=1
Step 1: Input Base (2), Power (4) Step 2: Product= Base Step 3: Counter = 1 Step 4: While(Counter<Power) Repeat steps 4 through 6 Step 5: Product = Product * Step 6: Step 7: Counter = Counter + 1 Print Product
is Count er < Power
Y Product = Product * Base Print Product
Counter = Counter + 1
STOP
Trace Table of Example 9: Bas Powe Product Counte e r r 2 4 2 1
2
Decisio n Y
Print -
4 8 16
2 3 4
Y Y N
16
Example 10: Write down an algorithm and draw a flowchart to find and print the largest of three numbers. Read numbers one by one. Verify your result by a trace table. (Use 5, 7, 3 as the numbers read) Algorithm: Step Step Step Step 1: 2: 3: 4: Input N1 Max = N1 Input N2 If N2>Max then Max = N2 endif Input N3 If N3>Max then Max = N3 endif Print The latgest number is:,Max
Step 5: Step 6: Step 7:
Trace Table of Example 10: N1 N N3 Max Decisio Print 2 n 5 5 7 7 Y 3 7 N 7
Flowchart of Example 10:
START
Input N1 Max = N1 Input N2
is N2>Max Y
Max = N2
Input N3
is N3>Max Y
Max = N3
Print Largest Number is, Max
STOP
Example 11: Write down an algorithm and draw a flowchart to find and print the largest of N (N can be any number) numbers. Read numbers one by one. Verify your result by a trace table. (Assume N to be 5 and the following set to be the numbers {1 4 2 6 8 }) Algorithm: Step Step Step Step 1: 2: 3: 4: Input N, Current Max = Current Counter =1 While(Counter<N) Repeat steps 4 through 7 Counter = Counter + 1 Input Next If (Next>Max) then Max = Next endif Print Max
Step 5: Step 6: Step 7: Step 8:
Flowchart of example 11:
START Input
N, Current
Max = Current Counter=1
is Counter < N
Counter = Counter +1 Input
Next
Print Max
STOP
Is Next >Max
Max = Next
Trace Table of Example 11:
N 5 Current 1 Counter 1 2 3 4 5 Max 1 4 4 6 8 Decision 1 Y Y Y Y N Next 4 2 6 8 Decision 2 Y N Y Print -
Example 12: Write down an algorithm and draw a flowchart to count and print from 1 to 10. Verify your results by trace table Algorithm: Step 1: Step 2: Step 4: Step 5: Step 6:
START Input
Low(1), High(10)
Flowchart:
Input Low(1), High(10) Count = 1 While(Count High) Repeat steps 4 through 6 Print Count Count = Count + 1
Count = Low
is Count High
Y Print Count Count = Count +1 STOP
Trace Table:
Low 1 High 10 Count 1 2 3 4 5 6 7 8 9 10 11 Decision Y Y Y Y Y Y Y Y Y Y N Print 1 2 3 4 5 6 7 8 9 10
Example 12: Write an algorithm and draw a flowchart to calculate the factorial of a number(N). Verify your result by a trace table by assuming N = 5. Hint: The factorial of N is the product of numbers from 1 to N) Algorithm: Step Step Step Step 1: 2: 3: 4: Input N Factor = 1 Counter = 1 While(Counter N) Repeat steps 4 through 6 Factor = Factor * Counter Counter = Counter + 1 Print (N, Factor) Trace Table:
START Input
N N 5 Factor 1 1 2 6 24 120 Counter 1 2 3 4 5 6 Decision Y Y Y Y Y N Print
Step 5: Step 6: Step 7: Flowchart:
120
Factor = 1 Counter = 1
is Counter N
Y Factor = Factor * Counter Count = Count +1
Print Factor
STOP
PROBLEMS Prob. 1. Write an algorithm and draw a flowchart to print the square of all numbers from LOW to HIGH. (Test your algorithm with LOW=1 and HIGH=10). Prob. 2. Write an algorithm and draw a flowchart to print the SUM of numbersfrom LOW to HIGH. (Test your algorithm with LOW=3 and HIGH=9.) Prob. 3. Write an algorithm and draw a flowchart to print all numbers between LOW and HIGH that are divisible by NUMBER. Prob. 4. Write an algorithm and draw a flowchart to print all the prime numbers between LOW and HIGH. (Test your algorithm with LOW=1 and HIGH=100.) Prob. 5. Write an algorithm and draw a flowchart to count and print all numbers from LOW to HIGH by steps of STEP. Test with LOW=0 and HIGH=100. Prob. 6. Write an algorithm and draw a flowchart to count and print all numbers from HIGH to LOW by steps of STEP. Test with HIGH=100 and LOW=0. Prob. 7. Write an algorithm and draw a flowchart to print the multiplication table for 6's. i.e. ---- 1 6 = 6 ---- 2 6 = 12 ---- 12 6 = 72 Prob. 8. Write an algorithm and draw a flowchart to print the complete multiplication table for 1's. through 12's. ---- 1 1 = 1 ---- 2 1 = 2 1 2=2 2 2=4
9
1 12 = 12 2 12 = 24
---- 12 1 = 12
12 2 = 24
12 12 = 144
Prob. 9. Write an algorithm and draw a flowchart to arrange N values read from the input in ascending order. Prob. 10. Write an algorithm and draw a flowchart that will find and print the product of 3 numbers. Prob. 11. Write an algorithm and draw a flowchart that will find and print the number of vowels in a given set of characters and print there number of occurrences.
10