CHAP 1- Basic concepts in algorithm
CHAP 1- Basic concepts in algorithm
…………………………. ………………………….
HIGHER TECHNICAL TEACHERS ECOLE NORMALE SUPERIEURE
TRAINING COLLEGE BAMENDA D’ENSEIGNEMENT TECHNIQUE
…………………………. ………………………….
COMPUTER SCIENCE DEPARTMENT DEPARTEMENT DE GENIE
…………………………. INFORMATIQUE
………………………….
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.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
Problem
analysis
Task analysis
Documentation
Algorithm
Testing and development
debugging
Coding
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
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
Off- page This symbol is used to indicate the flowchart continues on the
Connecter next page
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)
b) What is an identifier,
c) Operators
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, …
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
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.
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
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
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
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
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:
Example:
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.