1
Programming
Lecture 2
PSEUDOCODE & FLOWCHARTS
Dr. Badria Nabil
Program Development Steps
2
Define the problem. →Human thought
Plan the problem solution. →
◼ writing the algorithm [pseudo-natural language (English, Arabic)
or
◼ drawing the flowchart diagram).
Code the program. → High Level Programming
Language (C, C++, Java, …)
Compile the program. → Machine Code
Run the program.
Test and debug the program.
WHAT IS AN ALGORITHM?
3
An algorithm is a set of ordered steps for solving a
problem.
Examples:
◼ An algorithm for preparing breakfast.
◼ An algorithm for converting Gregorian dates to Islamic
dates.
◼ An algorithm for drawing a curve.
Algorithm in Real Life
4
Consider the following …
Problem: Baking a Cake
How to solve:
◼ Start
◼ Preheat the oven at 180oC
◼ Prepare a baking pan
◼ Beat butter with sugar
◼ Mix them with flour, eggs and essence vanilla
◼ Pour the dough into the baking pan
◼ Put the pan into the oven
◼ End
CLASS ACTIVITY
5
Write a simple algorithm for withdrawing
a sum of money at an ATM.
HOW TO SPECIFY AN
6
ALGORITHM?
An algorithm must be specific enough so that it can
be conveniently translated into a computer program
(using C++, for example).
An algorithm can be specified:
Textually
For example, using pseudo code (see later)
Graphically
For example, using flowcharts or UML activity charts
Pseudocode
7
Pseudocode is a detailed description of what a
computer program must do, expressed in an English
like language rather than in a programming
language.
An outline of a program, written in a form that can
easily be converted into real programming
statements. It resembles the actual program that will
be implemented later. However, it cannot be
compiled nor executed.
Pseudocode Example
8
Write a Program to Print the Sum of two integer
Numbers
1. Start the program
2. Read the first number ( N1 )
3. Read the second number ( N2 )
4. Sum the both numbers Sum ➔ Sum = N1 + N2
5. Print the Sum
6. End the program
Flowchart
9
A flowchart is a type of diagram that graphically
depict the logical steps to carry out a task and show
how the steps relate to each other.
A flowchart is a graphical representation of the
sequence of operations in a program.
An algorithm can be represented graphically using
a flowchart.
Flowcharts Symbols
10
End Start Start/End
Print n1 Read n1 Input/Output
N2 = n1+3 N2 = 5 Process (arithmetic operations)
n1 > 3 Decision , can be used with loops
Flowline (move from step to
step)
Flowchart
11
Start Algorithm starts here
Input
Gregorian date Input data from user
Convert Gregorian
date to Islamic date Perform the date conversion
Display
Display the result
Islamic date
End Algorithm ends here
Example 1
12
Draw a flowchart to calculate the average of three
numbers.
Solution
13
Input start
Input three numbers N1,N2,N3
Processing Read N1,N2,N3
Average =(N1+N2+N3)/3
Output Average =(N1+N2+N3)/3
Average
Print Average
End
Example 2
14
Draw a flowchart for a program that calculates and
print the area and the perimeter of a rectangle.
Area = length*width
Perimeter = 2*( length + width)
Solution
15
start
Input
Read L, W
Length
width
area = L * W
Processing
Area = length*width perimeter = 2 (L+W)
Perimeter = 2*( length + width)
Output Print area
Area
Print perimeter
Perimeter
End
Example 3
16
Draw the flow chart for a program that calculates
the total salary for an employee using this equation:
Total_Sal = Salary +Overtime
Solution
17
Input start
Salary
Overtime Read Salary
Processing
Total_Sal = Salary +Overtime
Read Overtime
Output
Total_Sal =
Total_Sal
Salary +Overtime
Print Total_Sal
End
Statement structures
•18
Sequence – follow instructions from one line to the
next without skipping over any lines
Decision - if the answer to a question is “Yes” then
one group of instructions is executed. If the answer
is “No,” then another is executed
Looping – a series of instructions are executed over
and over
Decision flow chart
•19
Example 4
20
Draw a flowchart for a program that determine if the
temperature degree is above or below freezing.
Freezing = 32
Input
Temp.
Processing
Check if Temp is below the 32 → below freezing.
Check if Temp is above the 32 → above freezing.
Output
Print “below freezing” or “above freezing”
Solution
21
Example 5
22
Draw a flowchart for a program that identifies the
temperature status based on the following
➢ Temperature>=27 is hot.
➢ 27>Temperature>=20 is good.
➢ Temperature<20 is cold.
23
Assignment 1
24
Draw a flowchart for a program that calculates the
Zakat, where the user enter the amount of money
then the program show the zakat.
Zakat =(2.5/100) * amount.
If the amount is less than 1000, Zakat =0.
Assignment 2
25
Draw a flowchart that selects the largest of
three distinct numbers a ,b,c.