Presentation is loading. Please wait.

Presentation is loading. Please wait.

Lecture 5: Developing Procedural Thinking (How to think like a programmer) B Burlingame 30 Sept 2015.

Similar presentations


Presentation on theme: "Lecture 5: Developing Procedural Thinking (How to think like a programmer) B Burlingame 30 Sept 2015."— Presentation transcript:

1 Lecture 5: Developing Procedural Thinking (How to think like a programmer) B Burlingame 30 Sept 2015

2 Ref: xkcd: www.xkcd.com

3 Announcements Homework 2 due up front Homework 3 posted, due two weeks Thursday lab was canceled last week  Thursday students have a choice: Catch up now or catch up the week of Nov 11 Still don’t have Canvas…

4 The Plan for Today Program design process Algorithms, decomposition, and step-wise refinement  Example Program design example Basic decisions in MatLab 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)http://www.gliffy.com  Don’t use Powerpoint or Word. They are time wasters and SUPER ugly.

5 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

6 Program Design Process 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

7 Program Design Process – step 1 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 State the problem in a clear and concise manner Example Write a program to find the distance between two points P1P2 or P1P2 P1 P2 or Better Write a program to find the straight line distance between two points Is the problem statement okay?

8 Program Design Process – step 2 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 Write a program to find the straight line distance between two points Inputs Outputs

9 Program Design Process – step 3 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 DecomposeRefine Write a program to find the straight line distance between two points

10 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) via http://www.cs.xu.edu/csci170/08f/sect01/Overheads/WhatIsAnAlgorithm.html (visited 19JUN2009) http://www.cs.xu.edu/csci170/08f/sect01/Overheads/WhatIsAnAlgorithm.html

11 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. Pseudocode 2. Flow Charts We’ll use the pseudocode method first.

12 Pseudocode Pseudocode (also called Program Design Language, PDL) is English-like statements that precisely describe specific operations 1 :  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 1 This definition comes from, McConnel, S. (1993). Code Complete, Microsoft Press, Redmond, WA, p. 54.

13 Pseudocode – First Pass 1. Prompt user to enter points 2. Get points from user 3. Calculate the straight line distance 4. Return distance Write a program to find the straight line distance between two points Comments 1. High level – just the major steps 2. Focus on the logic

14 Pseudocode - Refinement 1. Start 2. Declare variables: X1, Y1, X2, Y2, Distance 3. Prompt user to enter X1 and Y1 4. Prompt user to enter X2 and Y2 5. Calculate the straight line distance, Distance 6. Return Distance 7. Stop Write a program to find the straight line distance between two points Comments 1. Refine high level ideas down to computable actions What could still be refined?

15 Flowcharts - 1 Flowcharts  A graphical tool that diagrammatically depicts the steps and structure of an algorithm or program SymbolName/MeaningSymbolMeaning Process – Any type of internal operation: data transformation, data movement, logic operation, etc. Connector – connects sections of the flowchart, so that the diagram can maintain a smooth, linear flow Input/Output – input or output of data Terminal – indicates start or end of the program or algorithm 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

16

17 Calculating the Distance, D 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 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 P2 D X Y

18 Program Design Process – step 4 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 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?

19 Program Design Process – step 5 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 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.

20 Matlab Distance Function function [distance]=dist() % This method matches our algorithm x = int8(1); y = int8(2); p1 = [0, 0]; p2 = [0, 0]; p1=input('Enter [x1, y1]: '); p2=input('Enter [x2, y2]: '); distance = sqrt( (p1(x)-p2(x))^2 + (p1(y)-p2(y))^2 ); function [distance]=dist(p1, p2) % A bit more MatLabish way to do things distance = sqrt( (p1(1)-p2(1))^2 + (p1(2)-p2(2))^2 );

21 Program Design Process – step 6 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 Test your code with cases that you know the answer to. Try the ‘boundary’ cases to make sure your code works for them too.

22 Algorithm Practice Find the maximum of two values

23 Algorithm – Items to Consider Is the problem statement clear and concise?  Could be better: Return the maximum numerical value between two floating point numbers What are the inputs?  Two floating point numbers (a and b) What are the outputs?  The larger value

24 Algorithm – Possible Solution Pseudocode 1.Receive A and B from function call 2.If A is larger 2.1. Set max equal to A 3.Otherwise 3.1. Set max equal to B 4.Return max Matlab Function function [max]=mymax(A, B) if(A < B) max = B; else max = A; end Flowchart


Download ppt "Lecture 5: Developing Procedural Thinking (How to think like a programmer) B Burlingame 30 Sept 2015."

Similar presentations


Ads by Google