Presentation is loading. Please wait.

Presentation is loading. Please wait.

CMSC 104: Peter Olsen, Fall 99Lecture 9:1 Algorithms III Representing Algorithms with pseudo-code.

Similar presentations


Presentation on theme: "CMSC 104: Peter Olsen, Fall 99Lecture 9:1 Algorithms III Representing Algorithms with pseudo-code."— Presentation transcript:

1 CMSC 104: Peter Olsen, Fall 99Lecture 9:1 Algorithms III Representing Algorithms with pseudo-code

2 CMSC 104: Peter Olsen, Fall 99Lecture 9:2 Goal l A method for representing algorithms that: oIs easy to use (or it won’t be) oClear and precise (or why bother?) oUnderstandable by others without special training.

3 CMSC 104: Peter Olsen, Fall 99Lecture 9:3 Algorithm Definition An algorithm for a problem is a sequence of finite number of steps arranged in a specific logical order which, when executed, will produce a correct solution for a specific problem. “An algorithm is any finite sequence of steps connecting a problem with its solution.”

4 CMSC 104: Peter Olsen, Fall 99Lecture 9:4 Requirements for a Solution Algorithm – Unambiguous – Finite – Defined Input – Desired Output – Generality – Correctness – Efficiency

5 CMSC 104: Peter Olsen, Fall 99Lecture 9:5 Algorithm Representations English Pseudo-code (“Structured English”) Flow chart

6 CMSC 104: Peter Olsen, Fall 99Lecture 9:6 Algorithms in English l To find a zero using Newton’s Method oBegin the procedure by letting x be the current point. oUsing their definitions, calculate f(x) and f’(x). oIf f’(x) is less than an error tolerance, e, quit. oIf it is not, calculate the next point: y = x - f(x)/f’(x) oContinue until f(x) is “close enough” to zero. x is the zero of f.

7 CMSC 104: Peter Olsen, Fall 99Lecture 9:7 Algorithms in English l Easy to understand. l Easy to use. l Not very clear or precise.

8 CMSC 104: Peter Olsen, Fall 99Lecture 9:8 Pseudo-code English like Semi-formal Limited vocabulary Intended specifically to design and describe algorithms.

9 CMSC 104: Peter Olsen, Fall 99Lecture 9:9 Pseudo-code The main purpose of a pseudo-code is to define the procedural logic of an algorithm in a simple, easy-to-understand manner for readers, who may or may not be proficient in computer programming.

10 CMSC 104: Peter Olsen, Fall 99Lecture 9:10 Pseudo-code Used in designing algorithms. Used in communicating to users. Used in implementing algorithms as programs. Used in debugging logic errors in programs. Used in documenting programs for future maintenance and expansion purposes.

11 CMSC 104: Peter Olsen, Fall 99Lecture 9:11 Pseudo-code Must have a limited vocabulary. Must be easy to learn. Must produce simple, English-like narrative notation. Must be capable of describing all algorithms, regardless of their complexity.

12 CMSC 104: Peter Olsen, Fall 99Lecture 9:12 Control Structures of All Computer Programs Sequence Selection Repetition

13 CMSC 104: Peter Olsen, Fall 99Lecture 9:13 Sequence Series of steps or statements that are executed in the order they are written. Example: Read taxable income Read filing status Compute income tax Print income tax

14 CMSC 104: Peter Olsen, Fall 99Lecture 9:14 Sequence A sequence can be thought of as one logical step. InOut Sequence

15 CMSC 104: Peter Olsen, Fall 99Lecture 9:15 Selection Defines one or two courses of action depending on the evaluation of a condition. A condition is an expression that is either true or false. Example if condition (is true) then-part else else-part end_if

16 CMSC 104: Peter Olsen, Fall 99Lecture 9:16 Nested Selection if status is equal to 1 print “Single” else if status is equal to 2 print “Married filing jointly” else if status is equal to 3 print “Married filing separately end_if end_if end_if

17 CMSC 104: Peter Olsen, Fall 99Lecture 9:17 Nested Selection SINGLE = 1; MARRIEDJOINT = 2; MARRIEDSEP = 3 if status is equal to SINGLE print “Single” else if status is equal to MARRIEDJOINT print “Married filing jointly” else if status is equal to MARRIEDSEP print “Married filing separately end_if end_if end_if

18 CMSC 104: Peter Olsen, Fall 99Lecture 9:18 Nested Selection l Defines a logical block with one entrance, one exit, and two or more paths inside. If Endif Input Output

19 CMSC 104: Peter Olsen, Fall 99Lecture 9:19 Repetition Specifies a block of one or more statements that are repeatedly executed until a condition is satisfied. while condition loop-body end_while

20 CMSC 104: Peter Olsen, Fall 99Lecture 9:20 Repetition While while m is less than m, … end while For loop for m from 1 to n, … end for Until do … until m is less than n, end do

21 CMSC 104: Peter Olsen, Fall 99Lecture 9:21 Repetition l A logical block with one input, one output, and one return loop. While EndWhile

22 CMSC 104: Peter Olsen, Fall 99Lecture 9:22 Conventions Each pseudo-code statement consists of keywords that describe operations and some appropriate, English-like description of operands. Each statement should be written on a separate line. Continuation lines should be indented. Goal is clarity

23 CMSC 104: Peter Olsen, Fall 99Lecture 9:23 Conventions (II) Sequence statements should begin with unambiguous words (verbs -- compute, set, initialize). Selection statements then-part and else-part should be indented. Selection statements end with the keyword end_if.

24 CMSC 104: Peter Olsen, Fall 99Lecture 9:24 Conventions (III) Repetition statements end with end_while. Loop-bodies are indented. All words in a pseudo-code statement must be chosen to be unambiguous, and as easy as possible to understand by non- programmers. Enclose comments between /* and */

25 CMSC 104: Peter Olsen, Fall 99Lecture 9:25 Example Set correctIncomeInput to ‘n’ while correctIncomeInput is equal to ‘n’ print “Enter taxable income: Should not be less than 50000 read income if income is equal to or greater than 50000 set correctIncomeInput to ‘y’ end_if end_while

26 CMSC 104: Peter Olsen, Fall 99Lecture 9:26 Flowcharting Flowcharting is another technique used in designing and representing algorithms. A flowchart is a graph consisting of geometric shapes that are connected by flow lines.

27 CMSC 104: Peter Olsen, Fall 99Lecture 9:27 Symbols Terminal symbol Process symbol Input-Output symbol

28 CMSC 104: Peter Olsen, Fall 99Lecture 9:28 Symbols (III) Flow Lines indicating the logical sequence of statements

29 CMSC 104: Peter Olsen, Fall 99Lecture 9:29 Flowchart If Keyboard Process


Download ppt "CMSC 104: Peter Olsen, Fall 99Lecture 9:1 Algorithms III Representing Algorithms with pseudo-code."

Similar presentations


Ads by Google