0% found this document useful (0 votes)
161 views82 pages

Introduction To Pseudocode: Rikip Ginanjar

The document introduces pseudocode by defining it as a way to represent algorithms using simple English statements and keywords to describe program logic and control structures. It explains that pseudocode uses statements written on separate lines with indentation to signify control structures and that meaningful names should be used for variables. The document also presents the six basic computer operations and three basic control structures that form the basis of any program.

Uploaded by

Syhr Alydrs
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
161 views82 pages

Introduction To Pseudocode: Rikip Ginanjar

The document introduces pseudocode by defining it as a way to represent algorithms using simple English statements and keywords to describe program logic and control structures. It explains that pseudocode uses statements written on separate lines with indentation to signify control structures and that meaningful names should be used for variables. The document also presents the six basic computer operations and three basic control structures that form the basis of any program.

Uploaded by

Syhr Alydrs
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 82

Introduction to Pseudocode

Rikip Ginanjar.
2

What is Pseudocode?

• One of the popular representation of Algorithm


• Widely choosen because:
– easy to read and write
– allow the programmer to concentrate on the logic of the
problem
– Structured in English language

RIkip Ginanjar.
3

Pseudocode Convention

• Statement are written in simple English


• Each instruction is written on a separate line
• Keywords and indentation are used to signify particular
control structures.
• Each set of instructions is written from top to bottom, with
only one entry and one exit.
• Groups of statements may be formed into modules, and
that group given a name.

RIkip Ginanjar.
4

Six Basic Computer Operations

1. A computer can receive information

Verb used:
•Read  used when the algorithm is to receive the input from a record on a file
•Get  used when the algorithm is to receive input from the keyboard.

Read student name


Get system date
Read number_1,
number_2
Get tax_code

RIkip Ginanjar.
5

2. A computer can put out information

Verb used:
•Print  used when the output is to be sent to the printer
•Write  used when the output is to be written to a file
•Put, Output, Display  used when the output is to be written to the screen
•Prompt  required before an input instruction Get, causes the message to be
sent to the screen which requires the user responds, usually by providing input.

