Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1of 4

ALGORITHM An 'algorithm' is the name given to a defined set of steps used to complete a task.

For instance you could define an algorithm to make a cup of tea. You start by filling the kettle, then place a teabag in the cup and so on. In computer terms, an algorithm describes the set of steps needed to carry out a software task. For example there are algorithms worked out for sorting a list efficiently. There are algorithms to create random numbers, how to shade a pixel, how to calculate something. Many books have been written containing ready-made algorithms. This is excellent news for a programmer as it means they do not have to re-invent the wheel every time they want to carry out a common task. Important note: Algorithms are *not* computer code, a programmer using 'C++' can use the same algorithm as someone programming in 'Java', they use the same steps, just coded differently. VARIABLES Before we set out an example, you need to know about a vital part of programming. Namely the meaning of 'variable'. A variable is to computing as a brick is to building. It is absolutely basic to computing. A variable is a named value that can be changed as the program runs. For instance define a 'variable' called A and set it to a starting value such as 10 i.e. A = 10 Now anywhere in the program that needs to change that value can refer to A For example Let A = 10 then another part of the program changes it by doing something to it, like this A = A + 1 which adds 1 to the variable called A or A = 3 which re-defines the value of A or

A = B + 1 which copies the value of another variable called B and adds 1 to it. A variable is actually a location in memory within the computer. The program changes the data held in that location by refering to the variable name. Variables can be almost any name but it makes sense to use sensible description such as Sales_Price and not be too long. It is also a good idea to avoid spaces as it can make spotting a problem very difficult. AN EXAMPLE ALGORITHM Set out an algorithm to count to 10 1. 2. 3. 4. Set a variable to a starting value of 0 (let's call the variable 'X') Add 1 to the current value of X If ten steps have taken place then finish If not then repeat step 2

This does the job - it counts to 10 - but this is actually a poor algorithm because it is too specific. What if the programmer wants to add to 11 instead? A good algorithm will be as general as possible to allow for the widest use. For instance change the algorithm above to add up to *any* number 1. 2. 3. 4. 5. Set a variable Y to hold the number of counts needed (say 11) Set a variable X to a starting value of 0 Add 1 to the current value of X Is X the same as Y yet? If not then repeat step three otherwise finish

Just by using an extra variable to hold the number of additions the algorithm can now deal with any count. Notice the use of variables within an algorithm. PSEUDO CODE Algorithms are often described by setting out 'pseudo-code' Pseudo-code does not follow any particular computer language. It lays out the algorithm as a series of statements written in English (or any local language). Some statements will test for some condition and branch to different parts of the algorithm.

Example. Write pseudo code for an algorithm that adds data to an existing file. 1. 2. 3. 4. 5. 6. Ensure the data to be added is present in memory Identify the file to be opened Open the file Move to the end of the file Append the data from memory to the end of the file Save and close the file

As you can see, each line is an action to do something i.e. each line has a verb that describes the action : 'Ensure', 'Identify', 'Open', 'Move', 'Append' and 'Save' and it is laid out in the order of the algorithm. Pseudo-code continued From the previous page .... 1. Ensure the data to be added is present in memory 2. Identify the file to be opened 3. Open the file ........ As it stands this pseudo code example is a bit too 'high level'. It makes a lot of assumptions, for instance, that the person knows how to 'Ensure the data is present in memory'. If more detail is needed the pseudo code can be expanded: 1. Ensure the data to be added is present in memory by if data is present in memory then carry on else load data into memory 2. Identify the file to be opened 3. Open the file 4. Move to the end of the file 5. Append the data from memory to the end of the file 6. Save and close the file Notice that the word 'if' was used in section 1 above and the term 'else'. These terms are called 'conditional' statements and they cause the algorithm to branch depending on some condition being met. This kind of statement is called a 'selection' because it determines the next instruction to be carried out. The 'if' tests whether a condition is true. If it is true then the next statements are carried out up to the 'else' condition. If the condition is not met then the 'else' code is carried out.

The pseudo code is laid out so that it is easy to see the conditional statements. Indenting code is a very popular way of doing this. CREATING AN ALGORITHM It takes a bit of practice to lay out an algorithm, but the key is to apply some logical thinking to the problem. For instance, find the largest number in a list Consider a list like this: 3,6,2,67,9,1 Clearly 67 is the largest number. One algorithm that would find this is store first list item in variable largest_item For each item in the list store item in variable current_item if current_item > largest_item then largest_item = current_item

End This algorithm takes the first item in the list and sets it to be the largest. Then it loops through the other items and checks each one in turn, this is done by the FOR EACH statement. The IF statement checks to see if a larger item has been found.

You might also like