0% found this document useful (0 votes)
15 views

CHAP 1- Basic concepts in algorithm

Uploaded by

eschosysbifmet
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views

CHAP 1- Basic concepts in algorithm

Uploaded by

eschosysbifmet
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

THE UNIVERSITY OF BAMENDA UNIVERSITE DE BAMENDA

…………………………. ………………………….
HIGHER TECHNICAL TEACHERS ECOLE NORMALE SUPERIEURE
TRAINING COLLEGE BAMENDA D’ENSEIGNEMENT TECHNIQUE
…………………………. ………………………….
COMPUTER SCIENCE DEPARTMENT DEPARTEMENT DE GENIE
…………………………. INFORMATIQUE
………………………….

Course Title: Algorithms and data structure Code: ICT 417


Option: Information and Communication Technologies Level: 400
Instructor: Mr. DZEUGANG Placide

CHAP I : INTRODUCTION TO ALGORITHM

Although computers appear to be amazingly intelligent machines, they cannot think on their own. They
still rely on human being to give them directives. The computers work on a set of instructions called
computer program, which clearly specifies the way to carry out a task. An analogy of this may be
thought of as the instructions given by the manager or team leader to its team. The team members
follow those instructions and accordingly perform their duties. Similarly, a computer also takes
instructions in the form of computer programs to carry out the requested task. This chapter explain the
basic concepts on programming Discuss what an algorithm is and how to use it to represent the solution
of a problem.
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 to represent algorithms

Contents

I. WHAT IS A COMPUTER PROGRAM?.......................................................................................... 2


II. WHAT IS AN ALGORITHM?...................................................................................................... 3
III. REPRESENTATION OF AN ALGORITHM ............................................................................... 4
IV. ALGORITHM STRUCTURES ..................................................................................................... 7

Topic 1: Introduction to algorithm. ICT 400 By DZEUGANG Placide


I. WHAT IS A COMPUTER PROGRAM?

I.1 Definition
A Computer program (also called software or just a program) is a sequence of instructions written to
perform a specified task with a computer. The act of writing computer program is referred to as
programming. The statements that make up a computer program are collectively referred to as
program code.
A program consists of a series of instructions that a computer processes to perform the required
operation. In addition, it also includes some fixed data, required to perform the instructions, and the
process of defining those instructions and data. Thus, in order to design a program, a programmer must
determine three basic rudiments:
• The instructions to be performed.
• The order in which those instructions are to be performed

The data required to perform those instructions.

I.2 Characteristic of a good computer program


Some of the most essential attributes of good computer program include
(a) Efficiency: This is the degree with which the program fulfils its purposes without waste of
resources
(b) Reusability: This is the ease with which a program or a module of a program can be reused to
design other program.
(c) Maintainability: this is the ease with which modifications can be made to satisfy new
requirements or to correct deficiencies. Well-designed program should be flexible enough to
accommodate future changes that will be needed as new requirements come to light.
(d) Reliability: This is the frequency and extends to which a program fails to perform its functions
under normal operating circumstances.
(e) Correctness: This is the degree with which a program meets its specified requirements.
(f) Robustness: This is the degree with which software resist to users manipulations errors
(g) Documentation: enough comment should be inserted in a program to facilitate its
understanding by any computer user
(h) Portability: Portability refers to the ability of an application to run on different platforms
(operating systems) with or without minimal changes.
(i) Flexibility: A program should be flexible enough to handle most of the changes without having
to rewrite the entire program.
(j) Readability: The program should be written in such a way that it makes other programmers or
users to follow the logic of the program without much effort.

Topic 1: Introduction to algorithm. ICT 400 By DZEUGANG Placide


I.1. Program Development Cycle
There are different approaches to problem solving. One common approach is to use the program
development cycle, with the number of steps that may vary according to the person who has formalized
the development. The development cycle of a program includes the following phases

Problem
analysis

Task analysis
Documentation

Algorithm
Testing and development
debugging
Coding

Figure 1. Program Development Cycle


 Problem Analysis: In this phase, the problem is analysed precisely and completely. Based
on one's understanding, the developer knows about the scope within which the problem
needs to be developed.
 Task Analysis: After analysing the problem, the developer needs to develop various
solutions to solve the given problem. From these solutions, the optimum solution is chosen,
which can solve the problem comfortably and economically.
 Algorithm Development: An algorithm depicts the solution in logical steps (sequence of
instructions).
 Coding: After meeting all the design considerations, the actual coding of the program takes
place in the chosen programming language. languages.
 Testing and Debugging: A program compiler and programmer-designed test data machine
test the code for syntax errors. The results obtained are compared with results calculated
manually from these test data.
 Documentation: Documenting a program enables the user to operate the program correctly.
