Download as pdf or txt
Download as pdf or txt
You are on page 1of 15

INTRODUCTION TO ALGORITHM

(SWE116)

First Semester 2018/2019 Academic Year


By
Humphrey OJONG
UB (COT) © 2019

4 January 2019 H.M Ojong@EMITINA 1


LESSON I

ALGORITHMS

4 January 2019 H.M Ojong@EMITINA 2


LEARNING OBJECTIVES:
At the end of this chapter, the student will be able to:
̶ explain algorithms, and the key features of an algorithm
̶ learn the different ways of stating algorithms
̶ decide a strategy for designing algorithms.

4 January 2019 H.M Ojong@EMITINA 3


3.1 INTRODUCTION
 Writing a program to solve a problem requires a thorough
understanding of the problem, and a carefully planned
approach to the solution.
 Definition:
An algorithm is a procedure for solving a problem in
terms of
i) the actions to be executed, and
ii) the order in which these actions are to be executed.
 Exercise:
1) Outline the actions to be taken to prepare tea, and the order in
which these actions have to proceed.
2) Explain the activities you carried out in the morning when you
getup from bed until you come to school.

4 January 2019 H.M Ojong@EMITINA 4


 Properties of algorithms:
• provide a solution in a finite number of steps.
• always provide an answer (may not be the desired answer, or may be
that there is no answer)
• is guaranteed to terminate.

 Specifying the order of program execution is known as


program control.

4 January 2019 H.M Ojong@EMITINA 5


3.2 Ways Of Stating Algorithms
 Algorithms can be stated in the following ways:
a) Step-form:
̵ Involves written statements; with each statement solving a part of the problem.
̵ Every statement is logically related to the preceding statement
b) Pseudocode:
̵ Uses a restricted English-like vocabulary to define the actions involved in
solving the problem.
̵ Consist of action statements; that is, executable statements in a C program.
c) Flowchart:
̵ Graphical representation of an algorithm.
̵ Facilitates communication between programmers and business persons.
̵ Table 1 shows the different symbols used in flowcharts.

4 January 2019 H.M Ojong@EMITINA 6


Table 1: Flowchart symbols

4 January 2019 H.M Ojong@EMITINA 7


3.2 Examples of Developing Algorithms
A. Step-form:
̵ The following rules are implemented:
1) Enclose algorithm with START and CLOSE statements.
2) Use INPUT or READ statement to accept data from user.
3) Use PRINT statement to display user message or content in a variable.
Example 1:
Write an algorithm for finding the sum of two numbers.
Solution:
1. START
2. PRINT “Enter two numbers to add”
3. INPUT A, B
4. C← A + B
5. PRINT C
6. STOP

8
4 January 2019 H.M Ojong@EMITINA
Example 2:
Write an algorithm for interchanging the numeric values of two
variables.
Solution:
1. START
2. PRINT “Enter the value of A and B”
3. INPUT A, B
4. C ← A
5. A ← B
6. B ← C
7. PRINT A, B
8. STOP

4 January 2019 H.M Ojong@EMITINA 9


Example 3:
Writes an algorithm that compares two numbers and prints either
the message identifying the greater number or the message saying
that both numbers are equal.
Solution:
1. START
2. PRINT “Enter two numbers”
3. INPUT A, B
4. IF A > B THEN
PRINT “A is greater than B”
5. IF B > A THEN
PRINT “B is greater than A”
6. IF A = B THEN
PRINT “A is equal to B”
7. STOP

H.M Ojong@EMITINA 10
4 January 2019
B. Pseudocode:
̵ Has no standard or rule, and easy to write and use.
C. Flowcharts:
̵ Has standard shapes interconnected by flow lines.
̵ Activities to be performed are written within the shapes in
English.
Example 4:
Draw a flowchart to find the sum
of the first 100 natural numbers.

4 January 2019 H.M Ojong@EMITINA 11


Example 5:
Draw a flowchart to find the largest of three numbers A, B, and C.
Solution:

4 January 2019 H.M Ojong@EMITINA 12


Example 6:
Draw a flowchart to read the marks of a student and classify
them into different grades as follows:
̵ Grade A, if student’s mark is greater than or equal to 90
̵ Grade B, if marks is greater than or equal to 80 but less
than 90,
̵ Grade C, if marks is greater than or equal to 65 but less
than 80, and
̵ Grade D, otherwise.

4 January 2019 H.M Ojong@EMITINA 13


Solution:

4 January 2019 H.M Ojong@EMITINA 14


QUESTIONS??

4 January 2019 H.M Ojong@EMITINA 15

You might also like