Print `Program Completed´


Write customer record to master file
Put out name, address and postcode
Output total_tax
Display ´End of data´

Prompt for student_mark


Get student_mark

RIkip Ginanjar.
6

3. A computer can perform arithmetic

Verb used:
•Compute
•Calculate

•Symbols used:
+, -, *, /, ()

Add number to total


Total = total + number

Divide total_marks by student_count


Sales_tax = cost_price * 0.10
Compute C = (F – 32) * 5/9

RIkip Ginanjar.
7

4. A computer can assign a value to a variable or memory location

• Three cases :
1. To give data an initial value in pseudocode, the verbs
Initialise or Set are used
2. To assign a value as a result of some processing, the symbols
´=´or ´´ are written
3. To keep a variable for later use, the verbs Save or Store are
used.

Initialize total_price to zero


Set student_count to 0
Total_price = cost_price + sales_tax
Total_price  cost_price + sales_tax
Store customer_num in last_customer_num

RIkip Ginanjar.
8
5. A computer can compare two variables and select one of two
alternate actions

Keyword used:
IF, THEN, ELSE

IF student_attendance_status is part_time
THEN
add 1 to part_time_count
ELSE
Add 1 to full_time_count
ENDIF

RIkip Ginanjar.
9

6. A computer can repeat a group of actions

Keyword used:
DOWHILE, ENDDO

DOWHILE student_total < 50


Read student record
Print student name, address to report
Add 1 to student_total
ENDDO

RIkip Ginanjar.
10

Meaningful names

• When designing a solution algorithm, a programmer


should introduce unique names, which are:
– Represent the variables or objects in the problem
– Meaningful
example: number1, number2, number3  more meaningful than
A, B, C
- Used word separator if more than one word
example: sales_tax, word_count
- Or Capital letter as separator
example: salesTax, wordCount

RIkip Ginanjar.
11

The Structure Theorem

• It is possible to write any computer program by


using only three basic control structures that are
easily represented in pseudocode:
» Sequence
» Selection
» Repetition

RIkip Ginanjar.
12

Sequence

Is the straightforward execution of one processing step after another.

Statement a
Statement b
Statement c

• Add 1 to pageCount
• Print heading line 1
• Print heading line 2
• Set lineCount to zero
• Read customer record

RIkip Ginanjar.
13

Selection

Presentation of condition and the choice between two actions

IF condition p is true THEN


statement(s) in true case
ELSE
statement(s) in false case
ENDIF

Example:
IF student_attendance_status is part_time THEN
add 1 to part_time_count
ELSE
add 1 to full_time_count
ENDI>f
RIkip Ginanjar.
14

Repetition

The presentation of a set of instructions to be performed repeatedly,


as long as a condition is true

DOWHILE condition p is true


statement block
ENDDO

Example:
Set student_total to zero
DOWHILE student_total < 50
Read student record
Print student name, address to report
Add 1 to student_total
ENDDO

RIkip Ginanjar.
15

DESIGNING A SOLUTION ALGORITHM

• The most challengin task in the life cycle of a


program
• First attempt usually doesnt result in a finished
product
• Keep altering the step and algorithms till satesfied
result achieved.

RIkip Ginanjar.
16

Point to be considered in Solution Algorithm

1. A name should be given to the algorithm, which is


describe the function of algorithm
2. An END statement used to indicate the algorithm is
complete
3. All processing steps between the algorithm name and
END statement should be indented for readability.
4. Each processing step in the defining diagram relates
directly to one or more statements in the algorithm.

RIkip Ginanjar.
17

Example 3.1. Solution Algorithm for example 2.1

 A program is required to read three


numbers, add them together and print
their total.

RIkip Ginanjar.
18

• Defining diagram

Input Processing Output

Number1 total
Number2
Number3

RIkip Ginanjar.
19

Solution Algorithm
• Add_three_numbers
Read number1, number2, number3
Total = number1 + number2 + number3
Print total
END

RIkip Ginanjar.
20

Example 3. 2. Find average temperature

• A program is required to prompt the


terminal operator for the maximum and
minimum temperature readings on a
particular day, accept those readings
as integers, and calculate and display
to the screen the average temperature,
calculated by (maximum temperature +
minimum temperature)/2.

RIkip Ginanjar.
21

• Defining diagram

Input Processing Output


Max_temp Prompt for temperatures Avg_temp
Min_temp Get temperatures
Calculate average temperature
Display average temperature

RIkip Ginanjar.
22

Solution Algorithm
• Find average_temperature
Prompt operator for max_temp, min_temp
Get max_temp, min_temp
Avg_temp= (max_Temp + min_temp)/2
Output avg_temp to the screen
END

RIkip Ginanjar.
23

What about this ? -


Compute mowing time

• A program is required to read from the


screen the lenght and widht of a
rectangular house block, and the lenght
and width of the rectangular house that
has been built on the block. The
algorithm should then compute and
display the mowing time required to cut
the grass around the house, at the rate
of two square metres per minute.

RIkip Ginanjar.
24

Solution Algorithm
Calculate_mowing_time
Prompt operator for block_lenght, block_width
Get block_length, block_width
block_area = block_lenght*block_width
Prompt operator for house_lenght, house_width
Get house_lenght, house_width
house_area=house_lenght*house_width
Mowing_area=block_area-house_area
Mowing_time=mowing_area/2
Output mowing_time to screen
END

RIkip Ginanjar.
25

Checking the solution algorithm


(Desk Checking)

• Tracing through the logic of the algorithm with


some chosen data..

RIkip Ginanjar.
26

Step in desk Checking an algorithm

1. Choose valid simple input test case (2-3 enough)


2. Establish what the expected result should be.
3. Make a table of relevant variable names
4. Checking the test case line by line, step by step
5. Repeat process 4 for other test case
6. Check if expected result 2 matches with actual result 5

RIkip Ginanjar.
27

Example 3.4. Desk Chek for example 2.1

 A program is required to read three


numbers, add them together and print
their total.

RIkip Ginanjar.
28

Solution Algorithm
• Add_three_numbers
Read number1, number2, number3
Total = number1 + number2 + number3
Print total
END

RIkip Ginanjar.
29

Desk Checking
1. Choose two sets input test data.
Set 1: 10,20, 30 and Set 2: 40, 41, 42

Data Set 1 Data Set 2

Number 1 10 40

Number 2 20 41

Number 3 30 42

RIkip Ginanjar.
30

2. Establish the expected result for each test case

Data Set 1 Data Set 2

Total 60 123

RIkip Ginanjar.
31

3. Set up a table of relevant variable names, and pass each test data set
statement by statement.

Statement number1 number2 number3 total


number
First Pass
1 10 20 30
2 60
3 Print
Second Pass
1 40 41 42
2 123
3 Print

RIkip Ginanjar.
32

4. Check the expected results (60 and 123) match the actual results.

RIkip Ginanjar.
33

Desk Check of Example 3. 2.

• A program is required to prompt the


terminal operator for the maximum and
minimum temperature readings on a
particular day, accept those readings
as integers, and calculate and display
to the screen the average temperature,
calculated by (maximum temperature +
minimum temperature)/2.

RIkip Ginanjar.
34

Solution Algorithm
• Find average_temperature
Prompt operator for max_temp, min_temp
Get max_temp, min_temp
Avg_temp= (max_Temp + min_temp)/2
Output avg_temp to the screen
END

RIkip Ginanjar.
35

Desk Checking
1. Choose two sets input test data.
Set 1: 30, 10 and Set 2: 40, 20

Data Set 1 Data Set 2

Max_temp 30 40

Min_temp 10 20

RIkip Ginanjar.
36

2. Establish the expected result for each test case

Data Set 1 Data Set 2

Avg_temp 20 30

RIkip Ginanjar.
37

3. Set up a table of relevant variable names, and pass each test data set
statement by statement.

Statement number Max_temp Min_temp Avg_temp


First Pass
1,2 30 10
3 20
4 0utput
Second Pass
1,2 40 20
3 30
4 output

RIkip Ginanjar.
38

4. Check the expected results match the actual results.

RIkip Ginanjar.
39

Assignment 2:
Desk Checking for
Compute mowing time

• A program is required to read from


the screen the lenght and widht of a
rectangular house block, and the
lenght and width of the rectangular
house that has been built on the
block. The algorithm should then
compute and display the mowing time
required to cut the grass around the
house, at the rate of two square
metres per minute.

RIkip Ginanjar.
40

Solution Algorithm
Calculate_mowing_time
Prompt operator for block_lenght, block_width
Get block_length, block_width
block_area = block_lenght*block_width
Prompt operator for house_lenght, house_width
Get house_lenght, house_width
house_area=house_lenght*house_width
Mowing_area=block_area-house_area
Mowing_time=mowing_area/2
Output mowing_time to screen
END

RIkip Ginanjar.
41

Assignment 3 – Desk Checking for Mowing_time which now contains a logic error

Calculate_mowing_time
Prompt operator for block_lenght, block_width
Get block_length, block_width
block_area = block_lenght * block_width
Prompt operator for house_lenght, house_width
Get house_lenght, house_width
house_area=block_lenght * block_width
Mowing_area=block_area - house_area
Mowing_time=mowing_area/2
Output mowing_time to screen
END

RIkip Ginanjar.
Flowcharts

Rikip Ginanjar.
43

Symbol

Terminal Symbol:
indicates the starting or stopping pointin the logic.

Input/Output Symbol:
Represents an input or output process in an algorithm

Process Symbol:
Represents any single process in an algorithm

Predefined Process Symbol:

Decision Symbol:
Represents a decision in the logic involving the comparison
Of two values.

RIkip Ginanjar.
The three basic control structures

Rikip Ginanjar.
45

1. Sequence

Statemement a

Statemement b

Statemement c

RIkip Ginanjar.
46

2. Selection

T F
Condition p?

Statemement a Statemement b

RIkip Ginanjar.
47

Selection (2)

F
Condition p?

Statemement a

RIkip Ginanjar.
48

3. Repetition

F
Condition p?

Statemement block

RIkip Ginanjar.
49

Example 12.1 Add three numbers

 A program is required to read three


numbers, add them together and print
their total.

RIkip Ginanjar.
50

• Defining diagram

Input Processing Output

Number1 Read three numbers total


Number2 Add number together
Number3 Print total number

RIkip Ginanjar.
51

Solution Algorithm

Start

Read
Number1
Number2
number3

Add numbers to total

Print total

Stop

RIkip Ginanjar.
52

Example 12.2 Find average temperature

• A program is required to prompt


the terminal operator for the
maximum and minimum temperature
readings on a particular day,
accept those readings as
integers, and calculate and
display to the screen the average
temperature, calculated by
(maximum temperature + minimum
temperature)/2.

RIkip Ginanjar.
53

• Defining diagram

Input Processing Output


Max_temp Prompt for temperatures Avg_temp
Min_temp Get temperatures
Calculate average temperature
Display average temperature

RIkip Ginanjar.
54

Example 12.3 Compute mowing time

• A program is required to read from the


screen the lenght and widht of a
rectangular house block, and the lenght
and width of the rectangular house that
has been built on the block. The
algorithm should then compute and
display the mowing time required to cut
the grass around the house, at the rate
of two square metres per minute.

RIkip Ginanjar.
55

• Defining diagram

Input Processing Output


Block_lenght Prompt for block measurements Mowing_time
Block_width Get block measurements
House_lenght Prompt for house measurements
House_width Get house measurements
Calculate mowing area
Calculate mowing time

RIkip Ginanjar.
56

Solution Algorithm

RIkip Ginanjar.
Flowchart and the selection control
structure

Rikip Ginanjar.
58

Simple IF statement

T F
Account_
balance <
$300?

Service_charge = $5 Service_charge = $2

RIkip Ginanjar.
59

Null ELSE statement

Student_ F
attendance =
P/T?

Increment
part_time_count

RIkip Ginanjar.
60

Combined IF statement

F
Student =
P/T AND
Gender =
F?

Increment
Female_part_time_count

RIkip Ginanjar.
61

Nested IF statement
T F
Record
Code =`A‘ ?

