Pseudo Code
Pseudo Code
Flowcharts
Understanding the
Programming Process
A programmer's job involves
writing instructions and can be
broken down into six
programming steps :
1. Understand the problem.
2. Plan the logic.
3. Code the program.
4. Translate the program into
machine language.
5. Test the program.
6. Put the program into
Understanding the
Programming Process
1. Understanding the Problem:
a Professional computer
programmers write programs to
satisfy the needs of others.
. Note: Really understanding the
problem may be one of the most
difficult aspects of programming.
2. Code the Program : The heart
of the programming process lies
in planning the program's logic.
Understanding the
Programming Process
During this phase of the
programming process, the
programmer plans the steps to the
program, deciding what steps to
include and how to order them.
The two most common tools are
flowcharts and pseudocode.
Both tools involve writing the steps
of the program in English.
Note: " An algorithm is the
sequence of steps necessary to
Understanding the
Programming Process
3. Code the Program : write the
program in one of more than 400
programming languages.
4. Translate the Program into
Machine Language:
5. Test the Program : Selecting
test data to test program and
ensure that the program is free of
syntax errors and free of logical
errors.
6. Put the Program into
Algorithms
Algorithms
Algorithm is a set of rules applied
to numbers written in decimal
form. The word is derived from the
phonetic pronunciation of the last
name of Abu Ja'far Mohammed ibn
Musa al-Khowarizmi, who was an
Arabic mathematician who
invented a set of rules for
performing the four basic
arithmetic operations (addition,
multiplication , subtraction, and
Algorithms
An algorithm is a set of rules or
instructions for doing a task or
solving a problem.
It is a step-by-step series of
instructions wherein each
successive step is determined by
the outcome of previous steps.
A computer program is simply
an algorithm for a computer that
is written in a programming
language.
Algorithms
Both the pseudocode and
flowchart in algorithm should
always have a Start step at the
beginning and at least one Stop
step at the end.
Flowcharts
Flowcharts
Flowcharts are graphic
representations of algorithms. they
form an intermediate step between
human understanding of the task
to be accomplished and the coded
program that directs a computer to
complete the various steps of the
task.
A flowchart is a diagram made up
of boxes, diamonds and other
shapes, connected by arrows -
Flowcharts
Flowcharting combines symbols
and flowlines , to show figuratively
the operation of an algorithm.
Flowcharts be essential in
developing and understanding the
task.
Standard
Symbols used
in Flowcharts
Comments
The comment or annotation
symbol is used to annotate other
entries.
The dashed line indicates the
position of the comment.
Call
calc_pay
function.
Display results.
END
Rules for
flowcharting
Start.
Read some information.
Make a decision.
On the basis of that decision
, carry out one process
or the
other.
5. Print a result in either case.
6. Stop.
Sequence Structure
A series of actions are performed
in sequence.
32
Decision Structure
One of two possible actions is
taken, depending on a condition.
33
Repetition Structure
A repetition structure represents
part of the program that
repeats. This type of structure is
commonly known as a loop.
34
Repetition Structure
Repetition Structure
35
Repetition Structure
In Pre-Test Repetition
Structure The condition is tested
BEFORE any actions
are
YES
Display x
Add1 to x
performed.x < y?
In Post-Test Repetition
Structure The condition is tested
Display x
AFTER the actions
are performed. Add 1 to x
YES
x < y?
36
Case Structure
One of several possible actions is
taken, depending on the contents
of a variable.
37
Case Structure
Example:
If years employed
If years employed
= 2, bonus is set
= 3, bonus is set
to 200
to 400
If years employed
If years employed
CASE
is any other value,
= 1, bonus is set
years employed
bonus is set to 800
to 100
1
bonus =
100
Othe
r
bonus =
200
bonus =
400
bonus =
800
38
Exercises on
Flowcharts
Exercise _ 1
Design an Flowchart to convert a
temperature in degrees Fahrenheit
to degrees Celsius.
Hint: Celsius = 5/9 * (Fahrenheit
32).
Start
Read Fahrenheit
Output
Celsius
End
Exercise _ 2
Design an Flowchart that accepts
two numbers and computes their
sum, difference, product, and
quotient.
Start
Read Num1
Num1
Read
Read Num2
Exercise _ 3
You work in a store that sells
imported fabric. Most of the fabric
you sell is measured in square
yards, but your customers want to
know the equivalent amount in
square meters. Design an
Flowchart to perform this
conversion.
Hint: 1 square meter
= 1.198 yards.
Start
Output S
END
Exercise _ 4
Design an Flowchart that
computes the employees gross
salary given the hours work and
the hourly rate. Assume that 15%
of the salary is deducted as taxes.
Start
Gross Salary = H * R
Tax = (15/100) * Gross Salary
SalaryPrint Net
END
Exercise _ 5
Design an Flowchart that
computes the distance between
two points (X1, Y1) and (X2,Y2).
Hint: Distance =
Start
Read X1
Read X2
Read Y1
Read Y2
Z1 = (x1 x2)^2
Z2 = (y1 y2)^2
Distance =
Output Distance
END
Exercise _ 6 _Assignment_ 1
Ann likes to jog in the morning. As
she jogs, she counts the number of
strides she makes during the first
and the last minutes of her
jogging. Anne then averages those
two and calls this the number of
strides she makes in a minute.
Design an flowchart that accepts
those averages and the total time
she spent jogging (in hours and
minutes) and then outputs the
distance Ann jogged in miles.
Start
Read Average
Read Hours
Read Minutes
END
Pseudo code
How to write pseudo code
Control
Structures or
Logical
Structures
1. Sequence :
Execution of one step after
another. This is represented as a
sequence of pseudo- code
statements:
Statement 1
Statement 2
Statement 3
Example: Read three
numbers
Add three numbers
Display total of three
2. Selection
. Presentation of a condition and
the choice between two actions,
. the choice depending on whether
the condition is true or false. In
pseudo code, selection is
represented by the keywords IF,
THEN, ELSE and ENDIF
IF condition p is true
THEN
statement(s) in
true case
Example:
IF student is part-time
THEN
Add one to parttime-count
ELSE
Add one to fulltime-count
ENDIF
and
3. Repetition
Presentation of a set of
instructions to be performed
repeatedly, as long as a
condition is true.
WHILE condition p is
true
Statement(s)
to execute
ENDWHILE
Note:
Example:
Write a program that obtains two
integer numbers from the user. It
will print out the sum of those
numbers.
Answer is :
1. Prompt the user to enter the
first integer.
2. Prompt the user to enter a
second integer.
3. Compute the sum of the two
user inputs.
Exercises on
Pseudocode
Exercise _ 1
Design an pseudocode to convert a
temperature in degrees Fahrenheit
to degrees Celsius.
Hint: Celsius = 5/9 * (Fahrenheit
32).
Answer:
1. Start.
2. Get F (Fahrenheits degree) .
3. Celsius = 5/9 * (F 32).
4. Output Celsius .
5. Stop.
Exercise _ 2
Design an pseudocode that
accepts two numbers and
computes their sum, difference,
product, and quotient.
Answer:
1. Start.
2. Sum=0, Difference=0 ,
Product=0 , Quotient = 0 ;
3. Get the First number.
4. Get the Second number.
5. Add First number and
Exercise _ 2
6. Difference=First number Second number .
7. Product=First number *
Second number .
8. Quotient = First number /
Second number .
9. Output (Print or display )
Sum.
10. Output Difference.
11. Output Product.
12. Output Quotient .
Exercise _ 3
You work in a store that sells
imported fabric. Most of the fabric
you sell is measured in square
yards, but your customers want to
know the equivalent amount in
square meters. Design an
pseudocode to perform this
conversion.
Hint: 1 square meter
= 1.198 yards.
Answer:
Exercise _ 3
3. S (square meter value ) = Y /
1.98 .
4. Output S.
5. Stop.
Exercise _ 4
Design an pseudocode that
computes the employees gross
salary given the hours work and
the hourly rate. Assume that 15%
of the salary is deducted as taxes.
Answer:
1. Start.
2. Get H (hours work ).
3. Get R (hourly rate ) .
4. Gross Salary = H * R .
5. Tax = (15/100)* Gross
Exercise _ 4
6. Net Salary = Gross Salary
- Tax .
7. Output Net Salary .
8. Stop.
Exercise _ 5
Design an pseudocode that
computes the distance between
two points (X1, Y1) and (X2,Y2).
Hint: Distance =
Answer:
1. Start.
2. Get X1.
3. Get X2.
4. Get Y1.
5. Get Y2.
Exercise _ 5
6. Z1 = (X1 X2)^2
7. Z2 = (Y1 Y2 )^2
8. Distance =
9. Output Distance.
10.Stop.
Exercise _ 6
Design an pseudocode and the
corresponding flowchart for
adding the test scores as given
below: 26, 49, 98, 87, 62, 75
Answer of pseudocode:
1. Start;
2. Sum = 0 ;
3. Get Test score _1;
4. Add Test score _1 to Sum ;
5. Get Test score _2 ;
6. Add Test score _2 to Sum ;
Exercise _ 6
7. Get Test score _3;
8. Add Test score _3 to Sum ;
9. Get Test score _4 ;
10.Add Test score _4 to Sum ;
11.Get Test score _5 ;
12.Add Test score _5 to Sum ;
13.Get Test score _6 ;
14.Add Test score _6 to Sum ;
15.Output Sum ;
16.Stop ;
Exercise _ 6
Answer of pseudocode:
Exercise _ 7
The problem with Exercise_6 have
some steps appear more than
once, i.e. step 5 get second
number, step 7, get third number,
etc. One could shorten the
algorithm or flowchart as follows:
Answer:
1. Start
2. Sum = 0
3. Get Value
4. Sum = Sum + Value
Exercise _ 7
5. Go to step 3 to get next
Value
6. Output Sum
7. Stop
Answer of
flowchart:
Exercise _ 8
In Exercise_6, step 3 to 5 will be
repeated, where a number is
obtained and added to the sum .
So, we can use repetition structure
to represent the answer by adding
terminal value to end loop .
Answer of pseudocode:
1. Start
2. Sum = 0
3. Get a value
4. If the value is equal to 1,
Exercise _ 8
5. Add to sum ( sum = sum +
value)
6. Go to step 3 to get next
Value
7. Output the sum
8. Stop
Answer of
flowchart:
Exercise _ 9
In Exercise_6, step 3 to 5 will be
repeated, by using for loop .
Answer of pseudocode:
1. Start ;
2. Count = 6 ;
3. Sum = 0 ;
4. For (n = 1, n <= Count, n +
1)
5.
Get number ;
6.
Sum = Sum + number
;
Exercise _ 9
8. Display The sum of the
numbers is : ;
9. Print Sum ;
10.Stop ;
START
Count=6
Sum = 0
n=1
IS n <= Count
Get number
n= n+ 1
END
Exercise _ 10
Design an algorithm that computes
the absolute difference of two
values (X and Y), where the
difference is (X-Y) or (Y-X), which
ever is positive.
Answer of pseudocode:
1. Start ;
2. Get x ;
3. Get Y ;
4. If ( X > Y )
5.
Diff = X Y;
Exercise _ 10
6. Else
7.
Diff = Y - X;
8. EndIf ;
9. Print Diff ;
Answer of
flowchart:
START
Read X, Y
Diff= Y-X
IS
X>Y
Print Diff
END
Diff= X-Y
Exercise _Assignment_2
1. Design an algorithm and the
corresponding flowchart for
finding the sum of the numbers 2,
4, 6, 8, , n
2. Using flowcharts, write an
algorithm to read 100 numbers
and then display the sum.
3. Write an algorithm to read two
numbers then display the largest.
4. Write an algorithm to read two
numbers then display the
Exercise _Assignment_2
5. Write an algorithm to read three
numbers then display the largest.
6. Write an algorithm to read 100
numbers then display the largest.
7. Design an algorithm and the
corresponding flowchart for
finding the sum of n numbers.
Rfrences
1. ALGORITHMS, FLOWCHARTS, DATA
TYPES AND PSEUDOCODE.
2. Introduction to Flowcharting A
Supplement to
Starting Out with C++: From Control
Structures to Objects 5th Edition by Tony
Gaddis .
3. http://www.minich.com/education/wyo/s
tylesheets/pseudocode.htm
4. Introduction to Computers Lab
Handouts .