Module 1 - General Introduction On Algorithms
Module 1 - General Introduction On Algorithms
Learning objectives
After studying this lesson, student should be able to:
Discuss what an algorithm is and how to use it to represent the solution of a
problem.
Use Flowchart and Pseudocode represent algorithms
Contents
Contents................................................................................................................ 1
I. ALGORITHM..................................................................................................... 3
I.1. Definition......................................................................................................... 3
I.2. Qualities of a good algorithm................................................................................. 3
I.3. How to write an algorithm..................................................................................... 3
I.4. Example of algorithm........................................................................................... 4
II. REPRESENTATION OF AN ALGORITHM...............................................................4
II.1. Flowchart........................................................................................................ 4
II.2. Pseudocode...................................................................................................... 6
III. ALGORITHM STRUCTURES............................................................................. 7
III.1 Sequence......................................................................................................... 7
III.2 Selection (Decision)........................................................................................... 8
III.3 Repetition (Looping)........................................................................................ 10
APPLICATION EXERCISES...................................................................................... 12
Module 1 – General Introduction on Algorithms 2
I. ALGORITHM
I.1. Definition
Algorithms are one of the most basic tools that are used to develop the problem-solving logic.
An algorithm is defined as a finite sequence of instructions that when provided with a set of
input values, produces an output and then terminates. It can also be defined as a series of
steps taken to solve a given problem. Algorithm is not language specific. We can use the
same flowchart to code a program using different programming languages. Many algorithms
can be design for the same task.
Note that algorithms are not computer programs, as they cannot be executed by a computer.
Some properties of algorithm are as follows:
• Finiteness – the algorithm stops after a finite number of instructions are executed.
• Definiteness or unambiguous: There must be no ambiguity in any instruction. This
means that the action specified by the step cannot be interpreted in multiple ways &
can be performed without any confusion.
• Input:- an algorithm accepts zero or more inputs
• Output:- it produces at least one output.
• Effectiveness:- it consists of basic instructions that are realizable. This means that the
instructions can be performed by using the given inputs in a finite amount of time.
• Precision: a good algorithm must have a certain outlined step. The steps should be
exact enough, and not varying.
• Generality: the algorithm must apply to a set of defined inputs.
ST.LOUIS/2021-2022/HND/L100/ICT/INTRODUCTION_TO_ALGORITHMS/
MODULE_1 2
Module 1 – General Introduction on Algorithms 3
We use algorithms in our daily life. For example, to wash hand, the following algorithms
may be used.
1. Start 1. Start
2. Turn on water 2. Turn on water
3. dispense soap 3. dispense soap
4. wash hand till clean 4. Repeat Rub hands together
5. Rince soap off 5. until hand clean
6. Turn off water 6. Rince soap off
7. Dry hand 7. Turn off water
8. Stop 8. Dry hand
9. Stop
The above-mentioned algorithm terminates after six steps. This explains the feature of
finiteness. Every action of the algorithm is precisely defined; hence, there is no scope for
ambiguity.
- By Flowchart
- By Pseudocode
II.1. Flowchart
a) Flowchart Symbols
Flowcharts use simple geometric symbols and arrows to define relationships. Some standard
symbols that are frequently required for flowcharts are shown:
ST.LOUIS/2021-2022/HND/L100/ICT/INTRODUCTION_TO_ALGORITHMS/
MODULE_1 3
Module 1 – General Introduction on Algorithms 4
Manual Input
a developer / programmer.
This is used to represent data input or output from
Magnetic Disk
and to a magnetic disk.
b) Advantages of flowchart
The flowchart shows how the program works before you begin actually coding it. Some
advantages of flowcharting are the following.
c) Limitations of Flowcharts
Flowchart can be used for designing the basic concept of the program in pictorial form, but
cannot be used for programming purposes. Some of the limitations of the flowchart are given
below:
• Complex: The major disadvantage in using flowcharts is that when a program is very
large, the flowcharts may continue for many pages, making them hard to follow.
• Costly: If the flowchart is to be drawn for a huge program, the time and cost factor of
program development may get out of proportion, making it a costly affair.
• Difficult to Modify: Due to its symbolic nature, any change or modification to a
flowchart usually requires redrawing the entire logic again, and redrawing a complex
flowchart is not a simple task.
ST.LOUIS/2021-2022/HND/L100/ICT/INTRODUCTION_TO_ALGORITHMS/
MODULE_1 4
Module 1 – General Introduction on Algorithms 5
II.2. Pseudocode
Pseudocode is a detailed yet readable description of what an algorithm must do, expressed in
a formally-styled natural language rather than in a programming language. It describe the
entire logic of the algorithm so that implementation becomes a rote mechanical task of
translating line by line into source code.
a) Pseudocode Structures
Before going ahead with pseudocode, let us discuss some keywords, which are often used to
indicate input, output and processing operations.
b) Example of pseudocode
ST.LOUIS/2021-2022/HND/L100/ICT/INTRODUCTION_TO_ALGORITHMS/
MODULE_1 5
Module 1 – General Introduction on Algorithms 6
The 'structured' part of pseudocode and flowchart is a notation for representing three general
programming constructs: sequence, selection and repetition. Each of these constructs can
be embedded inside any other construct. It has been proven that three basic constructs for
flow of control are sufficient to implement any 'proper' algorithm.
III.1 Sequence
Sequence construct is a linear progression where one task is performed sequentially after
another. The actions are performed in the same sequence (top to bottom) in which they are
written
Example
ST.LOUIS/2021-2022/HND/L100/ICT/INTRODUCTION_TO_ALGORITHMS/
MODULE_1 6
Module 1 – General Introduction on Algorithms 7
Write an algorithm and flowchart for calculating the perimeter and surface of square, if the
default length of the sides of the square is a.
Flowchart Pseudocode C
Begin #include<stdio.h>
Input a int main()
P=4×a {
S=A×a int a, P, S;
Print P, S printf(“Enter the length”);
END scanf(“%d”,&a);
P=4*a;
S=a*a;
printf(“\nPerimetre = %d”, P);
printf(“\nSurface = %d”, S);
return 0 ;
}
Note that there is no branching and no process is repeated again. Each process is contributing
something to the next process.
Selection is a process of deciding which choice is made between two or more alternative
courses of action. Selection logic is depicted as an IF-THEN-ELSE-ENDIF or CASE-
ENDCASE structure. As the name suggests, in case of the IF-THEN-ELSE-ENDIF construct,
if the condition is true, the true alternative actions are performed and if condition is false,
then false alternative actions are performed on.
a) IF-THEN-ELSE-ENDIF construct
Flowchart Pseudocode C
• •
• •
• if(condition)
IF condition THEN {
List of actions List of actions
ELSE }
List of differentElse
actions {
ENDIF List of different
• actions
• }
• •
•
Note that the ELSE keyword and 'Action 2' are optional. In case you do not want to choose
between two alternate courses of actions, then simply use IF-THEN-ENDIF
Flowchart Pseudocode C
• •
ST.LOUIS/2021-2022/HND/L100/ICT/INTRODUCTION_TO_ALGORITHMS/
MODULE_1 7
Module 1 – General Introduction on Algorithms 8
• •
• •
IF condition THEN if(condition)
List of actions {
ENDIF List of actions
• }
• •
• •
•
Hence, if the condition is true, then perform the list of actions listed in the IF-THEN-ENDIF
construct and then move on to the other actions in the process. In case the condition is false,
then move on to the rest of the actions in the process directly. Let us write a pseudocode to
find the largest of three numbers
Flowchart Pseudocode C
START #include<stdio.h>
Input N int main()
If N < 100 Then {
N = N × 100 int N;
Else
N = N - 100
printf("Enter the
Print N number: ");
STOP scanf("%d",&N);
if(N<100)
N=N+100;
Else
N=N-100;
printf("\nnumber is
%d",N);
return 0;
}
b) CASE-ENDCASE construct
If there are a number of conditions to be checked, then using multiple IFs may look very
clumsy. Hence, it is advisable to use the CASE-ENDCASE selection construct for
multipleway selection logic. A CASE construct indicates a multiway branch based on many
conditions. CASE construct uses four keywords, CASE, OF, OTHERS and ENDCASE,
along with conditions that are used to indicate the various alternatives.
Flowchart Pseudocode C
CASE expression OF case (expression)
Condition 1: {
ST.LOUIS/2021-2022/HND/L100/ICT/INTRODUCTION_TO_ALGORITHMS/
MODULE_1 8
Module 1 – General Introduction on Algorithms 9
CASE construct performs the same process as multiple IFs, but it is much easier to read and
write. Conditions are normally numbers or characters indicating the value of 'Expression'
Flowchart Pseudocode C
START #include<stdio.h>
READ code int main()
CASE Grade OF {
A : discount = 0.0 char code ;
B : discount = 0.1 switch ( code )
C : discount = 0.2 {
OTHERS : discount = case 'A': discount = 0.0;
0.3 break;
ENDCASE case 'B': discount = 0.1;
DISPLAY discount break;
STOP case 'C': discount = 0.2;
break;
default: discount = 0.3;
}
Printf( "discount is: %f ",
discount);
}
Looping construct is used when some particular task(s) is to be repeated for a number of
times according to the specified condition. By using looping, the programmer avoids
repeating the same set of instructions. As the selection, the loop is also represented in
flowchart by a diamond. The difference is just at the orientation of the arrows.
ST.LOUIS/2021-2022/HND/L100/ICT/INTRODUCTION_TO_ALGORITHMS/
MODULE_1 9
Module 1 – General Introduction on Algorithms 10
a) WHILE- ENDWHILE
In case of WHILE-ENDWHILE, the loop will continue as long as the condition is true. The
loop is entered only if the condition is true. The 'statement' is performed for each iteration. At
the conclusion of each iteration, the condition is evaluated and the loop continues as long as
the condition is true.
Flowchart Pseudocode C
WHILE condition is while (condition)
True {
statements statements
ENDWHILE }
Flowchart Pseudocode C
INITIALIZE Count to zero #include<stdio.h>
WHILE Count >= 10 main()
ADD 1 to Count {
PRINT Count int i=0;
ENDWHILE while(i<10)
STOP {
printf("%d ",i);
i++;
}
return 0;
}
b) REPEAT-UNTIL
The REPEAT-UNTIL loop is similar to the WHILE-ENDWHILE, except that the test is
performed at the bottom of the loop instead of at the top
Flowchart Pseudocode C
ST.LOUIS/2021-2022/HND/L100/ICT/INTRODUCTION_TO_ALGORITHMS/
MODULE_1 10
Module 1 – General Introduction on Algorithms 11
Repeat Do
Statements {
Until condition is false
}
While (condition)
The 'statement' in this type of loop is always performed at least once, because the test is
performed after the statement is executed. At the end of each iteration, the condition is
evaluated, and the loop repeats until the condition gets true. The loop terminates when the
condition becomes true.
Flowchart Pseudocode C
INITIALIZE Count to zero #include<stdio.h>
REPEAT main()
ADD 1 to Count {
PRINT Count int i=0;
UNTIL Count is less than do
10 {
STOP printf("%d ",i);
i++;
}
while(i<10);
return 0;
}
ST.LOUIS/2021-2022/HND/L100/ICT/INTRODUCTION_TO_ALGORITHMS/
MODULE_1 11
Module 1 – General Introduction on Algorithms 12
APPLICATION EXERCISES
Exercise 3: Make the flowchart, and write the pseudocode for the following problem:
Exercise 4:
On a separate sheet of paper, make a flowchart organizing the “flow” of getting ready to go to
school in the morning. Be sure to include the following steps in your chart, but don’t be
afraid to add other things if you need them!
ST.LOUIS/2021-2022/HND/L100/ICT/INTRODUCTION_TO_ALGORITHMS/
MODULE_1 12
Module 1 – General Introduction on Algorithms 13
ST.LOUIS/2021-2022/HND/L100/ICT/INTRODUCTION_TO_ALGORITHMS/
MODULE_1 13