F
T
Record
Increment Code =`A‘ ?
Counter_A
F
Record
Code =`A‘ ?
Increment T
Counter_B

Increment
Increment Error_counter
Counter_C

RIkip Ginanjar.
62

Example 12.4 Read three characters

• Design an algorithm that will


prompt a terminal operator for
three characters, accept those
characters as input, sort them
into ascending sequence and
output them to the screen.

RIkip Ginanjar.
63

• Defining diagram

Input Processing Output

Char_1 Prompt for characters Char_1


Char_2 Accept three characters Char_2
Char_3 Sort three characters Char_3
Output three characters

RIkip Ginanjar.
64

Solution Algorithm ?

RIkip Ginanjar.
65

Case Structure

Case
Of
variable

Value 1 Value 2 Value 3 Value 4

Statement_a Statement_b Statement_c Statement_d

RIkip Ginanjar.
66

RIkip Ginanjar.
67

Flowchart and Array


• Design a program that will prompt for and receive
18 examination scores from a mathematics test,
compute the class average, and display all the
scores and the class average to the screen.

RIkip Ginanjar.
68

• Defining diagram

Input Processing Output


18 exam scores Prompt the scores 18 exam scores
Get scores Class_average
Compute class average
Display scores
Display class average

RIkip Ginanjar.
69

