Algorithms
Algorithms
The first thing that needs to be done when solving a problem using a programming language is to describe how to solve it! This will then lead the programmer into producing efficient code that will perform the task. There are a number of ways to describe a problem that we wish to write a program for. However we decide to describe a problem, it is known as an ‘algorithm’. An algorithm is simply a list of instructions that describe how to do a particular task. All that needs to happen is that the programmer analyses a task and writes down (or draws) step-by-step how the solution is to solve the problem. A programmer may also decide to write algorithms to describe the problem in the first place, and then write algorithms to describe the solution. Whichever approach is taken, there is some good news when writing algorithms. There are no rules dictating what you have to do and how you have to do it!
-
- You can produce an algorithm by writing a list of sentences.
- You can produce an algorithm by drawing a diagram, for example, a flow chart. This is explained in section 3.2.1.
- You can produce an algorithm by writing instructions using pseudo-code. This is explained in 3.2.1.
As has already been said, there are no rules to follow when writing algorithms. As long as your argument is structured and logical you can write or draw what you like. However, a popular way of representing algorithms is to use pseudo-code.
Describing a solution using algorithms
When writing algorithms, there are two broad questions to ask:
-
- How can the problem be solved?
- What 'special cases' need to be taken into account in the code?
Special cases are situations such as:
-
- What happens if a record cannot be found?
- What happens if a file is empty?
- What happens if the wrong data type is entered?
Dealing with special cases is initially down to experience. The more practice you have of writing algorithms and programs, the easier it is to decide what special cases need to be dealt with.