Outline:
- What is an algorithm with examples
- Properties
- Representation
- Pseudocode
- Flowcharts
- Examples
An algorithm is a term commonly used in computer science and mathematics. It is a 'problem-solving method’. You must be familiar with this term. It is the finite set of instructions, logic and step by step procedure execute in a finite amount of time. You can assume it as a to-do list for programmers.
Understand the meaning of the above definition.
A finite set of instructions: there should be a limited number of instructions
Logic: there should be logic, how to solve a particular problem.
Step by step: it consists of a few steps to follow while writing the actual code.
A finite amount of time: It has a finite number of instructions that have to be performed in a finite amount of time.
It is independent of the programming language. It is also called a procedure. The computer can not understand instructions written for human beings. It tells a human being what steps should take to perform a specific task or solve a problem. These steps should write sequentially. Some points to keep in mind while writing an algorithm are:
- There is no specific format or well-defined standard or fix syntax or language
- When writing an algorithm for a computer program, we can use code constructs which are common in all programming language like loops, flow control, variable declaration etc.
Example: School Routine
Algorithms are not limited to computing or mathematics. They can be applied to our daily lives. Let's check out this example.
When a kid goes to school, he follows these steps.
- Wake up early in the morning
- Brush his teeth and take a bath
- Wear uniform
- Have breakfast
- Get ready for school
- Go to the bus stand
- Wait for the transportation
- Get the bus
- Go to school
Properties/Characteristics:
An algorithm has the following properties.
Input:
There should be input.
Output:
There must be output as well. Otherwise, the algorithm is useless.
Finiteness:
It should terminate after a finite number of steps.
Determinacy:
Each step should clear, precise and unambiguous.
Effectiveness:
Each step of an algorithm executes effectively.
Generality:
It is equally applicable to all types of inputs. If the input changes the output changes accordingly.
Why do we need algorithms:
When you are writing a computer program, the first step is to write an algorithm. It is independent of the programming language. The purpose of an algorithm is to clearly express what you want to do. The steps which has to follow while solving the problem. In programming, the algorithm is a part of a programmer's job. It is his task to translate an algorithm successfully into a program.
Algorithms are also useful in analyzing computational resources like time to run a program that is computational time, space allocated by a program or memory management, etc.
Representation:
An algorithm can be represented either in pseudocode or flowcharts. Both techniques are used by coders, programmers and designers.
What is pseudocode? How to write an algorithm using pseudocode?
Pseudocode is an arbitrary language for describing algorithms. It only focuses on logic irrespective of the programming language. The purpose is to convey the problem-solving method as a text outline. It can easily be understood by a non-programmer as well. Ideally, pseudocode does not contain syntax and keywords.
What is a flowchart? How to write an algorithm using a flowchart?
A flowchart is the graphical representation of an algorithm. It consists of different geometrical shapes that describe the flow of a program. It describes the sequence of steps with the help of geometrical shapes involved in a problem. Directed arrows show the flow of the program.
Example 1: A simple program
Addition of two numbers
x = y+z
Pseudocode:
Start
Declare variables: num1, num2, result
Result = num1 + num2
Display result
Stop
Explanation:
In the first step, declare three variables. You need to add two numbers and then store the result in the third variable.
In the second step, the actual task that is addition is performed. This task is performed internally. If you need to display the results you have to write another instruction.
In the third step, there is an instruction for displaying the result or output.
Check the algorithm, it has all the properties that are stated above. Like input, output, finiteness (only three instructions), effectiveness, determinacy and generality.
Flowchart:
Explanation:
This is another way! A pictorial representation. Have a look at it. All the steps are the same. Different shapes show indicate that which is an input/output. Parallelograms show the input and output of the program. The rectangle shows the processing that is the task. Here addition is performed.
Example 2: Algorithm for control flow statements
Find the largest among three different numbers entered by the user
Pseudocode
Start
Declare three variables a, b, c
Read variables a, b, c
Compare variables using a conditional statement
If (a>b && a>c)
Display 'a’ is the greatest among three numbers
Else if (b>a && b>c)
Display 'b’ is the greatest among three numbers
Else
Display 'c’ is the greatest among three numbers
End
Explanation:
In the first step declare three variables.
In the second step, I write the 'read’ command. It means you ask the user to enter variables. After the user enters these variables, the system will read them.
In the third step, there is a decision making statement. Check if a is greater than or not. If a is greater than b then proceed further. Check whether a is greater than c as well. If this condition is also true, then proceed further.
Flowchart
Explanation:
In the first step, the processing box is there for the declaration of three variables
In the second step, the input/output box is there for input.
In the third step, a rhombus (labelled as a>b) is there. It means some decision making task. It compares two variables at a time.
The same comparison process in the next step.
In the last step again parallelograms are there to display the results/ outputs
Example 3: Algorithm for iterative statements (condition controlled statement do-while loop)
Print number 1 to 5
Pseudocode
Start
Declare and define variable Count = 1
Do
Display count
Count = count+1
While count <6
End
Explanation:
In the first step, a variable is declared and defined.
In the second step, a loop begins. A do-while loop.
In the third step, an instruction ‘ Display count’ is there.
The third step repeats until the condition is not satisfied.
In the fourth step, the count variable is incremented by 1.
In the fifth step, the condition is there. How many times does the loop repeat itself? The loop repeats until the variable count becomes 5.
So, the loop iterates 5 times and display the value of count each time.
Flowchart
Important terms:
Condition controlled loops: do-while loop is a condition controlled loop. Its iterations are based on specific conditions. It repeatedly checks whether the condition met or not.
Control flow statements: looping, decision making, branching statements are control flow statements.
Iterative statements: as the name implies it is a repetitive process. for loop is an iterative statement.
Syntax: in simple words, you can say it is the grammar of a programming language. It is a set of rules that specify the correct sequence of words and phrases.
&& (AND AND condition): it is also called logical AND operator. It returns a Boolean value that is either true or false.
Multiple Choice Questions
1. Algorithm is defined as a finite set of instructions executed in a finite amount of time.
- A finite amount of time
- 10 minutes
- Infinite amount of time
2. Algorithm is independent of programming language, so it has no fixed syntax.
- A fixed syntax
- No fixed syntax
3. Generality of the algorithm means it is valid for various set of inputs. If input changes, the output changes accordingly.
- True
- False
4. Algorithms are helpful in analyzing computational time.
- Wind speed
- Computational time
- None
5. When you are going to write a computer program, the first step is to write its algorithm.
- Algorithm
- Code
- Name
- None
6. Algorithms can be represented by pseudocode or flowcharts.
- True
- False
7. A single problem has plenty of algorithms.
- One
- Two
- Many
- Plenty
- c and d
8. Pseudocode is written in a high-level programming language
- True
- False