1 / 22

Lecture 2: Developing Procedural Thinking (How to think like a programmer)

Lecture 2: Developing Procedural Thinking (How to think like a programmer). BJ Furman 06FEB2012. The Plan for Today. Program design process Algorithms, decomposition, and step-wise refinement Example Program design example. Learning Objectives.

ilyssa
Télécharger la présentation

Lecture 2: Developing Procedural Thinking (How to think like a programmer)

An Image/Link below is provided (as is) to download presentation 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. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Lecture 2: Developing Procedural Thinking(How to think like a programmer) BJ Furman 06FEB2012

  2. The Plan for Today • Program design process • Algorithms, decomposition, and step-wise refinement • Example • Program design example

  3. 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

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

  5. Define the problem List the inputs and outputs Design the solution algorithm Check the algorithm by hand P2 P1 P2 P1 P1 P2 Write the program code Test the program code Program Design Process – step 1 State the problem in aclear and concise manner Example Write a program to find the distance between two points Is the problem statement okay? or or Better Write a program to find the straight line distance between two points

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

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

  8. 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)

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

  10. 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 Pseudocode 1This definition comes from, McConnel, S. (1993). Code Complete, Microsoft Press, Redmond, WA, p. 54.

  11. Pseudocode – First Pass Write a program to find the straight line distance between two points • Prompt user to enter points • Get points from user • Calculate the straight line distance • Display distance to the monitor Comments • High level – just the major steps • Focus on the logic

  12. Pseudocode - Refinement Write a program to find the straight line distance between two points • Start • Declare variables: X1, Y1, X2, Y2, D • Prompt user to enter X1 and Y1 • Display X1 and Y1 to the monitor • Prompt user to enter X2 and Y2 • Display X2 and Y2 to the monitor • Calculate the straight line distance, D • Display D to the monitor • Stop Comments • Refine high level ideas down to computable actions What could still be refined?

  13. Define the problem List the inputs and outputs Y2 D P2 P2 Y1 P1 P1 Design the solution algorithm X1 X2 Check the algorithm by hand Write the program code D Y X Test the program code Calculating the Distance, D Write a program to find the straight line distance between two points How do you find D?

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

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

  16. Program Code • Note the structure of the program

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

  18. Algorithm Practice • Find the midpoint between two points

  19. Algorithm – Items to Consider • Is the problem statement clear and concise? • Could be better: • “Find the midpoint on a line between two points • What are the inputs? • X and Y coordinates of the two points: • x1, y1 and x2, y2 • What are the outputs? • The midpoint between the points: • Xm, Ym

  20. M Algorithm – Items to Consider, cont. Y2 P2 Y1 P1 X1 X2 • Solution algorithm • Point must be on the line • Midpoint means ‘halfway’ between the points • Halfway in x and halfway in y • This should suggest a solution strategy

  21. Review

  22. References

More Related