You are on page 1of 19

DIT 0202: INTRODUCTION TO

PROGRAMMING AND
ALGORITHMS
WEEK 5: INTRODUCTION TO ALGORITHMIC PROBLEM SOLVING
Diploma in Information Technology
Stage II
Lecturer: Mr. Alvin Mdachi
By the of this lecture the student should be
able to:
• Define an algorithm
• Know the characteristics of an algorithm
• Draw flowcharts
• Understand what a pseudocode is and be able to create one given a
problem statement
Problem Solving
• A problem is a set of questions concerning some entities representing
the universe of the problem.
• A problem statement is the description of the properties of the entities
and of the relation between input data and problem solutions
• Solving method is the procedure to construct the solution starting from
the input data.

Input data Solving method Results


Problem Solving Example:
• Let a and b be to two non zero natural numbers. Find the natural
number c having the following properties.
i. C is a common divisor of a and b
ii. C is greater than any other common divisor of a and b

• Problems universe natural numbers (a and b represent the input data, c


represents the result)
• Problems statement (relations between the input data and the result) c
is the greatest common divisor of a and b
Problem solving Remark

• This is a problem which asks for computing a function (that which


associates to a pair of natural numbers the greatest common divisor)
• Another kind of problems are those which ask to decide if the input
data satisfy or not some properties.
• Example verify if a natural number is a prime number or not
• In both cases the solution can be obtained by using a computer only if
it exists a method which after a finite number of operations produces
the solution such a method is an algorithm
What is an Algorithm?
• Different definitions
• Algorithm is something like a cooking recipe used to solve problems
• Algorithm is step by step problem solving method
• Algorithm is finite sequence of operations applied to some input data
in order to obtain the problem solution
• Algorithm is a finite sequence of instructions, each of which has a
clear meaning and can be performed with a finite amount of effort in a
finite length of time
What is the origin of the word ?
• al-Khowarizmi - Persian mathematician (9th
century)
• algorism algorithm
• More on etymology
• algor coolness (in Latin)
• algos pain (in Greek)
Examples of Algorithms in day by day life
• Using a Phone:
➢pick up the phone
➢dial the number
➢talk .
• Algorithms in mathematics
➢Euclids algorithm (it is considered to be the
first algorithm): find the greatest common divisor of two
numbers
➢Eratostenes algorithm: generate prime numbers in a range
➢Horners algorithm: compute the value of a polynomial
The characteristics of a good algorithm are:

• Precision – the steps are precisely stated (defined).


• Uniqueness – results of each step are uniquely defined and only
depend on the input and the result of the preceding steps.
• Finiteness – the algorithm stops after a finite number of instructions
are executed.
• Input – the algorithm receives input.
• Output – the algorithm produces output.
• Generality – the algorithm applies to a set of inputs.
How can algorithms be described?
• The methods for solving problems are usually described in a
mathematical language
• The mathematical language is not always adequate to describe
algorithms because
• Operations which seems to be elementary when are described in a
mathematical language are not elementary when they have to be coded
in a programming language
• Example compute the value of a polynomial
There are two basic instruments to describe
algorithms:
1. Flowcharts
• graphical description of the flow of processing
steps
• they are not very often used
• however, sometimes are used to describe the overall structure of
an application
2. Pseudocode
• artificial language based on
• vocabulary (set of keywords)
• syntax (set of rules used to construct the languages phrases)
• not so restrictive as a programming language
Flowcharts
• A flowchart is a diagrammatic representation of an algorithm.
• Different symbols are used for different states in flowchart, For
example: Input/Output and decision making has different symbols.
Flowchart symbols cont’d
Example of a Flowchart
• Draw a flowchart to add two numbers entered by a user:
Draw flowchart to find the largest among three
different numbers entered by user.
What is a Pseudocode?
• A pseudocode is a detailed description of what the computer program
must do, expressed in an English like language rather than in a
programming language.
• Why do we call it pseudocode ?
✓It is similar to a programming language (code)
✓It is not so rigorous as a programming language
(pseudo)
✓In pseudocode the phrases are
✓Statements (used to describe processing steps)
✓Declarations (used to specify the data)
Pseudocode Example:
• Write a program to print the sum of two integer numbers:
1. Start the program
2. Read the first number and save in the variable (N1)
3. Read the second number and save in the variable (N1)
4. Sum both numbers and save in the variable (sum)
sum = N1+N2
5. Print the variable (sum)
6. End the program
Assignment 2 Due on WEDNESDAY
02/02/2022
1. Write a pseudocode for a program that calculates and prints the sum
of the even integers from 2 to 30
2. Draw a flowchart for a program that determines if the temperature
degree is above or below freezing
3. Draw a flowchart for program that accepts a person’s initial bank
balance followed by a sequence of numbers representing
transactions. A positive number represents a credit entry in the
account and a negative number represents a debit entry. The input is
terminated by a zero entry. The program should print the new
balance.

You might also like