Вы находитесь на странице: 1из 33

Chapter 2

Pseudocode

Outline
By the end of this chapter, you will
be able to:
Define algorithm
Differentiate between pseudocode and
flowchart
Solve a given problem by applying
pseudocode technique
Perform desk-checking

Algorithm
An algorithm is a step-by-step procedure
to solve a given problem procedure
consisting of a finite set of unambiguous
rules (instructions) which specify a finite
sequence of operations that provides the
solution to a problem, or to a specific class
of problems for any allowable set of input
quantities (if there are inputs).
The steps are normally "sequence,
"selection, "iteration" and a case-type
statement.
Uses human native language.
3

Example of algorithm
Instruct someone to add up a list of prices
on a pocket calculator:
Turn on calculator
Clear calculator
Repeat the following instructions
Key in dollar amount
Key in decimal point(.)
Key in cents amount
Press addition(+) key
Until all prices have been entered
Write down total price
Turn off calculator
4

..Algorithm
Can be implemented by using

Pseudocode

OR

Flowchart

Problem Solving
Understand and analyze the problem
3 separate components:
Input: List of things that is required by
the problem
Output: Outcome desired
Process: Actions that need to be done

Pseudocode?
Really structured English that has
been formalized and abbreviated to
look like the high-level computer
languages.
Remember!!!!! Computer needs to
perform the set of instructions you
write. So, meaningful words and
phrases will make it easier to
translate to programming language.
7

Meaningful names
To represent variables in our
pseudocode
number1 vs n1
Underscore (_) is used if there are
more than one word in the variable.
Eg. total_sales
Another option: use capital letters to
separate words. Eg. totalSales
Majority of programming languages
does not allow gap (space) in
8

Example
To add two numbers together
Input

Process

number_
1
number_
2

Read two numbers


Add the numbers
together
Display the total

Outp
ut
total

Pseudocode
After identifying Input, Output and
Process, we can write pseudocode.
Pseudocode: See translation to a
specific programming language as it
includes some basic computer
operations

10

Pseudocode
Pseudocode = Similar to how we
write essays. Beginning, body
content and ending of an essay.
Begin with Start
End with End
Body content are you processes show sequence of task in the
program
Pseudocode corresponds closely with
the input, processes and output
11

Pseudocode
Still remember what is:
INPUT?
A list of source data provided to the problem.
PROCESSES?
A list of actions needed to produce the required
outputs.
OUTPUT?
A list of the outputs required.
12

Pseudocode
A computer can:
receive information
Focus in this
put out information chapter
perform arithmetic
assign a value to a variable or
memory location
compare two variables and select
one of two alternative actions
repeat a group of actions
13

Focus in
next few
chapters

A computer can receive


information

14

Pseudocode
Keywords - input
Prompt and Get: use to receive
input from the keyboard.

Read: use to receive input from a


record on a file.

15

Pseudocode
Keywords - input
The difference between Prompt
and Get:
Usually Prompt before Get.
Prompt causes message to be sent to
the screen, asking user to respond
usually by providing input.
Prompt for student_mark
Get student_mark
16

A computer can put out


information

17

Pseudocode
Keywords - output
Display, Output or Put: used
when we want to output written to
the computer screen.
Print: enables output to be sent to a
printer.
Write: enables output to be written
to a file.
Print Program Completed
Output total_tax

18

A computer can perform


arithmetic

19

Pseudocode
Arithmetic Symbols
Symbols that you can use to write
pseudocodes:
Symbol
+
*
/
()

Purpose
Addition
Subtraction
Multiplication
Division
Parentheses
20

Pseudocode
Arithmetic Examples
divide total_marks by student_count
sales_tax = cost_price * 0.10
compute C = (F - 32) * 5/9

21

Example and Desk checking

22

Pseudocode
Example
Problem: Adding two numbers together
1. Start
2. Declare variables, number_1,
number_2, total
3. Get number_1, number_2
4. total = number_1 + number_2
5. Display total
6. End.
23

Desk checking
Desk checking: process of tracing
through the pseudocode with some
test data
Mimic what the computer would do
based on the pseudocode written
Help eliminate errors but will not be
able to prove that your algorithm is
correct
24

Desk checking
Should have at least 2 sets of input
data for testing.
Write the desired outcome from each
set
First set Second
set
number_1
5
7
number_2
18
33
total
23
40
25

Desk checking
Create a table with the variables that
are involved in the program and pass
each test data set through the
pseudocode.number_1 number_2 total
Statement
number
First pass
1, 2,3
4
5,6
Second pass
1, 2,3
4
5,6

18
23
display

33
26

40
display

Desk checking
Complete the desk check for pseudocode use
to calculate land price:

1.Start
2.Declare variables, width, length, size, price
3.Prompt user for width, length
4.Get width, length
5.size = width * length
6.price = size * 5.40
7.Display price
8.End
27

More Example

28

Add three numbers


A program is required to read three
numbers, add them together and
print their total.
INPUT: ???
PROCESSING: ???
OUTPUT: ?

Add three numbers


INPUT: number1, number2, number3
PROCESSING: read three numbers,
add numbers together, print total
number
OUTPUT: total

Add three numbers


ADD_three_numbers
1
Read number1, number2,
number3
2 total = number1 + number2 +
number3
3 Print total
END

Add three numbers Desk


Checking
Statement
number
First pass
1

number_
1
10

number_ number
2
_3
20

total

30

60

print

Second pass
1

40

41

42

123

print

Summary
Algorithm = a finite step by step of
instructions to solve a problem.
Algorithm can be implemented by
using pseudocode or flowchart.
Desk-checking = a process of tracing
through the algorithm with several
sample data.

33

Вам также может понравиться