QUIZ: What does this program a: .BLOCK 2 do? · – The divide-and-conquer approach can be applied...
Transcript of QUIZ: What does this program a: .BLOCK 2 do? · – The divide-and-conquer approach can be applied...
![Page 1: QUIZ: What does this program a: .BLOCK 2 do? · – The divide-and-conquer approach can be applied over and over again until each subtask is manageable. 9. ... Algorithm . Calculate](https://reader033.fdocuments.us/reader033/viewer/2022042119/5e97cbb15797e857783642f8/html5/thumbnails/1.jpg)
QUIZ: What does this program do?
1
BR maina: .BLOCK 2main: DECI a, d
LDA a, dSUBA 0x002A, iBREQ yesDECO a, d
fin: STOPyes: CHARO 0x0065, i
CHARO 0x0071, iCHARO 0x0075, iCHARO 0x0061, iCHARO 0x006C, iSTOP.END
![Page 2: QUIZ: What does this program a: .BLOCK 2 do? · – The divide-and-conquer approach can be applied over and over again until each subtask is manageable. 9. ... Algorithm . Calculate](https://reader033.fdocuments.us/reader033/viewer/2022042119/5e97cbb15797e857783642f8/html5/thumbnails/2.jpg)
Solution
2
The hex ASCII codes are for the letters e, q, u, a and l.
If the number entered is not42, the number itself is output, otherwise the message equal is output.
Note: Instead of SUBA, the instruction CPA can be used – do you see why?
![Page 3: QUIZ: What does this program a: .BLOCK 2 do? · – The divide-and-conquer approach can be applied over and over again until each subtask is manageable. 9. ... Algorithm . Calculate](https://reader033.fdocuments.us/reader033/viewer/2022042119/5e97cbb15797e857783642f8/html5/thumbnails/3.jpg)
QUIZ on Ch.6:Write the pseudocode for adding two numbers
together; our target language is PEP/8 assembly.
3
![Page 4: QUIZ: What does this program a: .BLOCK 2 do? · – The divide-and-conquer approach can be applied over and over again until each subtask is manageable. 9. ... Algorithm . Calculate](https://reader033.fdocuments.us/reader033/viewer/2022042119/5e97cbb15797e857783642f8/html5/thumbnails/4.jpg)
4
•Input the first number - into memory location "A" •Input the second number - into memory location "B" •Load A from memory into Accumulator reg. •Add B from memory; result goes into Accumulator reg. •Store the Accumulator into memory location "C" •Output the value in memory location "C"
Solution
For more practice: Write the assembly code!
![Page 5: QUIZ: What does this program a: .BLOCK 2 do? · – The divide-and-conquer approach can be applied over and over again until each subtask is manageable. 9. ... Algorithm . Calculate](https://reader033.fdocuments.us/reader033/viewer/2022042119/5e97cbb15797e857783642f8/html5/thumbnails/5.jpg)
![Page 6: QUIZ: What does this program a: .BLOCK 2 do? · – The divide-and-conquer approach can be applied over and over again until each subtask is manageable. 9. ... Algorithm . Calculate](https://reader033.fdocuments.us/reader033/viewer/2022042119/5e97cbb15797e857783642f8/html5/thumbnails/6.jpg)
Problem Solving
How to Solve It: A New Aspect of Mathematical Method by George Polya, 1945
The book is written within the context of solving mathematical problems, but the methods described are applicable to problem solving in general.
6
We can use the methods described by Polya to solve any computer-related problem!
![Page 7: QUIZ: What does this program a: .BLOCK 2 do? · – The divide-and-conquer approach can be applied over and over again until each subtask is manageable. 9. ... Algorithm . Calculate](https://reader033.fdocuments.us/reader033/viewer/2022042119/5e97cbb15797e857783642f8/html5/thumbnails/7.jpg)
1. Understand the problem– What are the hypotheses? Data? Unknowns?
2. Devise a plan– Can we find a related problem? A sub-problem?– Can we strengthen or relax the hypotheses to obtain an
easier problem?
3. Carry out the plan— Prove that each step is correct!
4. Look back– Check result– Find shortcuts and alternate solutions– Generalize to related problems
7
Polya’s 4 steps
![Page 8: QUIZ: What does this program a: .BLOCK 2 do? · – The divide-and-conquer approach can be applied over and over again until each subtask is manageable. 9. ... Algorithm . Calculate](https://reader033.fdocuments.us/reader033/viewer/2022042119/5e97cbb15797e857783642f8/html5/thumbnails/8.jpg)
Polya’s StrategiesDo not reinvent the wheel!Similar problems come up again and again in different guises.A good programmer recognizes a task or subtask that has been solved before and reuses the solution.
Can you think of two similar problems we solved in Python?
8
Finding a character in a string and finding an element in a list
![Page 9: QUIZ: What does this program a: .BLOCK 2 do? · – The divide-and-conquer approach can be applied over and over again until each subtask is manageable. 9. ... Algorithm . Calculate](https://reader033.fdocuments.us/reader033/viewer/2022042119/5e97cbb15797e857783642f8/html5/thumbnails/9.jpg)
Polya’s StrategiesDivide and Conquer!Break up a large problem into smaller sub-problems
and solve each separately – Applies the concept of abstraction
– The divide-and-conquer approach can be applied over and over again until each subtask is manageable
9
![Page 10: QUIZ: What does this program a: .BLOCK 2 do? · – The divide-and-conquer approach can be applied over and over again until each subtask is manageable. 9. ... Algorithm . Calculate](https://reader033.fdocuments.us/reader033/viewer/2022042119/5e97cbb15797e857783642f8/html5/thumbnails/10.jpg)
Divide and Conquer!Break up a large problem into smaller sub-problems
and solve each separately
10Image source: http://teamapproach.ca/trouble/Methodology.htm
![Page 11: QUIZ: What does this program a: .BLOCK 2 do? · – The divide-and-conquer approach can be applied over and over again until each subtask is manageable. 9. ... Algorithm . Calculate](https://reader033.fdocuments.us/reader033/viewer/2022042119/5e97cbb15797e857783642f8/html5/thumbnails/11.jpg)
Polya’s 4 steps can be applied to computerproblem solving!
11
Analysis and Specification PhaseAsk questions to understand all the requirementsExplain in detail what needs to be achieved
Algorithm Development PhaseDevelop algorithmTest algorithm
Implementation PhaseCode algorithmTest the code in various ways
Maintenance PhaseUse the code, find bugsFix bugsCode new features, as requested by users
![Page 12: QUIZ: What does this program a: .BLOCK 2 do? · – The divide-and-conquer approach can be applied over and over again until each subtask is manageable. 9. ... Algorithm . Calculate](https://reader033.fdocuments.us/reader033/viewer/2022042119/5e97cbb15797e857783642f8/html5/thumbnails/12.jpg)
QUIZ: Match the steps in Polya’s method to the ones in the computer method for problem solving
12
Analysis and Specification
Implementation
Algorithm Development
Maintenance
Devise a plan
Look back
Understand
Carry out the plan
![Page 13: QUIZ: What does this program a: .BLOCK 2 do? · – The divide-and-conquer approach can be applied over and over again until each subtask is manageable. 9. ... Algorithm . Calculate](https://reader033.fdocuments.us/reader033/viewer/2022042119/5e97cbb15797e857783642f8/html5/thumbnails/13.jpg)
Interactions among the
phases
13
![Page 14: QUIZ: What does this program a: .BLOCK 2 do? · – The divide-and-conquer approach can be applied over and over again until each subtask is manageable. 9. ... Algorithm . Calculate](https://reader033.fdocuments.us/reader033/viewer/2022042119/5e97cbb15797e857783642f8/html5/thumbnails/14.jpg)
Algorithms
Algorithm = A set of unambiguous steps for solving a problem in a finite amount of time,using a finite amount of resources
14
![Page 15: QUIZ: What does this program a: .BLOCK 2 do? · – The divide-and-conquer approach can be applied over and over again until each subtask is manageable. 9. ... Algorithm . Calculate](https://reader033.fdocuments.us/reader033/viewer/2022042119/5e97cbb15797e857783642f8/html5/thumbnails/15.jpg)
Algorithm = A set of unambiguous steps for solving a problem in a finite amount of time, using a finite amount of data
15Image source: http://my-online-log.com/tech/archives/1214
![Page 16: QUIZ: What does this program a: .BLOCK 2 do? · – The divide-and-conquer approach can be applied over and over again until each subtask is manageable. 9. ... Algorithm . Calculate](https://reader033.fdocuments.us/reader033/viewer/2022042119/5e97cbb15797e857783642f8/html5/thumbnails/16.jpg)
Refining the steps, from ambiguous (abstract) to unambiguous (concrete)
Abstract Step An algorithmic step containing unspecified detailsConcrete StepAn algorithm step in which all details are specified
16
Whether the step is abstract or concrete depends on the programming language we’re using!
![Page 17: QUIZ: What does this program a: .BLOCK 2 do? · – The divide-and-conquer approach can be applied over and over again until each subtask is manageable. 9. ... Algorithm . Calculate](https://reader033.fdocuments.us/reader033/viewer/2022042119/5e97cbb15797e857783642f8/html5/thumbnails/17.jpg)
7.2 Algorithms with simple variables
Variable = a means of storing intermediate results from one task to the next.
At the hardware level, a simple variable is just one or several adjacent Bytes in the computer memory.
Q: How many Bytes does a simple variable have in PEP/8?
17
![Page 18: QUIZ: What does this program a: .BLOCK 2 do? · – The divide-and-conquer approach can be applied over and over again until each subtask is manageable. 9. ... Algorithm . Calculate](https://reader033.fdocuments.us/reader033/viewer/2022042119/5e97cbb15797e857783642f8/html5/thumbnails/18.jpg)
QUIZWe have two variables a and b.Create an algorithm to swap their values!
18
What is wrong with this algorithm?a = bb = a
![Page 19: QUIZ: What does this program a: .BLOCK 2 do? · – The divide-and-conquer approach can be applied over and over again until each subtask is manageable. 9. ... Algorithm . Calculate](https://reader033.fdocuments.us/reader033/viewer/2022042119/5e97cbb15797e857783642f8/html5/thumbnails/19.jpg)
19
save = aa = bb = save
SolutionThe algorithm proposed results in both a and b storing the value of b!The correct algorithm involves three steps and an extra variable:
![Page 20: QUIZ: What does this program a: .BLOCK 2 do? · – The divide-and-conquer approach can be applied over and over again until each subtask is manageable. 9. ... Algorithm . Calculate](https://reader033.fdocuments.us/reader033/viewer/2022042119/5e97cbb15797e857783642f8/html5/thumbnails/20.jpg)
Source: AP CS Principles – Course and Exam Descriptions
Similar problem (Do not reinvent the wheel!)
![Page 21: QUIZ: What does this program a: .BLOCK 2 do? · – The divide-and-conquer approach can be applied over and over again until each subtask is manageable. 9. ... Algorithm . Calculate](https://reader033.fdocuments.us/reader033/viewer/2022042119/5e97cbb15797e857783642f8/html5/thumbnails/21.jpg)
Algorithms with Selection Statements (a.k.a. decision or if)
21
Flowchart of if statement
Figure is not in text
![Page 22: QUIZ: What does this program a: .BLOCK 2 do? · – The divide-and-conquer approach can be applied over and over again until each subtask is manageable. 9. ... Algorithm . Calculate](https://reader033.fdocuments.us/reader033/viewer/2022042119/5e97cbb15797e857783642f8/html5/thumbnails/22.jpg)
22
Algorithm with SelectionProblem: Write the appropriate dress for a given temperature.
Write "Enter temperature"Read temperatureDetermine Dress
Which statements are concrete?Which statements are abstract?
Algorithm Determine Dress v.1
Computer language is Python from now on!
![Page 23: QUIZ: What does this program a: .BLOCK 2 do? · – The divide-and-conquer approach can be applied over and over again until each subtask is manageable. 9. ... Algorithm . Calculate](https://reader033.fdocuments.us/reader033/viewer/2022042119/5e97cbb15797e857783642f8/html5/thumbnails/23.jpg)
23
Write “Enter temperature”Read temperatureIF (temperature > 90)
Write “Texas weather: wear shorts”ELSE IF (temperature > 70)
Write “Ideal weather: short sleeves are fine”ELSE IF (temperature > 50)
Write “A little chilly: wear a light jacket”ELSE IF (temperature > 32)
Write “Philadelphia weather: wear a heavy coat”ELSE
Write “Stay inside”
Algorithm Determine Dress v.2
Is this concrete enough for implementation in Python?
![Page 24: QUIZ: What does this program a: .BLOCK 2 do? · – The divide-and-conquer approach can be applied over and over again until each subtask is manageable. 9. ... Algorithm . Calculate](https://reader033.fdocuments.us/reader033/viewer/2022042119/5e97cbb15797e857783642f8/html5/thumbnails/24.jpg)
Algorithms with Loops (a.k.a. repetition)
24
Flow of control of while loop (decision at beginning of loop)
Figure is not in text
![Page 25: QUIZ: What does this program a: .BLOCK 2 do? · – The divide-and-conquer approach can be applied over and over again until each subtask is manageable. 9. ... Algorithm . Calculate](https://reader033.fdocuments.us/reader033/viewer/2022042119/5e97cbb15797e857783642f8/html5/thumbnails/25.jpg)
QUIZ: There are loops that can execute 0 times and loops that must execute at least 1 time.Which type is this?
25
![Page 26: QUIZ: What does this program a: .BLOCK 2 do? · – The divide-and-conquer approach can be applied over and over again until each subtask is manageable. 9. ... Algorithm . Calculate](https://reader033.fdocuments.us/reader033/viewer/2022042119/5e97cbb15797e857783642f8/html5/thumbnails/26.jpg)
Answer: It depends on whether the decision (diamond) is at the beginning or at the end of the loop!
26
Figure is not in text
![Page 27: QUIZ: What does this program a: .BLOCK 2 do? · – The divide-and-conquer approach can be applied over and over again until each subtask is manageable. 9. ... Algorithm . Calculate](https://reader033.fdocuments.us/reader033/viewer/2022042119/5e97cbb15797e857783642f8/html5/thumbnails/27.jpg)
Loops in Python
27
Not in text
![Page 28: QUIZ: What does this program a: .BLOCK 2 do? · – The divide-and-conquer approach can be applied over and over again until each subtask is manageable. 9. ... Algorithm . Calculate](https://reader033.fdocuments.us/reader033/viewer/2022042119/5e97cbb15797e857783642f8/html5/thumbnails/28.jpg)
Extra-credit question:
28
![Page 29: QUIZ: What does this program a: .BLOCK 2 do? · – The divide-and-conquer approach can be applied over and over again until each subtask is manageable. 9. ... Algorithm . Calculate](https://reader033.fdocuments.us/reader033/viewer/2022042119/5e97cbb15797e857783642f8/html5/thumbnails/29.jpg)
Event-controlled Loops, a.k.a. WHILE loops
29
They are the most general type of loops!
Set sum to 0Set allPositive to trueWHILE (allPositive)
Read numberIF (number > 0)
Set sum to sum + numberELSE
Set allPositive to falseWrite "Sum is " + sum
![Page 30: QUIZ: What does this program a: .BLOCK 2 do? · – The divide-and-conquer approach can be applied over and over again until each subtask is manageable. 9. ... Algorithm . Calculate](https://reader033.fdocuments.us/reader033/viewer/2022042119/5e97cbb15797e857783642f8/html5/thumbnails/30.jpg)
Counter-controlled Loops
30
They are a particular case of event-controlled loops: the event is that a counter variable has reached a predetermined limit
Set sum to 0Set limit to 42Set count to 1While (count <= limit)
Read numberSet sum to sum + numberIncrement count
Write "Sum is " + sum
For loops are counter-
controlled!
![Page 31: QUIZ: What does this program a: .BLOCK 2 do? · – The divide-and-conquer approach can be applied over and over again until each subtask is manageable. 9. ... Algorithm . Calculate](https://reader033.fdocuments.us/reader033/viewer/2022042119/5e97cbb15797e857783642f8/html5/thumbnails/31.jpg)
31
Important application of looping: Successive approximation algorithms
Read in squareCalculate the square rootWrite out square and the square root
Algorithm Calculate Square Root v.1
Which steps are abstract and which concrete?
![Page 32: QUIZ: What does this program a: .BLOCK 2 do? · – The divide-and-conquer approach can be applied over and over again until each subtask is manageable. 9. ... Algorithm . Calculate](https://reader033.fdocuments.us/reader033/viewer/2022042119/5e97cbb15797e857783642f8/html5/thumbnails/32.jpg)
32
Set epsilon to 1WHILE (epsilon > 0.001)
Calculate new guessSet epsilon to abs(square - guess * guess)
Which steps are abstract and which concrete?
Algorithm Calculate Square Root v.2
In Python usemath.fabs(…)
A more appropriate name for guess would be approximation
![Page 33: QUIZ: What does this program a: .BLOCK 2 do? · – The divide-and-conquer approach can be applied over and over again until each subtask is manageable. 9. ... Algorithm . Calculate](https://reader033.fdocuments.us/reader033/viewer/2022042119/5e97cbb15797e857783642f8/html5/thumbnails/33.jpg)
33
Set newGuess to(guess + (square/guess)) / 2.0
Set guess to square/4Set epsilon to 1
What’s the mathematical formula for the new approximation?
How do we get the loop started?
Algorithm Calculate Square Root - Refinements in v.2
![Page 34: QUIZ: What does this program a: .BLOCK 2 do? · – The divide-and-conquer approach can be applied over and over again until each subtask is manageable. 9. ... Algorithm . Calculate](https://reader033.fdocuments.us/reader033/viewer/2022042119/5e97cbb15797e857783642f8/html5/thumbnails/34.jpg)
34
Read in squareSet guess to square/4Set epsilon to 1WHILE (epsilon > 0.001)
Set guess to (guess + (square/guess)) / 2.0Set epsilon to abs(square - guess * guess)
Write out square and guess
Which steps are abstract and which concrete?
Algorithm Calculate Square Root v.3
![Page 35: QUIZ: What does this program a: .BLOCK 2 do? · – The divide-and-conquer approach can be applied over and over again until each subtask is manageable. 9. ... Algorithm . Calculate](https://reader033.fdocuments.us/reader033/viewer/2022042119/5e97cbb15797e857783642f8/html5/thumbnails/35.jpg)
35
Set newGuess to(guess + (square/guess)) / 2.0
QUIZ: Square root approximation alg.
We want to calculate = 2.236…
Set your initial guess x0 = 1 and show the next 3 approximations x1, x2, x3
5
![Page 36: QUIZ: What does this program a: .BLOCK 2 do? · – The divide-and-conquer approach can be applied over and over again until each subtask is manageable. 9. ... Algorithm . Calculate](https://reader033.fdocuments.us/reader033/viewer/2022042119/5e97cbb15797e857783642f8/html5/thumbnails/36.jpg)
36
SolutionWe want to calculate = 2.236…5
![Page 37: QUIZ: What does this program a: .BLOCK 2 do? · – The divide-and-conquer approach can be applied over and over again until each subtask is manageable. 9. ... Algorithm . Calculate](https://reader033.fdocuments.us/reader033/viewer/2022042119/5e97cbb15797e857783642f8/html5/thumbnails/37.jpg)
37
Set newGuess to(guess + (square/guess)) / 2.0
Quick work for next time (in notebook):
We want to calculate = 2.236…
Set your initial guess x0 = 42 and show the next 3 approximations x1, x2, x3
5
![Page 38: QUIZ: What does this program a: .BLOCK 2 do? · – The divide-and-conquer approach can be applied over and over again until each subtask is manageable. 9. ... Algorithm . Calculate](https://reader033.fdocuments.us/reader033/viewer/2022042119/5e97cbb15797e857783642f8/html5/thumbnails/38.jpg)
38
QUIZ: Square root algorithm
We want to calculate = 2.645751…
Set your initial guess to x0 = 7/4 = 1.75
and show the next 3 approximations x1, x2, x3
7
![Page 39: QUIZ: What does this program a: .BLOCK 2 do? · – The divide-and-conquer approach can be applied over and over again until each subtask is manageable. 9. ... Algorithm . Calculate](https://reader033.fdocuments.us/reader033/viewer/2022042119/5e97cbb15797e857783642f8/html5/thumbnails/39.jpg)
39
QUIZ: Square root algorithm
We want to calculate = 2.645751…7
![Page 40: QUIZ: What does this program a: .BLOCK 2 do? · – The divide-and-conquer approach can be applied over and over again until each subtask is manageable. 9. ... Algorithm . Calculate](https://reader033.fdocuments.us/reader033/viewer/2022042119/5e97cbb15797e857783642f8/html5/thumbnails/40.jpg)
40
QUIZ: Square root algorithm
We want to calculate = 2.645751…7
![Page 41: QUIZ: What does this program a: .BLOCK 2 do? · – The divide-and-conquer approach can be applied over and over again until each subtask is manageable. 9. ... Algorithm . Calculate](https://reader033.fdocuments.us/reader033/viewer/2022042119/5e97cbb15797e857783642f8/html5/thumbnails/41.jpg)
41
QUIZ: Square root algorithm
We want to calculate = 2.645751…7
![Page 42: QUIZ: What does this program a: .BLOCK 2 do? · – The divide-and-conquer approach can be applied over and over again until each subtask is manageable. 9. ... Algorithm . Calculate](https://reader033.fdocuments.us/reader033/viewer/2022042119/5e97cbb15797e857783642f8/html5/thumbnails/42.jpg)
42
Remember: The algorithm converges irrespective of the initial guess x0!
= 2.645751…7
![Page 43: QUIZ: What does this program a: .BLOCK 2 do? · – The divide-and-conquer approach can be applied over and over again until each subtask is manageable. 9. ... Algorithm . Calculate](https://reader033.fdocuments.us/reader033/viewer/2022042119/5e97cbb15797e857783642f8/html5/thumbnails/43.jpg)
43
QUIZ:
Re-write this program using a for loop!
![Page 44: QUIZ: What does this program a: .BLOCK 2 do? · – The divide-and-conquer approach can be applied over and over again until each subtask is manageable. 9. ... Algorithm . Calculate](https://reader033.fdocuments.us/reader033/viewer/2022042119/5e97cbb15797e857783642f8/html5/thumbnails/44.jpg)
Solution
![Page 45: QUIZ: What does this program a: .BLOCK 2 do? · – The divide-and-conquer approach can be applied over and over again until each subtask is manageable. 9. ... Algorithm . Calculate](https://reader033.fdocuments.us/reader033/viewer/2022042119/5e97cbb15797e857783642f8/html5/thumbnails/45.jpg)
Problem for lab:
Re-write this program using a while loop!
Hint: What condition should we use to control the loop?
EoHF 1
![Page 46: QUIZ: What does this program a: .BLOCK 2 do? · – The divide-and-conquer approach can be applied over and over again until each subtask is manageable. 9. ... Algorithm . Calculate](https://reader033.fdocuments.us/reader033/viewer/2022042119/5e97cbb15797e857783642f8/html5/thumbnails/46.jpg)
QUIZ True/FalseA. Count-controlled loops repeat a specific task a
number of times.B. Event-controlled loops repeat a specific
number of timesC. Count-controlled loops are controlled by a
counterD. Count-controlled loops are more general than
event-controlled loops
46
![Page 47: QUIZ: What does this program a: .BLOCK 2 do? · – The divide-and-conquer approach can be applied over and over again until each subtask is manageable. 9. ... Algorithm . Calculate](https://reader033.fdocuments.us/reader033/viewer/2022042119/5e97cbb15797e857783642f8/html5/thumbnails/47.jpg)
47
Set newGuess to(guess + (square/guess)) / 2.0
QUIZ: Square root algorithm (approximations)
We want to calculate = 3.16227…
Set your initial guess x0 = 10/4 and show the next 3 approximations x1, x2, x3
10
![Page 48: QUIZ: What does this program a: .BLOCK 2 do? · – The divide-and-conquer approach can be applied over and over again until each subtask is manageable. 9. ... Algorithm . Calculate](https://reader033.fdocuments.us/reader033/viewer/2022042119/5e97cbb15797e857783642f8/html5/thumbnails/48.jpg)
48
Solution
We want to calculate = 3.16227…
Set your initial guess x0 = 10/4 and show the next 3 approximations x1, x2, x3
10
x0 = 2.5 x1 = 3.25 x2 = 3.16346… x3 = 3.16227…
![Page 49: QUIZ: What does this program a: .BLOCK 2 do? · – The divide-and-conquer approach can be applied over and over again until each subtask is manageable. 9. ... Algorithm . Calculate](https://reader033.fdocuments.us/reader033/viewer/2022042119/5e97cbb15797e857783642f8/html5/thumbnails/49.jpg)
7.3 Composite Data Types and their Algorithms
They can be classified according to many criteria:
• homogeneous vs. heterogeneous
• accessed by index vs. accessed by name
• mutable vs. immutable
• linear vs. non-linear
• etc.
49
![Page 50: QUIZ: What does this program a: .BLOCK 2 do? · – The divide-and-conquer approach can be applied over and over again until each subtask is manageable. 9. ... Algorithm . Calculate](https://reader033.fdocuments.us/reader033/viewer/2022042119/5e97cbb15797e857783642f8/html5/thumbnails/50.jpg)
Composite Data Types
RecordsA named heterogeneous collection of items in which individual items are accessed by name. For example, we could bundle name, age and hourly wage items into a record named Employee
ArraysA named homogeneous collection of items in which an individual item is accessed by its position (index) within the collection
50
Are these the lists from Python? Why not?
Python strings are arrays of characters
![Page 51: QUIZ: What does this program a: .BLOCK 2 do? · – The divide-and-conquer approach can be applied over and over again until each subtask is manageable. 9. ... Algorithm . Calculate](https://reader033.fdocuments.us/reader033/viewer/2022042119/5e97cbb15797e857783642f8/html5/thumbnails/51.jpg)
Composite Data Types (contd.)
Lists (will be covered in next chapter)A named heterogeneous collection of items in which individual items are accessed by position (index).
We have them in Python, e.g.>>> myList = [“dog”, 42, 51.375, [1,2]]>>> myList[1]42
51
![Page 52: QUIZ: What does this program a: .BLOCK 2 do? · – The divide-and-conquer approach can be applied over and over again until each subtask is manageable. 9. ... Algorithm . Calculate](https://reader033.fdocuments.us/reader033/viewer/2022042119/5e97cbb15797e857783642f8/html5/thumbnails/52.jpg)
Composite Data Types - RecordsEmployee
nameagehourly/Wage
Algorithm to store values into the fields of record:
Employee employee // Declare an Employee variableSet employee.name to “Frank Jones”Set employee.age to 32Set employee.hourlyWage to 27.50
52
![Page 53: QUIZ: What does this program a: .BLOCK 2 do? · – The divide-and-conquer approach can be applied over and over again until each subtask is manageable. 9. ... Algorithm . Calculate](https://reader033.fdocuments.us/reader033/viewer/2022042119/5e97cbb15797e857783642f8/html5/thumbnails/53.jpg)
Composite Data Types - Arrays
53
numbers[0]
numbers[4]
numbers
![Page 54: QUIZ: What does this program a: .BLOCK 2 do? · – The divide-and-conquer approach can be applied over and over again until each subtask is manageable. 9. ... Algorithm . Calculate](https://reader033.fdocuments.us/reader033/viewer/2022042119/5e97cbb15797e857783642f8/html5/thumbnails/54.jpg)
Some items in an array may be unused at a given time
54
first
last
numbers
??
??
??
??
![Page 55: QUIZ: What does this program a: .BLOCK 2 do? · – The divide-and-conquer approach can be applied over and over again until each subtask is manageable. 9. ... Algorithm . Calculate](https://reader033.fdocuments.us/reader033/viewer/2022042119/5e97cbb15797e857783642f8/html5/thumbnails/55.jpg)
Useful Algorithms on Arrays
• Initializing all items• Printing all items• Searching for an item• Sorting the array
55
![Page 56: QUIZ: What does this program a: .BLOCK 2 do? · – The divide-and-conquer approach can be applied over and over again until each subtask is manageable. 9. ... Algorithm . Calculate](https://reader033.fdocuments.us/reader033/viewer/2022042119/5e97cbb15797e857783642f8/html5/thumbnails/56.jpg)
56
Algorithm for initializing an array
integer data[20]Write “How many values?”Read lengthSet index to 0WHILE (index < length)
Read data[index]Set index to index + 1
Task: Fill an array numbers with length values that are being input from the keyboard.
![Page 57: QUIZ: What does this program a: .BLOCK 2 do? · – The divide-and-conquer approach can be applied over and over again until each subtask is manageable. 9. ... Algorithm . Calculate](https://reader033.fdocuments.us/reader033/viewer/2022042119/5e97cbb15797e857783642f8/html5/thumbnails/57.jpg)
57
QUIZ
integer data[20]Write “How many values?”Read lengthSet index to 0WHILE (index < length)
Read data[index]Set index to index + 1
Modify this pseudocode to print the values after initializing them.
![Page 58: QUIZ: What does this program a: .BLOCK 2 do? · – The divide-and-conquer approach can be applied over and over again until each subtask is manageable. 9. ... Algorithm . Calculate](https://reader033.fdocuments.us/reader033/viewer/2022042119/5e97cbb15797e857783642f8/html5/thumbnails/58.jpg)
An Unsorted
Array
58
data
![Page 59: QUIZ: What does this program a: .BLOCK 2 do? · – The divide-and-conquer approach can be applied over and over again until each subtask is manageable. 9. ... Algorithm . Calculate](https://reader033.fdocuments.us/reader033/viewer/2022042119/5e97cbb15797e857783642f8/html5/thumbnails/59.jpg)
A Sorted Array
59
data
![Page 60: QUIZ: What does this program a: .BLOCK 2 do? · – The divide-and-conquer approach can be applied over and over again until each subtask is manageable. 9. ... Algorithm . Calculate](https://reader033.fdocuments.us/reader033/viewer/2022042119/5e97cbb15797e857783642f8/html5/thumbnails/60.jpg)
Sorted Arrays
• The values stored in an array have unique keysof a type for which the relational operators are defined.
• Sorting rearranges the elements into either ascending or descending order within the array.
60
Reality check: In a real-life problem it’s very common
to encounter repeated keys!
![Page 61: QUIZ: What does this program a: .BLOCK 2 do? · – The divide-and-conquer approach can be applied over and over again until each subtask is manageable. 9. ... Algorithm . Calculate](https://reader033.fdocuments.us/reader033/viewer/2022042119/5e97cbb15797e857783642f8/html5/thumbnails/61.jpg)
7.4 Search algorithms
61
![Page 62: QUIZ: What does this program a: .BLOCK 2 do? · – The divide-and-conquer approach can be applied over and over again until each subtask is manageable. 9. ... Algorithm . Calculate](https://reader033.fdocuments.us/reader033/viewer/2022042119/5e97cbb15797e857783642f8/html5/thumbnails/62.jpg)
Sequential Search in anUnsorted Array
A sequential search examines each item in turn and compares it to the one we are searching for.
If it matches, we have found the item. If not, we examine the next item in the array.
We stop either when we have found the item or when we have looked at all the items and not found a match.
Thus, we have a loop with two ending conditions.
62
![Page 63: QUIZ: What does this program a: .BLOCK 2 do? · – The divide-and-conquer approach can be applied over and over again until each subtask is manageable. 9. ... Algorithm . Calculate](https://reader033.fdocuments.us/reader033/viewer/2022042119/5e97cbb15797e857783642f8/html5/thumbnails/63.jpg)
Set position to 0Set found to FALSEWHILE (position < length AND NOT found )
IF (numbers[position] equals searchItem)Set found to TRUE
ELSE Set position to position + 1
63
The array’s name is numbersThe value we’re searching for is stored in searchItem
![Page 64: QUIZ: What does this program a: .BLOCK 2 do? · – The divide-and-conquer approach can be applied over and over again until each subtask is manageable. 9. ... Algorithm . Calculate](https://reader033.fdocuments.us/reader033/viewer/2022042119/5e97cbb15797e857783642f8/html5/thumbnails/64.jpg)
Set position to 0Set found to FALSEWHILE (position < length AND NOT found )
IF (numbers[position] equals searchItem)Set found to TRUE
ELSE Set position to position + 1
64
QUIZ: When the loop exits, what do we need to do?
![Page 65: QUIZ: What does this program a: .BLOCK 2 do? · – The divide-and-conquer approach can be applied over and over again until each subtask is manageable. 9. ... Algorithm . Calculate](https://reader033.fdocuments.us/reader033/viewer/2022042119/5e97cbb15797e857783642f8/html5/thumbnails/65.jpg)
Set index to 0Set found to FALSEWHILE (index < length AND NOT found )
IF (data[index] equals searchItem)Set found to TRUE
ELSE Set index to index + 1
65
QUIZ: Desk-check this algorithm for the arrayThe item searched is:A. 35B. 43
42100351
![Page 66: QUIZ: What does this program a: .BLOCK 2 do? · – The divide-and-conquer approach can be applied over and over again until each subtask is manageable. 9. ... Algorithm . Calculate](https://reader033.fdocuments.us/reader033/viewer/2022042119/5e97cbb15797e857783642f8/html5/thumbnails/66.jpg)
Sequential Search in a Sorted ArrayIdea:If items in an array are sorted, we can stop
looking when we pass the place where the item would be if it were present in the array
66
Is this (always) better?
![Page 67: QUIZ: What does this program a: .BLOCK 2 do? · – The divide-and-conquer approach can be applied over and over again until each subtask is manageable. 9. ... Algorithm . Calculate](https://reader033.fdocuments.us/reader033/viewer/2022042119/5e97cbb15797e857783642f8/html5/thumbnails/67.jpg)
67
Sequential Search in a Sorted ArraySet index to 0Set found to FALSEWHILE (index < length AND NOT found)
IF (data[index] equals searchItem)Set found to TRUE
ELSE IF (data[index] > searchItem)Set index to length
ELSESet index to index + 1
This is the new part!
(Compare with previous alg. for unsorted array)
This alg. is called sequential search with early termination
![Page 68: QUIZ: What does this program a: .BLOCK 2 do? · – The divide-and-conquer approach can be applied over and over again until each subtask is manageable. 9. ... Algorithm . Calculate](https://reader033.fdocuments.us/reader033/viewer/2022042119/5e97cbb15797e857783642f8/html5/thumbnails/68.jpg)
QUIZ: End-of-chapter question 66 a
68
SearchItem = 4SearchItem = 49SearchItem = 50
![Page 69: QUIZ: What does this program a: .BLOCK 2 do? · – The divide-and-conquer approach can be applied over and over again until each subtask is manageable. 9. ... Algorithm . Calculate](https://reader033.fdocuments.us/reader033/viewer/2022042119/5e97cbb15797e857783642f8/html5/thumbnails/69.jpg)
Binary Search in Sorted Array
Search begins at the middle and finds the item or eliminates half of the unexamined items; the process is then repeated on the half where the item might be
69
24 30 31 42 44 90 92 94 99
Example: searchItem = 42
![Page 70: QUIZ: What does this program a: .BLOCK 2 do? · – The divide-and-conquer approach can be applied over and over again until each subtask is manageable. 9. ... Algorithm . Calculate](https://reader033.fdocuments.us/reader033/viewer/2022042119/5e97cbb15797e857783642f8/html5/thumbnails/70.jpg)
Binary Search
Algorithm
70
Set first to 0Set last to length-1Set found to FALSEWHILE (first <= last AND NOT found)
Set middle to (first + last)/ 2IF (item equals data[middle]))
Set found to TRUEELSE
IF (item < data[middle])Set last to middle – 1
ELSESet first to middle + 1
RETURN found
Grok why integerdivision works
here!
![Page 71: QUIZ: What does this program a: .BLOCK 2 do? · – The divide-and-conquer approach can be applied over and over again until each subtask is manageable. 9. ... Algorithm . Calculate](https://reader033.fdocuments.us/reader033/viewer/2022042119/5e97cbb15797e857783642f8/html5/thumbnails/71.jpg)
Binary Search example
71Figure 7.10 Trace of the binary search
rat
![Page 72: QUIZ: What does this program a: .BLOCK 2 do? · – The divide-and-conquer approach can be applied over and over again until each subtask is manageable. 9. ... Algorithm . Calculate](https://reader033.fdocuments.us/reader033/viewer/2022042119/5e97cbb15797e857783642f8/html5/thumbnails/72.jpg)
QUIZ Binary Search
72
Search for deer
![Page 73: QUIZ: What does this program a: .BLOCK 2 do? · – The divide-and-conquer approach can be applied over and over again until each subtask is manageable. 9. ... Algorithm . Calculate](https://reader033.fdocuments.us/reader033/viewer/2022042119/5e97cbb15797e857783642f8/html5/thumbnails/73.jpg)
Search for the key 42 in this array, using binary search:
73
Trick QUIZ
EoHW 2
![Page 74: QUIZ: What does this program a: .BLOCK 2 do? · – The divide-and-conquer approach can be applied over and over again until each subtask is manageable. 9. ... Algorithm . Calculate](https://reader033.fdocuments.us/reader033/viewer/2022042119/5e97cbb15797e857783642f8/html5/thumbnails/74.jpg)
0] Define arrays
1] Define records
2] How are arrays and records:
(a) similar?
(b) different?
74
QUIZ
![Page 75: QUIZ: What does this program a: .BLOCK 2 do? · – The divide-and-conquer approach can be applied over and over again until each subtask is manageable. 9. ... Algorithm . Calculate](https://reader033.fdocuments.us/reader033/viewer/2022042119/5e97cbb15797e857783642f8/html5/thumbnails/75.jpg)
3] What are the 4 fundamental types of algorithms used to manipulate arrays?
4] What control structure is normally used to access all the elements of an array?
75
QUIZ
![Page 76: QUIZ: What does this program a: .BLOCK 2 do? · – The divide-and-conquer approach can be applied over and over again until each subtask is manageable. 9. ... Algorithm . Calculate](https://reader033.fdocuments.us/reader033/viewer/2022042119/5e97cbb15797e857783642f8/html5/thumbnails/76.jpg)
5] Explain the following search algorithms in your
own words:
(a) Sequential search
(b) Sequential search with early termination
(c) Binary/logarithmic search
76
QUIZ
![Page 77: QUIZ: What does this program a: .BLOCK 2 do? · – The divide-and-conquer approach can be applied over and over again until each subtask is manageable. 9. ... Algorithm . Calculate](https://reader033.fdocuments.us/reader033/viewer/2022042119/5e97cbb15797e857783642f8/html5/thumbnails/77.jpg)
QUIZ Binary Search
-50 -28 -23 -10 -4 0 3 41 28 37 60 155 200
How many comparisons are needed to find the key 60 in this array?Show first, last, and middle for each iteration.
![Page 78: QUIZ: What does this program a: .BLOCK 2 do? · – The divide-and-conquer approach can be applied over and over again until each subtask is manageable. 9. ... Algorithm . Calculate](https://reader033.fdocuments.us/reader033/viewer/2022042119/5e97cbb15797e857783642f8/html5/thumbnails/78.jpg)
7.5 Sorting algorithms
78
![Page 79: QUIZ: What does this program a: .BLOCK 2 do? · – The divide-and-conquer approach can be applied over and over again until each subtask is manageable. 9. ... Algorithm . Calculate](https://reader033.fdocuments.us/reader033/viewer/2022042119/5e97cbb15797e857783642f8/html5/thumbnails/79.jpg)
Sorting
SortingArranging items in a collection so that there is an ordering on one (or more) of the fields in the items
Sort KeyThe field (or fields) on which the ordering is based
Sorting algorithmsAlgorithms that order the items in the collection based on the sort key
79
![Page 80: QUIZ: What does this program a: .BLOCK 2 do? · – The divide-and-conquer approach can be applied over and over again until each subtask is manageable. 9. ... Algorithm . Calculate](https://reader033.fdocuments.us/reader033/viewer/2022042119/5e97cbb15797e857783642f8/html5/thumbnails/80.jpg)
Selection Sort
80
Figure 7.11 Example of a selection sort (sorted elements are shaded)
![Page 81: QUIZ: What does this program a: .BLOCK 2 do? · – The divide-and-conquer approach can be applied over and over again until each subtask is manageable. 9. ... Algorithm . Calculate](https://reader033.fdocuments.us/reader033/viewer/2022042119/5e97cbb15797e857783642f8/html5/thumbnails/81.jpg)
81
10010111235200132
Show the swapped elements with arrows.Show the sorted elements with shading.
QUIZ Selection SortHandout at the end
![Page 82: QUIZ: What does this program a: .BLOCK 2 do? · – The divide-and-conquer approach can be applied over and over again until each subtask is manageable. 9. ... Algorithm . Calculate](https://reader033.fdocuments.us/reader033/viewer/2022042119/5e97cbb15797e857783642f8/html5/thumbnails/82.jpg)
Solution
82
![Page 83: QUIZ: What does this program a: .BLOCK 2 do? · – The divide-and-conquer approach can be applied over and over again until each subtask is manageable. 9. ... Algorithm . Calculate](https://reader033.fdocuments.us/reader033/viewer/2022042119/5e97cbb15797e857783642f8/html5/thumbnails/83.jpg)
83
24101112352012
Show the swapped elements with arrows.Show the sorted elements with shading.
Quick work for next timeHandout at the end
![Page 84: QUIZ: What does this program a: .BLOCK 2 do? · – The divide-and-conquer approach can be applied over and over again until each subtask is manageable. 9. ... Algorithm . Calculate](https://reader033.fdocuments.us/reader033/viewer/2022042119/5e97cbb15797e857783642f8/html5/thumbnails/84.jpg)
84
Solution
![Page 85: QUIZ: What does this program a: .BLOCK 2 do? · – The divide-and-conquer approach can be applied over and over again until each subtask is manageable. 9. ... Algorithm . Calculate](https://reader033.fdocuments.us/reader033/viewer/2022042119/5e97cbb15797e857783642f8/html5/thumbnails/85.jpg)
85
Selection Sort PseudocodeSelection SortSet firstUnsorted to 0WHILE (not sorted yet)
Find smallest unsorted itemSwap firstUnsorted item with the smallestSet firstUnsorted to firstUnsorted + 1
Not sorted yetcurrent < length – 1
Red steps are abstract (not concrete)
![Page 86: QUIZ: What does this program a: .BLOCK 2 do? · – The divide-and-conquer approach can be applied over and over again until each subtask is manageable. 9. ... Algorithm . Calculate](https://reader033.fdocuments.us/reader033/viewer/2022042119/5e97cbb15797e857783642f8/html5/thumbnails/86.jpg)
86
Find smallest unsorted itemSet indexOfSmallest to firstUnsortedSet index to firstUnsorted + 1WHILE (index <= length – 1)
IF (data[index] < data[indexOfSmallest])Set indexOfSmallest to index
Set index to index + 1Set index to indexOfSmallest
Swap firstUnsorted with smallestSet tempItem to data[firstUnsorted]Set data[firstUnsorted] to data[indexOfSmallest]Set data[indexOfSmallest] to tempItem
![Page 87: QUIZ: What does this program a: .BLOCK 2 do? · – The divide-and-conquer approach can be applied over and over again until each subtask is manageable. 9. ... Algorithm . Calculate](https://reader033.fdocuments.us/reader033/viewer/2022042119/5e97cbb15797e857783642f8/html5/thumbnails/87.jpg)
87
![Page 88: QUIZ: What does this program a: .BLOCK 2 do? · – The divide-and-conquer approach can be applied over and over again until each subtask is manageable. 9. ... Algorithm . Calculate](https://reader033.fdocuments.us/reader033/viewer/2022042119/5e97cbb15797e857783642f8/html5/thumbnails/88.jpg)
SKIP Bubble Sort and Insertion Sort, just remember that they are as fast
(efficient) as Selection Sort
88
Is it possible to devise a more efficient sorting algorithm?
![Page 89: QUIZ: What does this program a: .BLOCK 2 do? · – The divide-and-conquer approach can be applied over and over again until each subtask is manageable. 9. ... Algorithm . Calculate](https://reader033.fdocuments.us/reader033/viewer/2022042119/5e97cbb15797e857783642f8/html5/thumbnails/89.jpg)
89
Is it possible to devise a more efficient sorting algorithm?
Yes, but, in order to do this, we have to understand subprograms and
recursion.
![Page 90: QUIZ: What does this program a: .BLOCK 2 do? · – The divide-and-conquer approach can be applied over and over again until each subtask is manageable. 9. ... Algorithm . Calculate](https://reader033.fdocuments.us/reader033/viewer/2022042119/5e97cbb15797e857783642f8/html5/thumbnails/90.jpg)
7.6 Recursive Algorithms
To understand Quick Sort, we need:
• a new control structure: subprogram
• a new concept: recursion
90
![Page 91: QUIZ: What does this program a: .BLOCK 2 do? · – The divide-and-conquer approach can be applied over and over again until each subtask is manageable. 9. ... Algorithm . Calculate](https://reader033.fdocuments.us/reader033/viewer/2022042119/5e97cbb15797e857783642f8/html5/thumbnails/91.jpg)
Subprogram StatementsWe can give a section of code a name and use that name as a statement in another part of the programWhen the name is encountered, the processing in the other part of the program halts while the named code is executedWhen execution is finished, the first part of the program resumes execution
That section of code is called a subprogram
91
![Page 92: QUIZ: What does this program a: .BLOCK 2 do? · – The divide-and-conquer approach can be applied over and over again until each subtask is manageable. 9. ... Algorithm . Calculate](https://reader033.fdocuments.us/reader033/viewer/2022042119/5e97cbb15797e857783642f8/html5/thumbnails/92.jpg)
92
Figure 7.14 Subprogram flow of control
![Page 93: QUIZ: What does this program a: .BLOCK 2 do? · – The divide-and-conquer approach can be applied over and over again until each subtask is manageable. 9. ... Algorithm . Calculate](https://reader033.fdocuments.us/reader033/viewer/2022042119/5e97cbb15797e857783642f8/html5/thumbnails/93.jpg)
We have already used subprograms in Python!
Ee called them
• Functions– int() float() ord() chr() str() etc.
• Methods– list.append() string.upper() string.find() etc.
93
Do you remember what each of them
does?
![Page 94: QUIZ: What does this program a: .BLOCK 2 do? · – The divide-and-conquer approach can be applied over and over again until each subtask is manageable. 9. ... Algorithm . Calculate](https://reader033.fdocuments.us/reader033/viewer/2022042119/5e97cbb15797e857783642f8/html5/thumbnails/94.jpg)
Input to SubprogramsWhat if the subprogram needs data from the calling unit? This data is called input.
ParametersIdentifiers listed in parentheses beside the subprogram declaration; sometimes called formal parametersArgumentsIdentifiers listed in parentheses on the subprogram call; sometimes called actual parameters
94
![Page 95: QUIZ: What does this program a: .BLOCK 2 do? · – The divide-and-conquer approach can be applied over and over again until each subtask is manageable. 9. ... Algorithm . Calculate](https://reader033.fdocuments.us/reader033/viewer/2022042119/5e97cbb15797e857783642f8/html5/thumbnails/95.jpg)
Formal and actual params in Python
def disp(a, b):print a, 'and', b
x, y = 1, 2disp(x, y)
95
![Page 96: QUIZ: What does this program a: .BLOCK 2 do? · – The divide-and-conquer approach can be applied over and over again until each subtask is manageable. 9. ... Algorithm . Calculate](https://reader033.fdocuments.us/reader033/viewer/2022042119/5e97cbb15797e857783642f8/html5/thumbnails/96.jpg)
What if the subprogram needs to give data back to the calling unit? This data is called output.
Void subprograms They do not return a value, just perform certain actions>>> print(“Enter a positive integer”)>>> printer(3, 4)
Value-returning subprograms >>> a = input(“Enter a positive integer”)The keyword RETURN is used in many programming languages
96
![Page 97: QUIZ: What does this program a: .BLOCK 2 do? · – The divide-and-conquer approach can be applied over and over again until each subtask is manageable. 9. ... Algorithm . Calculate](https://reader033.fdocuments.us/reader033/viewer/2022042119/5e97cbb15797e857783642f8/html5/thumbnails/97.jpg)
What if the subprogram needs to give data back to the calling unit? This data is called output.
Void subprograms They do not return a value, just perform certain actions>>> print(“Enter a positive integer”)>>> printer(3, 4)
Value-returning subprograms >>> a = input(“Enter a positive integer”)The keyword RETURN is used in many programming languages
97
Do not confuse returning values with printing or displaying values. Returning means that a value is
given back to the main program!
![Page 98: QUIZ: What does this program a: .BLOCK 2 do? · – The divide-and-conquer approach can be applied over and over again until each subtask is manageable. 9. ... Algorithm . Calculate](https://reader033.fdocuments.us/reader033/viewer/2022042119/5e97cbb15797e857783642f8/html5/thumbnails/98.jpg)
Value-returning function in Python
def sum(a, b):return a + b
x, y = 1, 2print sum(x, y)
98
Compare to this previous function, which returns void (does not return anything)
![Page 99: QUIZ: What does this program a: .BLOCK 2 do? · – The divide-and-conquer approach can be applied over and over again until each subtask is manageable. 9. ... Algorithm . Calculate](https://reader033.fdocuments.us/reader033/viewer/2022042119/5e97cbb15797e857783642f8/html5/thumbnails/99.jpg)
Value-returning vs. void Subprograms
99
![Page 100: QUIZ: What does this program a: .BLOCK 2 do? · – The divide-and-conquer approach can be applied over and over again until each subtask is manageable. 9. ... Algorithm . Calculate](https://reader033.fdocuments.us/reader033/viewer/2022042119/5e97cbb15797e857783642f8/html5/thumbnails/100.jpg)
Source: AP CS Principles –Course and Exam Descriptions
QUIZIs MoveAndTurn a value-returning or void subprogram?
![Page 101: QUIZ: What does this program a: .BLOCK 2 do? · – The divide-and-conquer approach can be applied over and over again until each subtask is manageable. 9. ... Algorithm . Calculate](https://reader033.fdocuments.us/reader033/viewer/2022042119/5e97cbb15797e857783642f8/html5/thumbnails/101.jpg)
Subprograms are very important tools for abstraction
Other popular names for subprograms:• sub• subroutine• function• procedure• module• method
101
![Page 102: QUIZ: What does this program a: .BLOCK 2 do? · – The divide-and-conquer approach can be applied over and over again until each subtask is manageable. 9. ... Algorithm . Calculate](https://reader033.fdocuments.us/reader033/viewer/2022042119/5e97cbb15797e857783642f8/html5/thumbnails/102.jpg)
Recursion RecursionThe ability of a subprogram to call itselfBase caseThe case to which we have an answer General caseThe case that expresses the solution in terms of a call to itself with a smaller version of the problem
102
![Page 103: QUIZ: What does this program a: .BLOCK 2 do? · – The divide-and-conquer approach can be applied over and over again until each subtask is manageable. 9. ... Algorithm . Calculate](https://reader033.fdocuments.us/reader033/viewer/2022042119/5e97cbb15797e857783642f8/html5/thumbnails/103.jpg)
Recursion – Factorial function The factorial of a positive integer is defined (non-recursively) as the integer times the product of all the integers between itself and 1:
N! = 1⋅2⋅3⋅ … ⋅NBut an alternate recursive definition is possible:
N! = N⋅(N − 1)!
103
![Page 104: QUIZ: What does this program a: .BLOCK 2 do? · – The divide-and-conquer approach can be applied over and over again until each subtask is manageable. 9. ... Algorithm . Calculate](https://reader033.fdocuments.us/reader033/viewer/2022042119/5e97cbb15797e857783642f8/html5/thumbnails/104.jpg)
Two definitions for the factorial Iterative: N! = 1⋅2⋅3⋅ … ⋅NRecursive: N! = N⋅(N − 1)!
Base caseFact(0) = 1 (0! is 1)
General CaseFact(N) = N ⋅ Fact(N-1) (for N ≥ 1)
104
![Page 105: QUIZ: What does this program a: .BLOCK 2 do? · – The divide-and-conquer approach can be applied over and over again until each subtask is manageable. 9. ... Algorithm . Calculate](https://reader033.fdocuments.us/reader033/viewer/2022042119/5e97cbb15797e857783642f8/html5/thumbnails/105.jpg)
Is this a valid recursion?
Source: http://www.urbandictionary.com/define.php?term=recursion105
![Page 106: QUIZ: What does this program a: .BLOCK 2 do? · – The divide-and-conquer approach can be applied over and over again until each subtask is manageable. 9. ... Algorithm . Calculate](https://reader033.fdocuments.us/reader033/viewer/2022042119/5e97cbb15797e857783642f8/html5/thumbnails/106.jpg)
Base caseFact(1) = 1
General CaseFact(N) = N ⋅ Fact(N-1)
106
How does the genie calculate Fact(3)?
![Page 107: QUIZ: What does this program a: .BLOCK 2 do? · – The divide-and-conquer approach can be applied over and over again until each subtask is manageable. 9. ... Algorithm . Calculate](https://reader033.fdocuments.us/reader033/viewer/2022042119/5e97cbb15797e857783642f8/html5/thumbnails/107.jpg)
Your turn!N! = N * (N − 1)!Base case
Facto(0) = 1 (0! is 1)General Case
Fact(N) = N * Fact(N-1) (for N ≥ 1)
Calculate:0! = 1! = 2! = 5! =
107
![Page 108: QUIZ: What does this program a: .BLOCK 2 do? · – The divide-and-conquer approach can be applied over and over again until each subtask is manageable. 9. ... Algorithm . Calculate](https://reader033.fdocuments.us/reader033/viewer/2022042119/5e97cbb15797e857783642f8/html5/thumbnails/108.jpg)
108
Recursive Factorial algorithm
Write “Enter n”Read nSet result to Factorial(n)Write result + “ is the factorial of “ + n
Factorial(n)IF (n equals 0)
RETURN 1ELSE
RETURN n * Factorial(n-1)
![Page 109: QUIZ: What does this program a: .BLOCK 2 do? · – The divide-and-conquer approach can be applied over and over again until each subtask is manageable. 9. ... Algorithm . Calculate](https://reader033.fdocuments.us/reader033/viewer/2022042119/5e97cbb15797e857783642f8/html5/thumbnails/109.jpg)
109
QUIZ: Write this recursive function in Python
Write “Enter n”Read nSet result to Factorial(n)Write result + “ is the factorial of “ + n
Factorial(n)IF (n equals 0)
RETURN 1ELSE
RETURN n * Factorial(n-1)
![Page 110: QUIZ: What does this program a: .BLOCK 2 do? · – The divide-and-conquer approach can be applied over and over again until each subtask is manageable. 9. ... Algorithm . Calculate](https://reader033.fdocuments.us/reader033/viewer/2022042119/5e97cbb15797e857783642f8/html5/thumbnails/110.jpg)
solution
110
![Page 111: QUIZ: What does this program a: .BLOCK 2 do? · – The divide-and-conquer approach can be applied over and over again until each subtask is manageable. 9. ... Algorithm . Calculate](https://reader033.fdocuments.us/reader033/viewer/2022042119/5e97cbb15797e857783642f8/html5/thumbnails/111.jpg)
111
Recursive Binary Search BinarySearch (first, last)IF (first > last)
RETURN FALSEELSE
Set middle to (first + last)/ 2IF (item equals data[middle])
RETURN TRUEELSE
IF (item < data[middle])BinarySearch (first, middle – 1)
ELSEBinarySearch (middle + 1, last)
![Page 112: QUIZ: What does this program a: .BLOCK 2 do? · – The divide-and-conquer approach can be applied over and over again until each subtask is manageable. 9. ... Algorithm . Calculate](https://reader033.fdocuments.us/reader033/viewer/2022042119/5e97cbb15797e857783642f8/html5/thumbnails/112.jpg)
SKIP QUICKSORT …Just remember that it is faster
(more efficient) than Selection Sort
112
SKIP 7.7 Important Threads
![Page 113: QUIZ: What does this program a: .BLOCK 2 do? · – The divide-and-conquer approach can be applied over and over again until each subtask is manageable. 9. ... Algorithm . Calculate](https://reader033.fdocuments.us/reader033/viewer/2022042119/5e97cbb15797e857783642f8/html5/thumbnails/113.jpg)
Read and take notes: Who am I?
113
I am a mathematician.Why is my picture in abook about computerscience?
![Page 114: QUIZ: What does this program a: .BLOCK 2 do? · – The divide-and-conquer approach can be applied over and over again until each subtask is manageable. 9. ... Algorithm . Calculate](https://reader033.fdocuments.us/reader033/viewer/2022042119/5e97cbb15797e857783642f8/html5/thumbnails/114.jpg)
Read and take notes:Ethical Issues - Open-Source Software
What are the advantages and disadvantages of open-source software?
What are the FSF and the GPL? Are they related?What does the success of Linux suggest about the
future of open-source software?Is open-source software exempt from copyright laws?
(Hint: What happened in 2008?)
114
![Page 115: QUIZ: What does this program a: .BLOCK 2 do? · – The divide-and-conquer approach can be applied over and over again until each subtask is manageable. 9. ... Algorithm . Calculate](https://reader033.fdocuments.us/reader033/viewer/2022042119/5e97cbb15797e857783642f8/html5/thumbnails/115.jpg)
Homework, due Tue, Nov.13:
End-of-chapter questions 30, 31, 34, 35, 36, 66 b, c
Not from text: Calculate 7! by hand, showing all the recursive steps, as done in the lecture examples.
115
![Page 116: QUIZ: What does this program a: .BLOCK 2 do? · – The divide-and-conquer approach can be applied over and over again until each subtask is manageable. 9. ... Algorithm . Calculate](https://reader033.fdocuments.us/reader033/viewer/2022042119/5e97cbb15797e857783642f8/html5/thumbnails/116.jpg)
Chapter review questions• Describe the computer problem-solving process and relate it
to Polya’s How to Solve It list• Distinguish between a simple type and a composite type• Distinguish between a void subprogram and a value-returning
subprogram• Recognize a recursive problem and write a recursive algorithm
to solve it • Distinguish between an unsorted array and a sorted array• Describe the Quicksort algorithm • Apply the linear search, binary search, selection sort and
Quicksort to an array of items by hand• What are the advantages and disadvantages of open-source
software?
116