Introduction To Pseudocode: Rikip Ginanjar
Introduction To Pseudocode: Rikip Ginanjar
Rikip Ginanjar.
2
What is Pseudocode?
RIkip Ginanjar.
3
Pseudocode Convention
RIkip Ginanjar.
4
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.
RIkip Ginanjar.
5
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.
RIkip Ginanjar.
6
Verb used:
•Compute
•Calculate
•Symbols used:
+, -, *, /, ()
RIkip Ginanjar.
7
• 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.
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
Keyword used:
DOWHILE, ENDDO
RIkip Ginanjar.
10
Meaningful names
RIkip Ginanjar.
11
RIkip Ginanjar.
12
Sequence
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
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
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
RIkip Ginanjar.
16
RIkip Ginanjar.
17
RIkip Ginanjar.
18
• Defining diagram
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
RIkip Ginanjar.
21
• Defining diagram
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
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
RIkip Ginanjar.
26
RIkip Ginanjar.
27
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
Number 1 10 40
Number 2 20 41
Number 3 30 42
RIkip Ginanjar.
30
Total 60 123
RIkip Ginanjar.
31
3. Set up a table of relevant variable names, and pass each test data set
statement by statement.
RIkip Ginanjar.
32
4. Check the expected results (60 and 123) match the actual results.
RIkip Ginanjar.
33
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
Max_temp 30 40
Min_temp 10 20
RIkip Ginanjar.
36
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.
RIkip Ginanjar.
38
RIkip Ginanjar.
39
Assignment 2:
Desk Checking for
Compute mowing time
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
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
RIkip Ginanjar.
50
• Defining diagram
RIkip Ginanjar.
51
Solution Algorithm
Start
Read
Number1
Number2
number3
Print total
Stop
RIkip Ginanjar.
52
RIkip Ginanjar.
53
• Defining diagram
RIkip Ginanjar.
54
RIkip Ginanjar.
55
• Defining diagram
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
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
RIkip Ginanjar.
63
• Defining diagram
RIkip Ginanjar.
64
Solution Algorithm ?
RIkip Ginanjar.
65
Case Structure
Case
Of
variable
RIkip Ginanjar.
66
RIkip Ginanjar.
67
RIkip Ginanjar.
68
• Defining diagram
RIkip Ginanjar.
69
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
RIkip Ginanjar.
72
• Defining diagram
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
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.
RIkip Ginanjar.
78
• Defining diagram
RIkip Ginanjar.
79
Hierarchy chart
Compute_employee_pay
Employee_details
Valid_input_
fields
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.