Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 13
1.
2 ALGORITHM SPECIFICATION
Algorithm can be described in three ways.
1. Natural language like English: When this way is choose care should be taken, we should ensure that each & every statement is definite. 2. Graphic representation called flowchart: This method will work well when the algorithm is small& simple. 3. Pseudo-code Method: In this method, we should typically describe algorithms as program, which resembles language like PASCAL & ALGOL 1.2.1 PSEUDO CODE CONVENTIONS What is a Pseudo Code?
Pseudo code is a compact and informal high-level description of a
computer programming algorithm that uses the structural conventions of some programming language.
Pseudo code is an artificial and informal language that helps
programmers develop algorithms.
Pseudo code is a ”text-based” detail(algorithmic) design tool.
Flow charts can be thought of as a graphical alternative to pseudo code. A Flow chart is a schematic representation of an algorithm, or the step-by-step solution of a problem, using some geometric figures. PSEUDO CODE CONVENTIONS The following are set of rules need to be followed while writing algorithms 1. Comments begin with // and continue until the end of line. 2. Blocks are indicated with matching braces { and }. A compound statement can be represented as a block. The body of a procedure also forms a block. Statements are delimited by ;. 3. An identifier begins with a letter. The data types of variables are not explicitly declared. Whether a variable is local or global to a procedure will also be evident from the context. PSEUDO CODE CONVENTIONS 4. Compound data types can be formed with records. Here is an example, Node= Record { data type – 1 data-1; ... ... ... ... ... ... data type – n data – n; node * link; } Here link is a pointer to the record type node. Individual data items of a record can be accessed with and period. 5. Assignment of values to variables is done using the assignment statement. <Variable>:= <expression>; PSEUDO CODE CONVENTIONS 6. There are two Boolean values TRUE and FALSE. Logical Operators AND, OR, NOT Relational Operators <, <=,>,>=, =, != 7. The Looping statements are employed are: For, while and repeat-until While Loop: While < condition > do { <statement-1> ... <statement-n> } As long as condition is TRUE, the statements get executed. When condition becomes FALSE, the loop is exited. The value of condition is evaluated at top of the loop. PSEUDO CODE CONVENTIONS The general form of For loop is For Loop: for variable: = value_1 to value_2 step step do { <statement-1> ... <statement-n> } Here value 1, value 2 and step are arithmetic expressions. A variable of type integer or real or a numerical constant is a simple form of an arithmetic expression. The clause “step step” is optional and taken as +1 if it does not occur. Step could either be positive or negative. Variable is tested for termination at the start of each iteration. PSEUDO CODE CONVENTIONS The repeat-until loop is constructed as follows. repeat-until: repeat <statement-1> ... <statement-n> until<condition> The statements are executed as long as condition is false. The value of condition is computed after executing the statements. The instruction break; can be used within any of the above looping instructions to force exit. In case of nested loops, break; results in the exit of the innermost loop that it is a part of. A return statement within any of the above also will result in exiting the loops. A return statement results in the exit of the function itself. PSEUDO CODE CONVENTIONS 8. A conditional statement has the following forms. If <condition> then <statement> If <condition> then <statement-1>else <statement-1> Here condition is the Boolean expression and statements are arbitrary statements. Case statement: Case { <condition-1> : <statement-1> ... <condition-n> : <statement-n> else : <statement-n+1> } PSEUDO CODE CONVENTIONS Here statement 1, statement 2 etc. could be either simple statement or compound statements. A case statement is interpreted as follows. If condition 1 is true, statement 1 gets executed and case statement is exited. If statement 1 is false, condition 2 is evaluated. If condition 2 is true, statement 2 gets executed and the case statement exited and so on. If none of the conditions are true, statements + 1 is executed and the case statement is exited. The else clause is optional. 9. Elements of multidimensional arrays are accessed using [ and ]. For example, if A is a two dimensional array, the <i,j>th element of an array is denoted as A[i,j]. 10. Input and output are done using the instructions read & write. PSEUDO CODE CONVENTIONS 11. There is only one type of procedure: Algorithm, the heading takes the form, Algorithm Name (Parameter lists) Where Name is the name of the procedure and parameter list is a listing of the procedure parameters. The body has one or more statements enclosed with braces { and }. An algorithm may or may not return values. Simple variables to procedures are passed by value. Arrays and records are passed by reference. An array name or record name is treated as a pointer to the respective data type. PSEUDO CODE CONVENTIONS • As an example, the following algorithm fields & returns the maximum of ‘n’ given numbers. Algorithm Max(A,n) // A is an array of size n { b := A[1]; for i:= 1 to n do if A[i] > b then b :=A[i]; return b; } • In this algorithm (named Max), A & n are procedure parameters. b & i are Local variables. Discuss the Pseudo code conventions for expressing algorithms. [7M] [R16 November - 2019 III-II Supplementary]
Write different pseudo code conventions used to represent an