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

2. 5. Notice the use of variables within an algorithm. A variable is actually a location in memory within the computer. It is also a good idea to avoid spaces as it can make spotting a problem very difficult. PSEUDO CODE Algorithms are often described by setting out 'pseudo-code' Pseudo-code does not follow any particular computer language. AN EXAMPLE ALGORITHM Set out an algorithm to count to 10 counts to 10 . 4. Variables can be almost any name but it makes sense to use sensible description such as Sales_Price and not be too long. 3. For instance change the algorithm above to add up to *any* number 1.but this is actually a poor algorithm because it is too specific. Some statements will test for some condition and branch to different parts of the algorithm. 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. 4.A = B + 1 which copies the value of another variable called B and adds 1 to it. The program changes the data held in that location by refering to the variable name. It lays out the algorithm as a series of statements written in English (or any local language). 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. 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 . . 3. 2.

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

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. but the key is to apply some logical thinking to the problem.1 Clearly 67 is the largest number. find the largest number in a list Consider a list like this: 3. CREATING AN ALGORITHM It takes a bit of practice to lay out an algorithm.9.67. this is done by the FOR EACH statement. Then it loops through the other items and checks each one in turn. 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. . For instance. The IF statement checks to see if a larger item has been found.6.2.