Algorithms in Programming
Objectives
Problem Solving in Programming
o Unstructured, Structured, Modular, Top-Down, Bottom-Up, Object-oriented Programming
Defining Algorithms
Pseudocode
Flowchart
IPO Diagram
Problem Solving Tools in Programming
– Solving problems is the core of computer science.
– Programmers must :
1) understand how a human solves a problem,
2) create an algorithm of the solution,
3) understand how to translate this "algorithm" into something a
computer can do,
4) and know how to "write" the specific syntax of a language
Problem Solving Tools in Programming
– To understand problem solving in programming, we should know the
following types of programming methods:
Unstructured
Structured
Modular
Top-Down
Bottom-Up
Object-oriented Programming
Unstructured Programming
– Here, the code is written as a single whole
block.
BEGIN
PRINT “Belgium Campus”
– The whole program is taken as a single unit. PRINT “Belgium Campus”
repetitive
PRINT “Belgium Campus”
PRINT “Belgium Campus”
– It is harder to do changes in the program.
PRINT “Belgium Campus”
PRINT “Belgium Campus”
– This paradigm was used in earlier versions END
of BASIC, COBOL, and FORTRAN
Structured Programming
– Converts large or complex programs into more manageable and small pieces
of code.
– The code is divided into functions or modules.
Structured Programming
– We sub-divide the whole program into small modules so that the program
becomes easy to understand.
– It is split into three versions:
a) Top Down Analysis
– Is a procedural programming style in which design begins by specifying
complex pieces and then dividing them into successively smaller pieces.
b) Modular Programming
– Is a programming style in which
the program is broken into
smaller independent units, called
modules.
– Each module developed is
independent of each other.
Modular Programming
– Modular programming is a solution to
the problem of very large programs that
are difficult to debug and maintain.
– Multiple programmers can code
different modules simultaneously, thus
reducing time to code and sharing work.
– Leads to reusable code that can be used
in other programs too.
c) Structured Code
– Type of programming that involves the
usage of:
o Sequence
o Repetition, and
o selection
Bottom-Up Approach
– Is the opposite of Top-Down Approach.
– Here, individual parts of the system are specified in detail.
– The parts are linked to form larger components, which are in turn linked
until a complete system is formed.
– We use it to solve smaller problems and integrate it as whole and
complete the solution.
Bottom-Up Approach
– the smaller modules are solved first, and then they are integrated to find the
solution of a bigger, top module.
Object Oriented Approach
– A computer programming model that organizes software design around
data, or objects, rather than functions and logic.
– OOP is the most popular programming approach used for software
development and is taught as the standard way to code.
– OOP is based on the concept of objects and classes.
Object Oriented Approach
– Based on classes and Objects
Algorithms
– An algorithm is a step by step process which helps
programmers solve a complicated program.
– It is also said that an algorithm is a set of well-defined
instructions that are written in a proper sequence to achieve
the goal of the program.
Algorithms
– There are different types of algorithms, we shall look at the
three below for this module:
1) Pseudocode
2) Flowcharts
3) Input-Process-Output (IPO) Diagrams
Example Algorithm
– Algorithms can also apply to daily life scenarios.
– Like if somebody wants to place an order for a pizza at a
restaurant along with cold drinks, they have to follow some
steps to get that done, i.e. place an order for pizza first, then
place an order for the cold drinks. If cold drinks are not
available, then have to look for another option, like cold
coffee. If it satisfies all the conditions, then the order can be
placed. Here it can be seen that the whole order is placed
into two small orders to avoid errors.
Example Algorithm
Pseudocode:
BEGIN
Order Pizza
If Cold drinks available, Then
Order Cold drinks
Otherwise
Order Cold coffee
End If
Pay the bill
END
Example Algorithm
Flowchart
Example Algorithm
IPO
Input Processing Output
Order details Add Pizza to Cart Receipt
Check: Cold Drink
available?
YES: Order Cold Drinks
NO: Order Coffee
Pay bill
1) Pseudocode
– Pseudocode is an artificial and informal language that helps programmers
develop algorithms.
– Pseudocode is a "text-based" algorithmic design tool.
– It is a high-level description of the algorithm to be implemented
on the machine end.
– This language can then be converted into a programming
language to have an exact machine code.
Pseudocode
– Keywords used in pseudocode include:
• BEGIN
• END
• IF…ELSE…THEN
• PRINT or DISPLAY
• SET
• WHILE, DO…WHILE, FOR…TO
• INPUT, OUTPUT
Pseudocode
Example 1: Add Two Numbers
Declaring variables
BEGIN Displaying text output
SET num1, num2, sum to user
PRINT "Input first
number:" Getting text input from user
num1 = INPUT and assigning it to variable
PRINT "Input second
number:" Processing a calculation and
num2 = INPUT assigning to sum variable
sum = num1 + num2
Displaying text plus value of
PRINT “The sum is: ” +
variable sum
sum
END
Pseudocode
Steps of pseudocode:
1. The variables “num1”, “num2”, and “sum” are declared.
2. The program outputs the message “Input first number:” and waits for
the user to input a value, which is stored in “num1”.
3. The program outputs the message “Input second number:” and waits
for the user to input a value, which is stored in “num2”.
4. The value of “sum” is calculated as the sum of “num1” and “num2”.
5. The value of “sum” is outputted as the result of the program.
Pseudocode
Example 2: Calculate sales taxes
BEGIN
SET price, tax, taxRate, total
OUTPUT "Enter Product Price"
price = INPUT
OUTPUT "Enter tax rate among 1 and 100"
tax = price * (taxRate/100)
total = price + tax
OUTPUT "Product tax = ” + tax
OUTPUT "Product total price = ” + total
END
Pseudocode
Steps of pseudocode:
1) The variables “price”, “tax”, “taxRate”, and “total” are declared.
2) The program outputs the message “Enter Product Price” and prompts the user to input
the price of the product.
3) The program outputs the message “Enter tax rate among 1 and 100” and prompts the
user to input the tax rate as a percentage.
4) The tax amount is calculated by multiplying the price of the product by the tax rate
divided by 100 and stored in the “tax” variable.
5) The total cost of the product, including tax, is calculated by adding the price of the
product and the tax amount and stored in the “total” variable.
6) The program outputs the message “Product tax=” followed by the value stored in the
“tax” variable.
7) The program outputs the message “Product total price =” followed by the value stored
in the “total” variable.
Benefits of pseudocode
– It can be quickly and easily converted into an actual programming language
as it is similar to a programming language.
– it is fairly easy to understand, even for non-programmers.
– it does not matter if there are errors in the syntax - it is usually still obvious
what is intended.
– changes to the design can be incorporated quite easily
2) Flowcharts
– A flowchart is a graphical diagram that depicts an algorithm.
– It illustrates a step-wise approach to solve a task.
– Different graphical figures or shapes are used to represent each step in the
flowchart.
Flowcharts Symbols
Flowcharts Symbols Examples
Flowcharts
Example: Flowchart for an
algorithm of a program that
prints the numbers 1 to 10:
Benefits of flowchart
• it is easy to see how a program flows.
• flowcharts follow an international standard
• it is easy for any flowchart user to pick up a diagram and
understand it
IPO (Input-Process-Output) Diagram
• Describes a simple way of how your program processes input and turns
it into output.
• Usually, you develop an IPO chart to represent all the input, processes
and outputs of a program, before creating an pseudocode.
• Making an IPO chart helps you understand what you can expect to get
out of the data you gather and process.
IPO
Create an IPO chart of a program that takes 2 numbers from the user,
calculates the sum, average and product, and prints the outputs.
Exercise
Go on Teams
Download Algorithm Exercises.pdf file and attempt the questions
The End