### Definition

An algorithm is a finite sequence of step by step, discrete, unambiguous instructions for solving a particular problem.

Or

Algorithm is a step-by-step procedure for solving a particular problem.

Or

An algorithm is an ordered set of unambiguous executable steps, defining a terminating process.

An algorithm, named after 9^{th} centaury Persian mathematician al-Khowarazmi, is simply a set of rules for carrying out some calculation, either by hand, or, more usually, on a machine. An algorithm is like a recipe. It lists the steps involved in accomplishing a task. It can be defined in programming terms as a set of detailed, unambiguous and ordered instructions developed to describe the processes necessary to produce the desired output from the given input. The algorithm is written in simple English and is not a formalized procedure, however to be useful there are some principles which should be adhered to. An algorithm must:

- Be precise and unambiguous
- Give the correct solution in all cases and
- Eventually end.

Algorithms can be represented in different ways. They are:

1) Natural languages

2) Pseudocode

3) Flowcharts

4) Control tables

## Pseudocode

Pseudocode is easy to read and write, as it represents the statements of an algorithm in English. Pseudocode is really structured English. It is English which has been formalised and abbreviated to look very like a high-level computer language.

There is currently no standard pseudocode. Different textbook authors seem to adopt their own special techniques and set of rules, which often resemble a particular programming language. But as a guideline, try the following conventions:

- Statements are written in simple English.
- Each instruction is written on a separate line.
- Keywords and indentation are used to signify particular control structures.
- Groups of statements may be formed into modules and that group given a name.

If a programmer uses words and phrases in his or her pseudocode that are in line with basic computer operations, then the translation from the pseudocode algorithm to a specific programming language becomes quite simple. We could break down the computers operations into six basic operations and represent these operations in pseudocode. Each operation can be represented as an English instruction with keywords and indentation to signify a particular control structure:

**A computer can receive information**– when a computer is required to receive information or input from a particular source, whether it is a keyboard, disk or other source, the verbs*Read*and*Get*can be used

1 2 3 4 5 |
e.g. Read student name Get system date Read number_1, number_2 |

**A computer can put out information**– when a computer is required to supply information or output to a device the verbs*Print*,*Write*or*Put*could be used

1 2 3 4 5 6 7 |
e.g. Print student number Write ’Program completed’ Write customer record to master file Put out name, address, postcode |

**A computer can perform arithmetic**– most programs require the computer to perform some sort of mathematical calculation, or formula. You could use actual mathematical symbols or the words for those symbols

1 2 3 4 5 6 7 8 9 |
e.g. Add Number to Total Total = Total + Number Divide Totalmarks by Studentcount SalesTax = Costprice * 0.10 Compute C = (F – 32) * 5/9 |

**A computer can assign a value to a piece of data**– there are many cases where a programmer will need to assign a value to a piece of information. Firstly to give data an initial value in pseudocode the verbs,*Initialise*or*Set*can be used. Secondly to assign a value as a result of some processing, the symbol “=” is written. Thirdly, to keep a piece of information for later use the verbs*Save*or*Store*could be used

1 2 3 4 5 6 7 |
e.g. Initialise total accumulators to zero Set StudentCount to 0 TotalPrice = CostPrice + SalesTax Store CustomerNumber in LastCustomerNumber |

**A computer can compare two pieces of information and select one of two alternatives**– an important ability is the comparison of two pieces of information and then as a result of the comparison select on of two alternate actions. To represent this operation in pseudocode the keywords IF, THEN and ELSE can be used

1 2 3 4 5 6 7 8 9 |
e.g. IF student is parttime THEN Add 1 to parttimecount ELSE Add 1 to fulltimecount ENDIF |

Note the use of indentation to emphasize the THEN and ELSE options and the use of the delimiter ENDIF to close the operation.

**A computer can repeat a group of actions**– when there is a sequence of steps which need to be repeated, the keywords DOWHILE and ENDDO could be used

1 2 3 4 5 6 7 8 9 |
e.g. DOWHILE studenttotal < 50 Read student record Write student name, address to report Add 1 to Studenttotal ENDDO |

Once again note the use of indentation. It is easy to read where the loop starts and ends. Alternatives to the above could be WHILE and ENDWHILE.

## Examples

**Problem: Write an algorithm for adding two integers.**

### Algorithm

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
Algorithm: Add(x,y) To add two integer x and y Inputs: x and y Output: Sum of x and y Start 1: Read two integers x and y 2: Set sum = 0 3: sum = x + y 4: Display sum Stop |

**Problem: Write an algorithm to find the greatest number among two integers.**

### Algorithm

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
Algorithm: Greatest(x,y) To find the greatest among two integers Inputs: x and y Output: Greatest integer among x and y Start 1: Read two integers x and y 2: IF x is greater than y THEN: 2.1: Display “X is greater” ELSE 2.1: Display “Y is greater” END IF Stop |

## Leave a Reply