Algorithm and Pseudocode




 Algorithm is simply put, a well defined computational procedure consisting of a set of instructions, that take some value or set of values, as input, and produces some value or set of values, as output. An algorithm is usually presented in the form of some pseudo-code, which is a mixture of English statement, some mathematical notations and selected keywords from a programming language.

Here’s how it works: INPUT, PROCESS (calculations, decisions and so on) and OUTPUT

Human example: You hear a question via your ears (input). You need to find an answer and you think (process). You then give the answer by speech (output).

Similarly, in a computer system, your keyboard or mouse is an input because you supply or give information to the computer. The CPU decides what to do with the information, which is called process. After calculation, it shows the result on the screen, which is called output.

Let us do a simple example.

You want the computer to add 2 numbers and give you the result (sum).

5+4

The numbers 5 and 4 are called value1 and value2 respectively.

To get the sum the computer would need to add these 2 values. Hence:

sum= value1 + value2
          5    +    4
= 9

In computer studies, we use mathematical symbols to do calculation just like in a normal mathematics class. The only difference is that the multiply symbol changes from X to *. This is to avoid confusion. Also, the divider symbol changes to just a forward slash /.

The list of symbols:


+         addition
-         subtraction
*         multiplication
/          division
=         assignment
( )       brackets for grouping calculations
  


Here is how an algorithm will look using pseudocode:

Read value1, value2
SUM=value1 + value2
DISPLAY SUM

Pseudocode Language

Pseudocode does not require that you write a complete sentence. For instance someone asks if you’re a boy or female a complete answer would be “I am a girl”. But you could answer rapidly by only answering “girl”. Saves lots of time and tying!

Example: You are a teacher who created a program to see if a student has passed. Let us make as if the computer was a human. You would talk to him like this: “If the student has 50 points exactly or more than 50 then he has passed.” Remember: Read between lines. What to do if the student hasn’t got 50 or more points? It means that he has failed! You must think about the “WHAT IF THIS HAPPENS?”

Let us put this in pseudocode: Remember! We need not write complete sentences.
>= this symbol means ‘greater or equal to’

Read mark
            If mark >= 50 Then
                        STUDENT PASS
            ELSE
                        STUDENT FAIL
            END IF

Let me explain the if, else, end if.
We use If when we need to compare. Here, we are comparing numbers. Hence it means ‘if the mark is greater or equal to 50’.
We use Then to tell the computer what to do if the mark is really equal to or greater than 50.
We use ELSE to tell the computer what to do if the mark if lower than 50.
We use END IF in all psuedocode. It simply tells the computer that it needs to stop calculating or processing.

Example: You are looking for a pair of shoes not exceeding $50. In this example we use the <= (is less than or equal to).

Read price
            If price <= 50 Then
                        BUY
            ELSE
                        DON’T BUY
            END IF
                       

Comments