CSI 1301. ALGORITHMS - PART 3 REPETITION/LOOP CONTROL STRUCTURE. Need for Repetition. An algorithm with get/give, assignment and conditional branch instructions processes one set of data (givens) each time it is executed What would we do if we needed to process multiple sets of data?
Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.
CSI 1301
ALGORITHMS - PART 3
REPETITION/LOOP
CONTROL STRUCTURE
Get N1
Get N2
…
Get N100
Let Total = N1 + N2 + N3 + … + N99 + N100
Give Total
Set Up
Set Up
TEST
Loop Block
----------------
Instruction 1
Instruction 2
Loop Block
----------------
Instruction 1
Instruction 2
Change
Change
TEST
Syntax
Set Up
Loop When (Test)
Instruction 1
Instruction 2
……….
Change
Finish Loop
Semantic
After the Set Up, the Test is evaluated, if its value is true, instruction1, instruction2,… are executed, and Test is evaluated again. Instruction1, Instruction2 are repeated as long as the Test is true.
When the value of Test becomes false, the Loop ends and the instruction following the Loop is executed.
Setting up a Loop in an Algorithm (2)
Syntax
Set Up
Loop
Finish Loop When (Test)
Semantic
After the Set Up, instruction1, instruction2, … are executed and then Test is evaluated, if its value is true, instruction1, instruction2,… are executed and the Test is evaluated again.
When the value of Test becomes false, the Loop ends and the instruction following the Loop is executed.
Loop
Get X
Finish Loop
Loop
Give Answer
Finish Loop
Name: SUM100
Givens: N
Change: None
Results: Total
Intermediates: Count
Definition: Total := SUM100(N)
Method
Let Total = 0
Let Count = 0
Loop When (Count < 100)
Get N
Let Total = Total + N
Let Count = Count + 1
Finish Loop
Give Total
Write an algorithm to find the sum of 100 numbers. Place the test at the beginning of the loop.
Name: SUM100
Givens: N
Change: None
Results: Total
Intermediates: Count
Definition: Total := SUM100(N)
Method
Let Total = 0
Let Count = 0
Loop
Get N
Let Total = Total + N
Let Count = Count + 1
Finish Loop When (Count = 100)
Give Total
Write an algorithm to find the sum of 100 numbers. Place the test at the end of the loop.
Setup
Test
Change
Write an algorithm to calculate the sum from 1 to N. ie. (1+2+3+4+…N)
Name: SUMN
Givens: N
Change: None
Results: Sum
Intermediates: Value
Definition: Sum := SUMN(N)
Method
Get N
Let Value = 1
Let Sum = 0
Loop When (Value <= N)
Let Sum = Sum + Value
Let Value = Value + 1
Finish Loop
Give Sum
Write an algorithm to calculate the sum from 1 to N. ie. (1+2+3+4+…N)
Trace algorithm 3.2 when N is 4
(1) Get N
(2) Let Value = 1
(3) Let Sum = 0
(4) Loop When (Value <= N)
(5) Let Sum = Sum + Value
(6) Let Value = Value + 1
(7) Finish Loop
(8) Give Sum
LN N Value Sum Test
1 4
2 1
3 0
4 (1<=4)
5 1
6 2
4 (2<=4)
5 3
6 3
4 (3<=4)
5 6
6 4
4 (4<=4)
5 10
6 5
4 (5<=4)
8 Output 10
AND will return a TRUE only when both are TRUE
X | Y | X AND Y
---+---+---------
F | F | F
F | T | F
T | F | F
T | T | T
OR will return a TRUE when either is TRUE
X | Y | X OR Y
---+---+---------
F | F | F
F | T | T
T | F | T
T | T | T
NOT will change a TRUE to a FALSE or a FALSE to a TRUE
X | NOT(X)
---+-------
F | T
T | F
XOR will return a TRUE when either is TRUE, but NOT BOTH
X | Y | X XOR Y
---+---+---------
F | F | F
F | T | T
T | F | T
T | T | F
Write an algorithm to find the average of up to 10 numbers entered by the user.
Write an algorithm to find the average of up to 10 numbers entered by the user.
Name: AVERAGE10
Givens: N
Change: None
Results: AVG
Intermediates: Count, Sum
Again
Definition:
AVG = AVERAGE10(N)
LN N Count Sum Again AVG Test
1 0
2 0
3 Yes
4 (0<10)AND YES
5 1
6 1
7 1
8 Yes
4 (1<10)AND YES
5 3
6 4
7 2
8 Yes
4 (2<10)AND YES
5 5
6 9
7 3
8 Yes
4 (3<10)AND YES
5 3
6 12
7 4
8 No
4 (4<10) AND NO
10 3
11 Output 3
Write an algorithm to perform a COUNT.
Let COUNT = 0
Loop
……….
Let COUNT = COUNT + 1
Finish Loop
Give COUNT
Write an algorithm to perform a SUM.
Let SUM = 0
Loop
Get N
……….
Let SUM = SUM + N
Finish Loop
Give SUM
Write an algorithm to perform an AVERAGE.
Let SUM = 0
Let COUNT = 0
Loop
Get N
……….
Let SUM = SUM + N
Let COUNT = COUNT + 1
Finish Loop
Let Average = SUM/COUNT
Give Average
Write algorithms to perform a MAX and MIN.
Let MAX = - infinity
Loop
Get N
If (N > MAX)
Let MAX = N
Finish Loop
Give MAX
Let MIN = infinity
Loop
Get N
If (N < MIN)
Let MIN = N
Finish Loop
Give MIN
Write an algorithm to perform a SEARCH.
Get Search
Let FOUND = False
LOOP Until (FOUND Or No_More_Values)
Get Value
If (Value = Search)
Let FOUND = True
Finish Loop
If FOUND
Give Value
Else
Give “Not Found”
Write an algorithm to find the average of all positive numbers given by the user.
Method
Let Sum = 0
Let Count = 0
Loop
Get N
If (N > 0)
Let Sum = Sum + N
Let Count = Count + 1
Get Again
Finish Loop When Not(Again)
Let Avg = Sum/Count
Give Avg
LN Sum Count N Avg Again Test
1,2 0 0
4 1
5 (1>0)
6 1
7 1
8 Yes
9 Not(Yes)
4 -5
5 (-5>0)
8 Yes
9 Not(Yes)
4 5
5 (5>0)
6 6
7 2
8 Yes
9 Not(Yes)
4 3
5 (3>0)
6 9
7 3
8 No
9 Not(No)
10 3
11 Output 3
(1) Let Sum = 0
(2) Let Count = 0
(3) Loop
(4) Get N
(5) If (N > 0)
(6) Let Sum = Sum + N
(7) Let Count = Count + 1
(8) Get Again
(9) Finish Loop When Not(Again)
(10) Let Avg = Sum/Count
(11) Give Avg
Write an algorithm to find the largest of 5 numbers (range 1 - 10).
Method
Set LastCount = 5
Let Max = -1
Let Count = 1
Loop When (Count <= LastCount)
Get N
If (N > Max)
Let Max = N
Let Count = Count + 1
Finish Loop
Give Max
LN Max Count LC N Test
1,2,3 -1 1 5
4 (1<=5)
5 1
6 (1>-1)
7 1
8 2
4 (2<=5)
5 5
6 (5>1)
7 5
8 3
4 (3<=5)
5 8
6 (8>5)
7 8
8 4
4 (4<=5)
5 3
6 (3>8)
8 5
4 (5<=5)
5 2
6 (2>8)
8 6
4 (6<=5)
10 Output 8
(1) Set LastCount = 5
(2) Let Max = -1
(3) Let Count = 1
(4) Loop When (Count <= 5)
(5) Get N
(6) If (N > Max)
(7) Let Max = N
(8) Let Count = Count + 1
(9) Finish Loop
(10) Give Max
Additional Materials
Flow Charts
Name: SUM100
Givens: N
Change: None
Results: Total
Intermediates: Count
Definition: Total := SUM100(N)
Name: SUM100
Givens: N
Change: None
Results: Total
Intermediates: Count
Definition: Total := SUM100(N)
Name: SUMN
Givens: N
Change: None
Results: Sum
Intermediates: Value
Definition: Sum := SUMN(N)
Name: AVERAGE10
Givens: N
Change: None
Results: AVG
Intermediates: Count, Sum
Again
Definition:
AVG = AVERAGE10(N)
Name: AVGPOS
Givens: N
Change: None
Results: Avg
Intermediates:
Again, Sum, Count
Definition:
Avg := AVGPOS(N)
Name: MAX5
Givens: N
Change: None
Results: Max
Intermediates:
Count
LastCount (Constant)
Definition:
Max := Max5(N)
NSD
Name: SUM100
Givens: N
Change: None
Results: Total
Intermediates: Count
Definition: Total := SUM100(N)
Name: SUM100
Givens: N
Change: None
Results: Total
Intermediates: Count
Definition: Total := SUM100(N)
Name: SUMN
Givens: N
Change: None
Results: Sum
Intermediates: Value
Definition: Sum := SUMN(N)
Name: AVERAGE10
Givens: N
Change: None
Results: AVG
Intermediates: Count, Sum
Again
Definition:
AVG = AVERAGE10(N)
Name: AVGPOS
Givens: N
Change: None
Results: Avg
Intermediates:
Again, Sum, Count
Definition:
Avg := AVGPOS(N)
Name: MAX5
Givens: N
Change: None
Results: Max
Intermediates:
Count
LastCount (Constant)
Definition:
Max := Max5(N)
Homework
Develop an algorithm to assist a clerk in determining some statistics about students. For each student, she enters the name, gender (M or F), age and marital status (M or S). She wants to determine the number of married men, married women, single women and eligible bachelors (single men over 25). Each time she has completed entry of data for a student, the algorithm should give her a chance to indicate whether she has entered the data for all of the students.
Write an algorithm to determine the closing balance for a teller. The teller enters his opening balance and then a number of transactions. Deposits are entered as positive numbers and withdrawals are entered as negative numbers. He also needs to calculate the number of deposits and the number of withdrawals. The teller indicates that all transactions have been entered by entering a transaction with a value of 0.
Write an algorithm to calculate and display the total gross earnings, tax payable, medical deduction and net earnings for all employees.
The user will input the hours worked and the hourly rate of pay for each employee. The gross earnings for each employee can be calculated from this information.
The net earnings are calculated by subtracting the tax payable and medical deductions from the gross earnings. Tax payable is 20% of gross pay for the first $300, 30% for the next $200 and 40% thereafter. The medical deduction is calculated as 1% of gross pay.
The user will indicate that all employee information has been entered by entering hours worked as 999.