It also enables other persons to understand the program clearly so that it may, if necessary,
be modified, or corrected by someone other than the original programmer.

II. WHAT IS AN ALGORITHM?

II.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 step by step, discrete, unambiguous instructions for solving

Topic 1: Introduction to algorithm. ICT 400 By DZEUGANG Placide


a particular problem. In simple terms, an algorithm is a series of
instructions to solve a problem (complete a task). Algorithms resemble
recipes. Recipes tell you how to accomplish a task by performing a
number of steps. For example, to bake a cake the steps are: preheat the
oven; mix flour, sugar, and eggs throughly; pour into a baking pan; and
so forth.

An algorithm should be converted into a program before being


introduced into the computer for the execution. This process is called
programming. The algorithmic allows then to pass from a problem to
be solved to an algorithm that describes the process of solving the
problem. Programming role then to translate this algorithm to a
language "understandable" by the computer so that it can be executed
automatically.
Algorithm is not programming language specific, i.e. an algorithm can be expressed as a program in
many different programming languages. There may be more than one algorithm to solve a single
problem, differing in their simplicity, efficiency, or generality.

II.2. Properties of an algorithm


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.

III. REPRESENTATION OF AN ALGORITHM

Problem solving methods are usually described in a mathematical language. Even if it is not a rigorous
language it is not always adequate for algorithms description since it does not allow specifying some
aspects which are important in an algorithm. The algorithm can be described through:
- Flowcharts
- Pseudocodes

III.1. Flowchart
Flowchart is the diagrammatic representation of an algorithm with the help of symbols carrying certain
meaning. Each basic instruction has a corresponding graphical shape and the compound statements are

Topic 1: Introduction to algorithm. ICT 400 By DZEUGANG Placide


described through corrected signs. Some standard symbols that are frequently required for flowcharts
are shown below:
Symbol Symbol name Description
Flow lines are used to connect symbols. These lines indicate
Flow Lines
the sequence of steps and the direction of flow.
The terminator symbol represents the start points, end points,
Terminal
and potential outcomes of a path.
used to illustrate a process, action or an operation. Examples:
Processing
"Add 1 to X"; "save changes" or similar.

Decision These typically contain a Yes/No question or True/False test.

It represents data that is available for input or output. It may


Input/output also represent resources used or generated. Ex: Get X from
the user; display X.
Connector Connector symbol is used to join different flow lines.

Off- page This symbol is used to indicate the flowchart continues on the
Connecter next page

Document is used to represent a paper document produced


Document
during the flowchart process.

Manual input symbol represents input to be given by a


Manual Input
developer / programmer.

This is used to represent data input or output from and to a


Magnetic Disk
magnetic disk.

III.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 is a language based on a
small vocabulary which contains keywords corresponding to statements and identifiers used to specify
the data. As all languages it is based on some rules, but they are less restrictive than those used in
programming languages;
a) Representation and Structure of an algorithm
Algorithms are written to be translated to programs that can be run on a computer. Since we can only
input, process, output and store data on a computer, the instructions in our algorithms will be limited
to these functions
 Input: Get information
 Process: Apply the different operations to obtain the result (assignment, selection, repetition)

Topic 1: Introduction to algorithm. ICT 400 By DZEUGANG Placide


 Output: Give information or write back information
 Storage: Store or display the information
Unlike a program, there is not a standard way to write an algorithm. The structure and the keywords
used can vary from one person to another.
Algorithm algo_name
{ variable and constants declaration}
Begin
Instruction 1
Instruction 2
.
.
.
Instruction n
end

b) What is an identifier,

An identifier can be a variable or a constant.


 Variables are memory location in computer's memory to store data. To indicate the memory
location, each variable should be given a unique name . Variable names are just the symbolic
representation of a memory location. Examples of variable name: sum, car_no, count etc.
 Constants are the terms that can't be changed during the execution of a program. For example:
1, 2.5, "Programming is easy."

c) Operators

The most used operators are:


 Assignment operator: ← or = ( c ← a + b means the value of a and b should be added and
assigned to c)
 Arithmetic operators: + (addition), - (subtraction), * (multiplication), / (division), ^
(power), DIV (integer quotient), % or MOD (remainder)
 Relational operators: = (equality), < (strictly less than), <= or ≤ (less than or equal to), >
(strictly greater than), >= or ≥ (greater than or equal to), <> or ≠ or != (different
 Logical operators: OR (disjunction), AND (conjunction ), NOT (negation)

d) Input/output instructions

 Input: To transfer and input data to a variable. We use keywords as READ, GET, …
 Output: To transfer the value of variable to the output device. We use keywords as: WRITE,