Control Structures required


1. An array to store the exam scores – called
´scores´
2. An index to identify each element in the array
3. A DO loop to accept the scores
4. Another DO loop to display the scores to the
screen.

RIkip Ginanjar.
70
Solution Algorithm

Start

Calculate Display
Total_score = average
zero average

I=1
I=1

Stop

Prompt and
Display
get
Scores (I)
Scores (I)

Add scores(I)
to I=I+1
Total score

T
I=I+1 I <= 18 ?

F
T
I <= 18 ?

RIkip Ginanjar.
71

Flowchart and Module


Design a solution algorithm that will prompt a
terminal operator for three characters, accept those
characters as input, sort them into ascending
sequence and output them to the screen. The
algorithm is to continue to read characters until
´XXX`is entered.

RIkip Ginanjar.
72

• Defining diagram

Input Processing Output


Char_1 Prompt for characters Char_1
Char_2 Accept three characters Char_2
Char_3 Sort three characters Char_3
Output three characters

RIkip Ginanjar.
73

Hierarchy chart

Process_three_
characters

Sort_three_
characters

RIkip Ginanjar.
74

Process_three_characters
Start
Sort_
Three_
Prompt
characters
For
characters

Outpur
Get characters
characters

Prompt
Characters For
NOT = xxx ? characters
F
Stop
Get
T characters

RIkip Ginanjar.
75
Sort_three_characters

Start

T Char_1 >
Char_2 ?
Swap
F
Char_1,
Char_2

Char_2 >
Char_3 ?
T F
Swap
Char_2,
Char_3

Char_1 >
Char_2 ?
Swap
T F
Char_1,
Char_2

Stop
RIkip Ginanjar.
76

Assignment: Calculate Employee‘s pay

A program is required by a company to read an


employee‘s number, pay rate and the number of
hours worked in a week. The program is then to
validate the pay rate and the hours worked fields
and, if valid, compute the employee‘s weekly pay
and print it along with the input data.

RIkip Ginanjar.
77

Assignment (cont‘)
Validation: According to the company‘s rules, the maximum hours an
emplooye can work per week is 60 hours, and the maximum hourly
rate is $25.00 per hour. If the hours worked field or the hourly rate
field is out of range, the input data and an appropriate message is to be
printed and the emplooye‘s weekly pay is not to be calculated.

Weekly pay calculation: Weekly pay is calculated as hours worked times


pay rate. If more than 35 hours are worked, payment for the overtime
hours worked is calculated at time-and half.

RIkip Ginanjar.
78

• Defining diagram

Input Processing Output


Emp_no Read employee details Emp_no
Pay_rate Validate input fields Pay_rate
Hrs_worked Calculate employee pay Hrs_worked
Print employee details Emp_weekly_pay
Error_message

RIkip Ginanjar.
79

Hierarchy chart

Compute_employee_pay

Employee_details

Valid_input_
fields

Read_employee_ Validate_input_ Calculate_ Print_employee_


details fields Employee_pay details

RIkip Ginanjar.
Solution Algorithm

Rikip Ginanjar.
81

Compute_employee_pay

Start Fields
Valid?
Read_
Employee_
details
Calculate_
Employee_
More pay
Recods?

Print_
Validate_ Stop Employee_
Input: details
fields

Read_
Employee_
details

RIkip Ginanjar.
82

Validate_input_fields

Start

Hours_
Valid_input_ Worked
fields = T > 60

Print
Error
Pay_rate message
> $ 25

Print
Error Valid_
message Input_
Fields = T

Valid_
Input_
Fields = F
Stop

RIkip Ginanjar.

You might also like