0% found this document useful (0 votes)
212 views

Algorithms Notes

The document discusses algorithms and their use in computer programming. It begins by defining an algorithm as a set of steps to complete a task. Algorithms are used in software tasks and many common algorithms have been written in books. Variables are also introduced as named values that can change as a program runs. An example algorithm for counting to 10 is provided, as well as an improved version using variables. Pseudocode and flowcharts are presented as ways to describe algorithms without using a specific programming language. Various shapes used in flowcharts are defined. Finally, an example algorithm for finding the largest number in a list is outlined.

Uploaded by

Mahad Imran
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
212 views

Algorithms Notes

The document discusses algorithms and their use in computer programming. It begins by defining an algorithm as a set of steps to complete a task. Algorithms are used in software tasks and many common algorithms have been written in books. Variables are also introduced as named values that can change as a program runs. An example algorithm for counting to 10 is provided, as well as an improved version using variables. Pseudocode and flowcharts are presented as ways to describe algorithms without using a specific programming language. Various shapes used in flowcharts are defined. Finally, an example algorithm for finding the largest number in a list is outlined.

Uploaded by

Mahad Imran
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 6

COMPUTER STUDIES O-LEVEL

1. Introduction
In general, 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. This mini-web takes you through the topic of algorithm

2. Using algorithms
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.

3. 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

COMPILED BY : NISAR ALI -1BEACONHOUSE-S. TOWN BOYS BRANCH

COMPUTER STUDIES O-LEVEL

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.

4. 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.

COMPILED BY : NISAR ALI -2BEACONHOUSE-S. TOWN BOYS BRANCH

COMPUTER STUDIES O-LEVEL

5. 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

6. 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. 3. 4. 5. 6. 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

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.

COMPILED BY : NISAR ALI -3BEACONHOUSE-S. TOWN BOYS BRANCH

COMPUTER STUDIES O-LEVEL

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.

7. Flow chart
A flowchart is a diagram which shows the breakdown of an algorithm into all of the necessary steps and is an alternative to pseudo code A flow chart makes use of some standard shapes joined together by lines

Each step is represented by a symbol and connecting lines show the step-by-step progression through the task. Have a look at the diagram on the right. It shows the process of ordering a burger. There is a clear start, a series of steps, a clear direction of flow and a clear end or finish point. This is a very simple flowchart. For some tasks or systems, the flowchart can be very complex and detailed.

8. Flow chart shapes


Flowcharts are meant to be shared with other people and so it makes sense that everyone uses the same shapes for certain things. As it happens the American National Standard Institute developed a set of symbol that have now become a de facto standard. Symbols are:

COMPILED BY : NISAR ALI -4BEACONHOUSE-S. TOWN BOYS BRANCH

COMPUTER STUDIES O-LEVEL

Start/End - the terminator symbol marks the starting or ending point of the process. It usually contains the word "Start" or "End." Action or Process - a box can represent a single step ("add two cups of flour"), or an entire sub-process ("make bread") within a larger process. Decision - a decision or branching point. Lines representing different decisions emerge from different points of the diamond. Input/Output - represents material or information entering or leaving the process, such as customer order (input) or a product (output). Data storage -indicates a step where data gets stored. For Example "Save Orders" Delay - indicates a delay in the process. For example "Wait 1 day"

9. 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

COMPILED BY : NISAR ALI -5BEACONHOUSE-S. TOWN BOYS BRANCH

COMPUTER STUDIES O-LEVEL

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.

COMPILED BY : NISAR ALI -6BEACONHOUSE-S. TOWN BOYS BRANCH

You might also like