PRINT, DISPLAY, …

e) Example of an algorithm using a pseudocode

Topic 1: Introduction to algorithm. ICT 400 By DZEUGANG Placide


In pseudocode, the algorithm can be written using less or more specific instructions. The two
algorithms below are adding two numbers entered by the user and display the result

Algorithm to add two numbers entered by the user


Step 1: Start Algorithm sum_two_nber
Step 2: Declare variables num1, num2 and sum. Variables: num1, num2, sum
Step 3: Read values num1 and num2. Begin
Step 4: Add num1 and num2 and assign the result to sum. Read num1, num2
Step 5: Display sum sum←num1+num2
Step 6: Stop display sum
end

f) Good programming practice


Even though there is no standard way to write an algorithm, there are few rules to follow to make the
algorithm more understandable. Some are presented below:
 Write only one statement per line: Each stmt in your pseudocode should express just one
action for the computer.
 Capitalize initial keyword: examples: READ, WRITE, …
 Indent to show hierarchy: We will use a particular indentation pattern in each of the design
structures:
 End multiline structures: The ENDIF (or END whatever) always is in line with the IF (or
whatever starts the structure).
 Use meaningful variable: use a variable like sum while calculating the sum instead of a
variable such as rt2fg.

IV. ALGORITHM STRUCTURES

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.
• Sequence, where information can flow in a straight line.
• Selection (branched), where the decisions are made according to some predefined condition.
• Repetition, where the logic can repeat in a loop, that is, where a sequence of steps is repeated
until the desired output is obtained.

IV.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

Topic 1: Introduction to algorithm. ICT 400 By DZEUGANG Placide


Fig. Flowchart and Pseudocode for Sequence Construct

Example
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
Algorithm perim_square
Variables: a, P, S
Begin
Read a
P←4×a
S←a×a
Print P, S
End

Note that there is no branching and no process is repeated again. Each process is contributing
something to the next process.

IV.2 Selection (Decision)


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

Topic 1: Introduction to algorithm. ICT 400 By DZEUGANG Placide


Flowchart Pseudocode



IF condition THEN
List of actions
ELSE
List of different actions
ENDIF


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



IF condition THEN
List of actions
ENDIF


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
Example: Write an algorithm and flowchart which a given number N increased by 100 if N is less than
100, otherwise N is decreased by the 100. Print this result.
Flowchart Pseudocode

Topic 1: Introduction to algorithm. ICT 400 By DZEUGANG Placide


Algorithm increase
Variable: N
START
Input N
If N < 100 Then
N = N × 100
Else
N = N - 100
Print N
STOP

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
CASE expression OF
Condition 1: Sequence 1
Condition 2: Sequence 2


Condition n: Sequence n
OTHERS : default sequence
ENCASE

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'
Example: To assign discount according to the code

Topic 1: Introduction to algorithm. ICT 400 By DZEUGANG Placide


Flowchart Pseudocode
START
READ code
CASE Grade OF
A : discount = 0.0
B : discount = 0.1
C : discount = 0.2
OTHERS : discount = 0.3
ENDCASE
DISPLAY discount
STOP

IV.3 Repetition (Looping)


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.
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
WHILE condition is True
statements
ENDWHILE

Example: To display the first ten natural numbers using DO WHILE-ENDDO

Topic 1: Introduction to algorithm. ICT 400 By DZEUGANG Placide


Flowchart Pseudocode
INITIALIZE Count to zero
WHILE Count >= 10
ADD 1 to Count
PRINT Count
ENDWHILE
STOP

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
Repeat
Statements
Until condition is false

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.
Example To display the first ten natural numbers using REPEAT-UNTIL
Flowchart Pseudocode

Topic 1: Introduction to algorithm. ICT 400 By DZEUGANG Placide


INITIALIZE Count to zero
REPEAT
ADD 1 to Count
PRINT Count
UNTIL Count is less than 10
STOP

c) For … do loop.

The statements in the for loop repeat continuously for aspecific number of times. The while and do-
whileloops repeat until a certain condition is met. The for loop repeats until a specific count is met.
Use a for loop when the number of repetition is know, or can be supplied by the user. The coding
format is:

For x from start to stop do


block of code;
endfor

Example:

for i = 1 to count do is essentially identical to this i = 1


print i version: while i <= count do
endfor print i
i=i+1
endwhile

This for loop means, “Run the indented statements once for each value between 1 and count, and set
the variable i to that value.” The computer automatically increases i by 1 each iteration until it
reaches count. The for loop is generally preferable to while loops when you want to cover a range of
numbers. It’s shorter and easier to read.

Topic 1: Introduction to algorithm. ICT 400 By DZEUGANG Placide

You might also like