What is an Algorithm
- An algorithm is a finite number of accurate, unambiguous steps that solve a problem or task.
- Characteristics of an algorithm.
- The number of steps must be finite.
- The steps must be precise.
- The steps must be unambiguous.
- The steps must have flow of control from one process to another.
- The steps must terminate.
- The steps must lead to an output or solution.
Steps for developing an algorithm
- Any algorithm that is used to solve a problem or task can be divided into three main steps.
- The input step
- The processing step
- The output step
-
- The input step
- This is where the instructions from the user are being gathered.
- The processing step
- This is where the instructions are worked through. It can involved all or some of the following steps:
- Assignment - values are assigned to variables, constants and/or literals
- Decision - checking for any condition to be followed
- Repition - repeat a task a specific number of times
- The output step
- This step is used to display result(s).
Control Structures
- The steps that are identified for preparing algorithms can be written using three basic control structures.
- Sequence
- Selection
- Iteration (Repetition/Loop)
- 1. Sequence
- Is used when you have instructions to be carried out in a particular order. The instructions/steps are carried out in the order that they were written. For example:
-
- Step1: Start
- Step2: Get two numbers store in num1 and num2
- Step3: Add the two number and store it in answer
- Step4: Display answer
- Step5: Stop
- The order would be step 1,2,3,4,5.
- 2. Selection
- Is used to carry out instruction(s) associated with particular conditions. The choice of option will be dependent on whether the condition is true or false. For example:
-
- Step1: Start
- Step2: Accept Score
- Step3: If score is more than 59 display student has passed else display student has failed
- Step4: Stop
In the above situation the program will display passed if only student score is abovw 59 and failed only when score is 59 and below.
3. Iteration
Is used to repeat certain instruction(s) any number of times. There are two types of iteration:
- Bounded Iteration
- Unbounded Iteration
- Bounded Iteration
- Is the process of repeating a set number of steps or instructions a fixed number of times. For example For Loops.
-
- Step1: Start
- Step2: For i=0 to 9
- Step3: Input num[i]
Step4: Sum=sum+num[i]
Step5: Display num[i]
Step6: Loop
Step7: Display Sum
Step8: Stop
Unbounded Iteration
Is the process of repeating a set of steps or instructions until a particular condition becomes false. For example While Loops.
- Step1: Start
- Step2: Input num
- Step3: While num <> 999
- Step4: Sum=sum+num
- Step5: Loop
- Step6: Display Sum
- Step7: Stop