Algorithms
An algorithm is simply a series of steps taken to accomplish a goal. Algorithms can be written in plain language. A large computer program contains many algorithms.
Before creating an algorithm to solve a problem, you must be sure that you understand the problem. If you don’t, you will probably create an algorithm that solves the wrong problem.
Basic Steps in algorithm and flowchart:
The algorithm and flowchart include following three types of control structures.
1. Input: In the input step algorithm takes the data to be processed.
2. Sequence: In the sequence structure, statements are placed one after the other and the execution takes place starting from up to down.
3. Branching (Selection): In branch control, there is a condition and according to a condition, a decision of either TRUE or FALSE is achieved. In the case of TRUE, one of the two branches is explored; but in the case of FALSE condition, the other alternative is taken. Generally, the ‘IF-THEN’ is used to represent branch control.
4. Loop (Repetition): The Loop or Repetition allows a statement(s) to be executed repeatedly based on certain loop condition e.g. WHILE, FOR loops.
5. Output: In the output step algorithm takes the data to be processed.
How to write algorithms:
Step 1: Define your algorithms input: Many algorithms take in data to be processed, e.g. to find the sum of two numbers, we need two numbers as input.
Step 2: Define the variables: Algorithm's variables allow you to store the input values and use it for more than one place. Declare number1 and numbers2 as variables for addition algorithm.
Step 3: Outline the algorithm's operations: Use input variable for computation purpose, e.g. to find sum of two numbers add the number1 and number2 variables and store the value in new variable (say) sum. An algorithm's operations can take the form of multiple steps and even branch, looping depending requirement.
Step 4: Output the results of your algorithm's operations: Print the algorithm result, i.e the sum of the two numbers, stored in sum variable.
Algorithm to calculate sum of two numbers:
Before developing the algorithm, let us first identify the input, process and output:
• Input: Number whose sum is required
• Process: Add the two numbers and store in sum
• Output: sum of the two numbers
Algorithm:
Step 1: Start
Step 2: Read A, B
Step 3: Sum = A + b
Step 4: Print sum
Step 5: Stop
Algorithm to to find the square of a number:
Step 1: Input a number and store it to num
Step 2: Compute num * num and store it in square
Step 3: Print square
Why do we need an Algorithm?
A programmer writes a program to instruct the computer to do certain tasks as desired. The computer then follows the steps written in the program code. Therefore, the programmer first prepares a roadmap of the program to be written, before actually writing the code. Without a roadmap, the programmer may not be able to clearly visualise the instructions to be written and may end up developing a program which may not work as expected.
Writing an algorithm is mostly considered as a first step to programming. Once we have an algorithm to solve a problem, we can write the computer program for giving instructions to the computer in high level language. If the algorithm is correct, computer will run the program correctly, every time. So, the purpose of using an algorithm is to increase the reliability, accuracy and efficiency of obtaining solutions.
Characteristics of a good algorithm
• Precision — the steps are precisely stated or defined.
• Uniqueness — results of each step are uniquely defined and only depend on the input and the Result of the preceding steps.
• Finiteness — the algorithm always stops after a finite number of steps.
• Input — the algorithm receives some input.
• Output — the algorithm produces some output
While writing an algorithm, it is required to clearly identify the following:
• The input to be taken from the user
• Processing or computation to be performed to get the desired result
• The output desired by the user
Representation of Algorithms
There are two common methods of representing an algorithm —flowchart and pseudocode. Either of the methods can be used to represent an algorithm while keeping in mind the following:
• it showcases the logic of the problem solution, excluding any implementation details
• it clearly reveals the flow of control during execution of the program