Presentation is loading. Please wait.

Presentation is loading. Please wait.

Fundamentals of Algorithms MCS - 2 Lecture # 3. Representation of Algorithms.

Similar presentations


Presentation on theme: "Fundamentals of Algorithms MCS - 2 Lecture # 3. Representation of Algorithms."— Presentation transcript:

1 Fundamentals of Algorithms MCS - 2 Lecture # 3

2 Representation of Algorithms

3 Representing algorithms  Algorithms may be specified in many forms  called representations or notations.  The representation of an algorithm is extremely important  It must rapidly convey the algorithm’s meaning with the least amount of effort by the reader.  No one representation is suitable for all algorithms.  It is worth getting familiar with a number of alternative forms.  The best representation for one algorithm may be the worst for another.

4 Representation of Algorithms Algorithms are generally represented by either  Verbal  The algorithm is expressed in words, sentences and paragraphs.  Usually very verbose, long, and often inaccurate.  Algebraic  The algorithm is expressed mathematically with symbols and formulas.  This is usually a very concise representation.  Tabular  The algorithm is represented by one or more rectangular grids (tables, arrays or matrices) with entries in the grids.  This method is useful for summarizing large selections.

5 Representation of Algorithms  Hierarchical  The algorithm is presented as a break-out diagram.  Data Flow Diagram  The algorithm is shown as a set of boxes that show the actions to be performed.  Flow chart  The algorithm is represented in the form of a diagram with action boxes linked by lines.  Pseudo code  The algorithm is presented as a set of instructions written using a mixture of natural language and mathematical notation.

6 Pseudo code  It is a mixture of  natural language and mathematical notation  independent of any programming language.  Reflects the fact “short-term communication” between members working on a specific project  The code itself and other documents are used for long-term archival purposes.  Pseudo code can be written in very formal constructs. It can be used almost like a free- verse description of what the program needs to do.  What the input is?  What the output should be?

7 Rules for Pseudo code No strict rules exist. Write only one statement per line Capitalize initial keyword Indent to show hierarchy End multi-line structures Keep statements language independent

8 A Recommended Pseudo code Format  Documentation Keyword  Action Keywords  Flow Control Keywords

9 Documentation Keyword Documentation keywords describe what needs to be done or provides information about why something is being done.  TASK  TASK statement is something that the program must perform.  REM  REM statement is merely a remark or comment.

10 Action Keywords Action keywords are the lines that actually do the work  Common Action Keywords  Input: READ, OBTAIN, GET  Output: PUT, PRINT, DISPLAY, SHOW  Compute: COMPUTE, CALCULATE, DETERMINE  Initialize: SET, INIT  Add one: INCREMENT, DECREMENT

11 Flow Control Keywords  Sequence Calculate Pay – sequence Begin input hours input rate pay = hours * rate print pay End Sum of 2 Numbers – sequence Begin input x, y sum = x + y print sum End

12 Flow Control Keywords  Selection IF (test condition) Statement(s) to be executed if test condition is TRUE ELSE Statement(s) to be executed if test condition is FALSE Calculate Pay with Overtime - selection Begin input hours, rate if hours  40 then pay = hours * rate else pay = 40 * rate + (hours – 40) * rate * 1.5 print pay End IF amount < 1000 interestRate =.06 // the “yes” or “true” action ELSE interestRate =.10 // the “no” or “false” action ENDIF

13 Flow Control Keywords  Repetition - Case 1  LOOP WHILE (test condition) Statement(s) to be executed if test condition is TRUE  Repetition - Case 2  LOOP Statement(s) to be executed if test condition is TRUE WHILE: (test condition) Average of 10 Numbers – iteration with a for loop Begin sum = 0 for i = 1 to 10 input x sum = sum + x avg = sum / 10.0 print avg End

14 Flow Control Keywords  Repetition - Case 3  LOOP UNTIL (test condition) Statement(s) to be executed if test condition is FALSE  Repetition - Case 4  LOOP Statement(s) to be executed if test condition is FALSE UNTIL (test condition)

15 Good Luck ! ☻


Download ppt "Fundamentals of Algorithms MCS - 2 Lecture # 3. Representation of Algorithms."

Similar presentations


Ads by Google