Professional Documents
Culture Documents
Pseudocode
OBJECTIVES
Upon completion of this chapter, you should be able to:
Define the term pseudocode.
Know advantages and disadvantages of pseudocode.
Use the rules and constraints associated with pseudocode.
Solve word problems using pseudocode.
3.1 Introduction
Pseudocode is statements written in abbreviated form to show the
steps required to solve a specific problem.
٣٦
3.2 Components
The best way to illustrate the pseudocode components is to provide
a sample. Figure 3.1 shows a part of the flowchart of problem 8 in chapter
2, where HW means Hours worked, OP means Overtime Pay, PR means
Pay Rate, , and GP means Gross Pay. The corresponding pseudocode is
shown in Figure 3.2.
٣٧
Which means that if hours worked > 150, you have to execute
the following two statements :
Calculate Overtime Pay = (Hours Worked – 150) * 1.5 * (Pay
( Rate)
Calculate Gross Pay = 150 * (Pay Rate) + Overtime Pay
Where if the hours worked ≤ 150, you have to execute the following
statement :
Calculate Gross Pay = (Pay Rate) * (Hours Worked)
٣٨
3.4 Program Logic Structures
3.4.1. Sequence
3.4.2. Selection
The pseudocode shown in Figure 3.٣ represents how we can code
the selection control structure. In this case an IF statement is presented.
There can be only two outcomes as a result of the IF statement being
processed. One as a result of the tested condition being TRUE and the
other as being FALSE. Therefore the IF-THEN-ELSE control structure
can be represented as follows :
Then
Do all statements listed under “Then”.
(executed when the tested condition is “TRUE”).
Else
Do all statements listed under “Else”.
(executed when the tested condition is “FALSE”).
٣٩
The tested condition can be greater than(>), less than,(<)
equal to(=), greater or equal to(>=), less than or equal to(<=),
or not equal to . The result of testing the condition must be
TRUE or FALSE.
For example:
M= 25
N = 12
IF ( M >= N ) Then
Statement 1
Else
Statement2
END IF
Statement 3
X= 10
Y=5
IF (X – Y) Then
Statement 1
Else
Statement2
END IF
Statement3
٤٠
In this case Statement 1 followed by Statement3 will be
executed because X – Y is positive, this means that the
corresponding logic value is considered True.
X= 10
Y = 10
IF (X – Y) Then
Statement 1
Else
Statement2
END IF
Statement3
3.4.2. Iterations
This statement implies that a certain activity or action will be
performed repetitively a certain number of times or until a certain
condition is met. Figure 3.4 shows a part of the flowchart of problem 6 in
chapter 2, where Sum is the sum of series, T is the value of the current
term, and N is the current number of terms. The corresponding
pseudocode is shown in figure 3.5, where statement
Shows the iteration control structure under pseudocode for the given
problem. Here statements 3 and 4 are repeated until T is less than 0.01.
٤١
Figure 3.4 A Part of flowchart solution for problem 2.6.
٤٢
3.5 Advantages and disadvantages of Pseudocode
٤٣
3.6 SOME EXAMPLES ON PSEUDOCODING
Example 3-1
Solution:
٤٤
Example 3-2
Write a pseudocode to find the sum of first 100 natural numbers.
This means that you want to find Sum where Sum is given by
Sum = 1 + 2 + 3+...........................................+ 99 + 100
Solution:
٤٥
You have to note that
N=N+1
iss not a mathematical expression, but it is an assignment statement. The
correct grammatical form ( syntax) of the assignment statement is given
by
VariableName = expression
which means that at first we calculate the value of the expression then
store it in the variable which name is written to the left of the equal sign.
Returning back to statement
N=N+1
this statement means add one to the contents of variable N, then store the
result into variable N, which means increment the value stored in variable
N. By the same way we can say that
Sum = Sum + N
means that add the current value of Sum to N, then store the result into
Sum.
Example 3-3
Write a pseudocode to find the sum of first 25 odd natural numbers.
numbers
This mean that we want to find Sum, where Sum is given by
Sum = 1 + 3 + 5 ……………………………………………………………here we
add 25 odd natural numbers.
numbers
٤٦
Solution:
T=T+2
5. IF N = 25 Then
Go to step 6
Else
Perform steps 3 through 4 until T is less than 0.01
END IF
6. Print an output line showing the sum of the first
25 odd terms of the series.
series
7. Stop processing.
processing
Example 3-4
Write a pseudocode to find the sum of all terms of the following
series greater than or equal to 0.01.
1 1 1 1
1 …………………………
2 3 4 5
٤٧
Solution:
Example 3-5
٤٨
Solution:
5. Stop Processing
Solution:
1. Input the employee’s payroll data:
• Social Security Number (SSN)
• Pay Rate (PR)
• Hours Worked (HW)
٥٠
3. Tax = 0.20 * (Gross
Gross Pay)
Pay
7. Stop Processing.
٥١
Example 3-7
Solution:
٥٢
3. Input all data for a single employee:
• Birthday date
• Sex code
• Occupation code
• Date of employment
4. IF sex code is ‘Male’ Then
Male_count = Male_count + 1
ELSE
Female_count = Female_count + 1
END IF
5. Calculate the employee’s age:
Age = (Today’s date) – (Birthday date)
6. IF employee’s age greater than 60 Then
Over_60 = Over_60 + 1
Go to 12
END IF
7. IF employee’s age greater than 50 Then
50_60 = 50_60 + 1
Go to 12
END IF
8. IF employee’s age greater than 40 Then
40_49 = 40_49 + 1
Go to 12
END IF
9. IF employee’s age greater than 30 Then
30_39 = 30_39 + 1
Go to 12
END IF
10. IF employee’s age greater than 20 Then
20_29 = 20_29 + 1
Go to 12
END IF
٥٣
11. IF employee’s age less than 20 Then
under_20 = under_20 + 1
END IF
12. Find the period of employment and store it in variable X:
X = Today – (Date of employment)
13. IF X is greater than or equal to 10 Then
10_Yr = 10_Yr + 1
END IF
14. IF occupation code = ‘ENG’ Then
ENG = ENG + 1
END IF
15. IF this is the last employee Then
Print a report showing all the needed statistics.
ELSE
Perform steps 3 through 14 until all employees are
processed.
END IF
16. Stop processing.
٥٤
Example 3-8
0
√ 4
, … … … … … … … … … … … … . 3.1
2
Solution
٥٥
ELSE
END IF
ELSE
Print “The roots are complex.”
Go To Step 5
END IF
5. Stop processing.
٥٦
3-7 Questions
Question 1:
Write a pseudocode for flowchart shown in Figure 3.14
Question 2:
Write a pseudocode to read the ages of Hany and Hesham , then it prints
the name of the elder. (The corresponding flowchart is the solution of
problem 2-2)
٥٧
Question 3:
Write a pseudocode that reads a temperature in Fahrenheit “°F” degrees
and convert it into Celsius “°C” degrees, using the formula
° °
Question 4:
Write a pseudocode that reads the radius of a sphere “r”, then it calculates
its volume “V” and surface area “A” using formulas
where
If the read radius is negative, the code should print a warning message
that states that the radius should be positive, and then terminates.
Question 5:
Write a pseudocode that reads number “x”, then it calculate the value of
function y using the formula
… … … … … ..
٥٨