lecture 3: developing procedural thinking (how to think like a programmer) b burlingame 16 feb 2016
DESCRIPTION
Announcements Homework 1 due up front Trouble with homework 1, hang out after class Homework 2 posted, due two weeks Read chapters 1 – 3 in text Office hours 5 – 7, Eng 213TRANSCRIPT
![Page 1: Lecture 3: Developing Procedural Thinking (How to think like a programmer) B Burlingame 16 Feb 2016](https://reader035.vdocuments.net/reader035/viewer/2022062504/5a4d1b937f8b9ab0599c259b/html5/thumbnails/1.jpg)
Lecture 3: Developing Procedural Thinking(How to think like a programmer)
B Burlingame16 Feb 2016
![Page 2: Lecture 3: Developing Procedural Thinking (How to think like a programmer) B Burlingame 16 Feb 2016](https://reader035.vdocuments.net/reader035/viewer/2022062504/5a4d1b937f8b9ab0599c259b/html5/thumbnails/2.jpg)
Ref: xkcd: www.xkcd.com/1195
![Page 3: Lecture 3: Developing Procedural Thinking (How to think like a programmer) B Burlingame 16 Feb 2016](https://reader035.vdocuments.net/reader035/viewer/2022062504/5a4d1b937f8b9ab0599c259b/html5/thumbnails/3.jpg)
Announcements Homework 1 due up front
Trouble with homework 1, hang out after class
Homework 2 posted, due two weeks Read chapters 1 – 3 in text Office hours 5 – 7, Eng 213
![Page 4: Lecture 3: Developing Procedural Thinking (How to think like a programmer) B Burlingame 16 Feb 2016](https://reader035.vdocuments.net/reader035/viewer/2022062504/5a4d1b937f8b9ab0599c259b/html5/thumbnails/4.jpg)
The Plan for Today Program design process Algorithms, decomposition, and step-wise refinement
Example Program design example For flowcharts, use a proper graphics program.
Visio is the standard and is in labs Gliffy is online, free, and good enough (http://www.gliffy.com) Don’t use Powerpoint or Word. They are time wasters and
SUPER ugly.
![Page 5: Lecture 3: Developing Procedural Thinking (How to think like a programmer) B Burlingame 16 Feb 2016](https://reader035.vdocuments.net/reader035/viewer/2022062504/5a4d1b937f8b9ab0599c259b/html5/thumbnails/5.jpg)
Learning Objectives List and describe the steps in designing a
computational solution (computer program) to a problem
Articulate what is meant by an algorithm Apply the steps to a particular problem
![Page 6: Lecture 3: Developing Procedural Thinking (How to think like a programmer) B Burlingame 16 Feb 2016](https://reader035.vdocuments.net/reader035/viewer/2022062504/5a4d1b937f8b9ab0599c259b/html5/thumbnails/6.jpg)
Program Design ProcessDefine the problem
List the inputs and outputs
Design the solution algorithm
Check the algorithm by hand
Write the program code
Test the program code
![Page 7: Lecture 3: Developing Procedural Thinking (How to think like a programmer) B Burlingame 16 Feb 2016](https://reader035.vdocuments.net/reader035/viewer/2022062504/5a4d1b937f8b9ab0599c259b/html5/thumbnails/7.jpg)
Program Design Process – step 1Define the problem
List the inputs and outputs
Design the solution algorithm
Check the algorithm by hand
Write the program code
Test the program code
State the problem in aclear and concise manner
ExampleWrite a program to find the
distance between two points
P1 P2or
P1 P2 P1
P2or
BetterWrite a program to find the straight line distance between two points
Is the problem statement okay?
![Page 8: Lecture 3: Developing Procedural Thinking (How to think like a programmer) B Burlingame 16 Feb 2016](https://reader035.vdocuments.net/reader035/viewer/2022062504/5a4d1b937f8b9ab0599c259b/html5/thumbnails/8.jpg)
Program Design Process – step 2Define the problem
List the inputs and outputs
Design the solution algorithm
Check the algorithm by hand
Write the program code
Test the program code
Write a program to find the straight line distance between two points
Inputs• •
Outputs• •
![Page 9: Lecture 3: Developing Procedural Thinking (How to think like a programmer) B Burlingame 16 Feb 2016](https://reader035.vdocuments.net/reader035/viewer/2022062504/5a4d1b937f8b9ab0599c259b/html5/thumbnails/9.jpg)
Program Design Process – step 3Define the problem
List the inputs and outputs
Design the solution algorithm
Check the algorithm by hand
Write the program code
Test the program code
Decompose Refine
Write a program to find the straight line distance between two points
![Page 10: Lecture 3: Developing Procedural Thinking (How to think like a programmer) B Burlingame 16 Feb 2016](https://reader035.vdocuments.net/reader035/viewer/2022062504/5a4d1b937f8b9ab0599c259b/html5/thumbnails/10.jpg)
Definition of an Algorithm An algorithm is a well-ordered collection of
unambiguous and effectively computable operations, that when executed, produces a result and halts in a finite amount of time. Well-ordered means the steps are in a clear order Unambiguous means the operations described are understood
by a computing agent without further simplification A computing agent is the thing that is supposed to carry out the
algorithm Effectively computable means the computing agent can actually
carry out the operation
This definition comes from, An Invitation to Computer Science (Gersting/Schneider) viahttp://www.cs.xu.edu/csci170/08f/sect01/Overheads/WhatIsAnAlgorithm.html (visited 19JUN2009)
![Page 11: Lecture 3: Developing Procedural Thinking (How to think like a programmer) B Burlingame 16 Feb 2016](https://reader035.vdocuments.net/reader035/viewer/2022062504/5a4d1b937f8b9ab0599c259b/html5/thumbnails/11.jpg)
Program Design Process – step 3,cont.
Define the problem
List the inputs and outputs
Design the solution algorithm
Check the algorithm by hand
Write the program code
Test the program code
Two approaches are often used to help think through the steps to be carried out by the program code:1. Pseudocode2. Flow ChartsWe’ll use the pseudocode method first.
![Page 12: Lecture 3: Developing Procedural Thinking (How to think like a programmer) B Burlingame 16 Feb 2016](https://reader035.vdocuments.net/reader035/viewer/2022062504/5a4d1b937f8b9ab0599c259b/html5/thumbnails/12.jpg)
Pseudocode Pseudocode (also called Program Design Language,
PDL) is English-like statements that precisely describe specific operations1: Action statements Focuses on the logic of the program Avoids language-specific elements Written at a level so that code can be
generated almost automatically. Will likely need to refine in more and more detail
1This definition comes from, McConnel, S. (1993). Code Complete, Microsoft Press, Redmond, WA, p. 54.
![Page 13: Lecture 3: Developing Procedural Thinking (How to think like a programmer) B Burlingame 16 Feb 2016](https://reader035.vdocuments.net/reader035/viewer/2022062504/5a4d1b937f8b9ab0599c259b/html5/thumbnails/13.jpg)
Pseudocode – First Pass
1. Prompt user to enter points2. Get points from user3. Calculate the straight line distance4. Return distance
Write a program to find the straight line distance between two points
Comments1. High level – just the major steps2. Focus on the logic
![Page 14: Lecture 3: Developing Procedural Thinking (How to think like a programmer) B Burlingame 16 Feb 2016](https://reader035.vdocuments.net/reader035/viewer/2022062504/5a4d1b937f8b9ab0599c259b/html5/thumbnails/14.jpg)
Pseudocode - Refinement
1. Start2. Declare variables: X1, Y1, X2, Y2, Distance3. Prompt user to enter X1 and Y14. Prompt user to enter X2 and Y25. Calculate the straight line distance6. Return Distance7. Stop
Write a program to find the straight line distance between two points
Comments1. Refine high level ideas down to computable actionsWhat could still be refined?
![Page 15: Lecture 3: Developing Procedural Thinking (How to think like a programmer) B Burlingame 16 Feb 2016](https://reader035.vdocuments.net/reader035/viewer/2022062504/5a4d1b937f8b9ab0599c259b/html5/thumbnails/15.jpg)
Flowcharts A graphical tool that diagrammatically depicts
the steps and structure of an algorithm or program Symbol Name/Meaning Symbol Meaning
Process – Any type of internal operation: data transformation, data movement, logic operation, etc.
On-page Connector – connects sections of the flowchart, so that the diagram can maintain a smooth, linear flow
Input/Output – input or output of data
Off-Page Connector – same as above, but directs flow to a given page number
Decision – evaluates a condition or statement and branches depending on whether the evaluation is true or false
Flow lines – arrows that indicate the direction of the progression of the program
Sub-process – Any type of internal operation documented in another flowchart
Terminal – indicates start or end of the program or algorithm
![Page 16: Lecture 3: Developing Procedural Thinking (How to think like a programmer) B Burlingame 16 Feb 2016](https://reader035.vdocuments.net/reader035/viewer/2022062504/5a4d1b937f8b9ab0599c259b/html5/thumbnails/16.jpg)
start
Initialzation
Prompt for
X1, Y1
Prompt for
X2, Y2
Calculate distance
end
function dist()
p1 = [x1, y1]P2 = [x2, y2]distance = 0
distance = я( (p1x – p2x)² + (p1y – p2y)² )
![Page 17: Lecture 3: Developing Procedural Thinking (How to think like a programmer) B Burlingame 16 Feb 2016](https://reader035.vdocuments.net/reader035/viewer/2022062504/5a4d1b937f8b9ab0599c259b/html5/thumbnails/17.jpg)
Calculating the Distance, DDefine the problem
List the inputs and outputs
Design the solution algorithm
Check the algorithm by hand
Write the program code
Test the program code
Write a program to find the straight line distance between two points
P1
P2
X1 X2
Y1
Y2 D
How do you find D?
P1
P2D
X
Y22 YXD
1212
YYYXXX
22 1212 YYXXD
![Page 18: Lecture 3: Developing Procedural Thinking (How to think like a programmer) B Burlingame 16 Feb 2016](https://reader035.vdocuments.net/reader035/viewer/2022062504/5a4d1b937f8b9ab0599c259b/html5/thumbnails/18.jpg)
Program Design Process – step 4Define the problem
List the inputs and outputs
Design the solution algorithm
Check the algorithm by hand
Write the program code
Test the program code
Write a program to find the straight line distance between two points
What values of Xi, Yi (where i=1, 2) would be good to test the algorithm with?
![Page 19: Lecture 3: Developing Procedural Thinking (How to think like a programmer) B Burlingame 16 Feb 2016](https://reader035.vdocuments.net/reader035/viewer/2022062504/5a4d1b937f8b9ab0599c259b/html5/thumbnails/19.jpg)
Program Design Process – step 5Define the problem
List the inputs and outputs
Design the solution algorithm
Check the algorithm by hand
Write the program code
Test the program code
If you have refined your algorithm sufficiently, writing the code should proceed straightforwardly from it.If not, continue refining the algorithm until you can write the code directly from it.Your pseudocode can be turned into the comments for your code.
We’ll start writing code, next week
![Page 20: Lecture 3: Developing Procedural Thinking (How to think like a programmer) B Burlingame 16 Feb 2016](https://reader035.vdocuments.net/reader035/viewer/2022062504/5a4d1b937f8b9ab0599c259b/html5/thumbnails/20.jpg)
Program Design Process – step 6Define the problem
List the inputs and outputs
Design the solution algorithm
Check the algorithm by hand
Write the program code
Test the program code
Test your code with cases that you know the answer to.Try the ‘boundary’ cases to make sure your code works for them too.
![Page 21: Lecture 3: Developing Procedural Thinking (How to think like a programmer) B Burlingame 16 Feb 2016](https://reader035.vdocuments.net/reader035/viewer/2022062504/5a4d1b937f8b9ab0599c259b/html5/thumbnails/21.jpg)
Algorithm Practice Find the maximum of n values
![Page 22: Lecture 3: Developing Procedural Thinking (How to think like a programmer) B Burlingame 16 Feb 2016](https://reader035.vdocuments.net/reader035/viewer/2022062504/5a4d1b937f8b9ab0599c259b/html5/thumbnails/22.jpg)
Algorithm – Items to Consider Is the problem statement clear and
concise? Could be better:
Return the maximum numerical value amongst n floating point numbers
What are the inputs? n floating point numbers
What are the outputs? The largest value
![Page 23: Lecture 3: Developing Procedural Thinking (How to think like a programmer) B Burlingame 16 Feb 2016](https://reader035.vdocuments.net/reader035/viewer/2022062504/5a4d1b937f8b9ab0599c259b/html5/thumbnails/23.jpg)
Algorithm – Possible SolutionPseudocode
1. Start2. Initialize number = 0, counter = 0, n = 0, max = -∞3. Get the quantity of numbers from user4. Store that quantity in n 5. Is counter > n ?6. add 1 to counter7. get number8. is (number > max) ?9. max = number10. go to line 511. otherwise12. print max13.end
Flowchart
Notice the loops!
start
Init
Get the qty of number from the user
Store qty in n
counter > n
Add 1 to counter
Number> max?
max = number
Get number
Print max
End
number = 0counter = 0n = 0max = -ʺ
False True
YesNo
function find_max
return max
![Page 24: Lecture 3: Developing Procedural Thinking (How to think like a programmer) B Burlingame 16 Feb 2016](https://reader035.vdocuments.net/reader035/viewer/2022062504/5a4d1b937f8b9ab0599c259b/html5/thumbnails/24.jpg)
Find average start
Init
Get the qty of number from the user
Store qty in n
counter > n
Add 1 to counter
Get number
End
number = 0counter = 0n = 0avg = 0sum = 0
False True
function find_average
return avg
sum += number
Handle stats
(n, sum)
A
B
AB
return avg
Start handle stats
Avg = sum / n
end handle stats
return avg
function handle_stats(n, sum)