23
٣٦ CHAPTER THREE 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. Pseudocode means "false code". That means that it is not the programming language "code" or statements that are used to direct the action of the computer. After writing the Pseudocode it is very easy to translate these statements into computer programming language instructions that can direct the action of the computer. There are no special symbols with special meaning (such as flowcharting) that have to be memorized. Its rules and constraints are few, flexible, and easy to understand.

Pseudo Code PDF

Embed Size (px)

Citation preview

Page 1: Pseudo Code PDF

٣٦

CHAPTER THREE

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.

Pseudocode means "false code". That

means that it is not the programming

language "code" or statements that are used

to direct the action of the computer. After

writing the Pseudocode it is very easy to

translate these statements into computer

programming language instructions that can

direct the action of the computer.

There are no special symbols with special meaning (such as flowcharting)

that have to be memorized. Its rules and constraints are few, flexible, and

easy to understand.

Page 2: Pseudo Code PDF

٣٧

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.

Figure 3.1 A Part of flowchart solution for problem 2.٨.

Figure 3.2 Pseudocode corresponding to Figure 3.1.

1. If hours worked is greater than 150 Then

Calculate Overtime Pay = (Hours Worked – 150) * 1.5 * (Pay Rate)

Calculate Gross Pay = 150 * (Pay Rate) + Overtime Pay

Else

Calculate Gross Pay = (Pay Rate) * (Hours Worked)

2. Calculate TAX = 0.20 * (Gross Pay)

Page 3: Pseudo Code PDF

Which means that if hours worked

the following two statements

Calculate Overtime Pay = (Hours Worked

Calculate Gross Pay

Followed by the following statement

Calculate TAX

Where if the hours worked

statement :

Calculate Gross Pay = (Pay Rate) * (Hours Worked)

Followed by the following statement

Calculate TAX

This shows that the only components used to

write a pseudocode are

1. Words

2. Clauses

3. Sentences

These components and eas

pseudocode so attractive.

3.3 Rules and Constraints

1. The data names of variables being used to solve the

problem should describe what the variable represents.

2. All statements should be written in a

easy to understand.

٣٨

hours worked > 150, you have to execute

the following two statements :

Calculate Overtime Pay = (Hours Worked – 150) * 1.5 * (

Calculate Gross Pay = 150 * (Pay Rate) + Overtime Pay

Followed by the following statement :

Calculate TAX = 0.20 * (Gross Pay)

worked ≤ 150, you have to execute the following

Calculate Gross Pay = (Pay Rate) * (Hours Worked)

Followed by the following statement :

Calculate TAX = 0.20 * (Gross Pay)

This shows that the only components used to

write a pseudocode are

These components and easiness of use make

pseudocode so attractive.

Rules and Constraints

The data names of variables being used to solve the

problem should describe what the variable represents.

All statements should be written in a way that is

understand.

you have to execute

* (Pay Rate)

Overtime Pay

you have to execute the following

The data names of variables being used to solve the

problem should describe what the variable represents.

way that is

Page 4: Pseudo Code PDF

٣٩

3.4 Program Logic Structures

A program is consisting of a number of structures. The most important

structures are as follows:

3.4.1. Sequence

In pseudo coding, statements are done in sequential fashion unless

one of the statements contains an instruction that interrupts the normal

sequential flow.

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 :

Figure 3.3 IF-THEN-ELSE control structure.

IF condition being tested is TRUE

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”).

Page 5: Pseudo Code PDF

٤٠

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

Note that in this case Statement 1, followed by Statement 3 will be

executed.

Note that if the tested condition was just an expression the result of

which was a number, then only the value of zero will represent FALSE,

where all other values will represent TRUE.

For example

X= 10

Y = 5

IF (X – Y) Then

Statement 1

Else

Statement2

END IF

Statement3

Page 6: Pseudo Code PDF

٤١

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.

When the code is given as follows :

X= 10

Y = 10

IF (X – Y) Then

Statement 1

Else

Statement2

END IF

Statement3

In this case Statement2 followed by Statement3 will be executed because

X – Y is zero, this means that the corresponding logic value is considered False.

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

Perform steps 3 through 4 until T is less than 0.01

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.

Page 7: Pseudo Code PDF

٤٢

Figure 3.4 A Part of flowchart solution for problem 2.6.

Figure 3.5 Pseudocode corresponding to figure 3.4.

1. Initialize the Sum:

Sum = 0

2. Initialize the number of terms N, then find the corresponding

term T:

N = 1

T = 1/N

3. Add the new term to Sum:

Sum = Sum + T

4. Increment the number of terms N, then find the

corresponding term T:

5. IF T<0.01 Then

Go to step 6

Else

Perform steps 3 through 4 until T is less than 0.01

END IF

6. Stop processing

Page 8: Pseudo Code PDF

٤٣

3.5 Advantages and disadvantages of Pseudocode

Some of advantages of pseudocode are :

• It is simple because it uses English-like statements.

• No special symbols are used.

• No specific syntax is used.

• It is very easy to translate its statements to that for high level

languages on a one-for-one basis.

The main disadvantages of pseudocode is that :

• It can be quite lengthy for complex problems.

Page 9: Pseudo Code PDF

٤٤

3.6 SOME EXAMPLES ON PSEUDOCODING

Example 3-1

Write a pseudocode to represent the process of reading two numbers,

dividing them, and then displaying the result.

Note that: the corresponding flowchart is given in example 2.3.

Solution:

1. Input (Read) the first number “number1”.

2. Input (Read) the second number “number2”.

3. IF the value of number1 = that of number2 Then

Print “Division is impossible because number2 is zero”

Go to step 4

ELSE

result = number1 / number2

Print number1, number2, and result

END IF

4. Stop processing.

Figure 3.6 Pseudocode corresponding to example 3.1.

Note that in the given pseudocode, after first printing statement ,

we jump to label 4 to terminate the program.

Page 10: Pseudo Code PDF

٤٥

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

Note that: the corresponding flowchart is given in example 2.٤.

Solution:

Figure 3.7 Pseudocode corresponding to example 3.2.

1. Initialize the Sum:

Sum = 0

2. Initialize the term number N, which is the term itself:

N = 0

3. Increment the term number:

N = N + 1

4. Add the new term to Sum:

Sum = Sum + N

5. IF N= 100 Then

Go to step 6

Else

Perform steps 3 through 4 until N is equal to 100

END IF

6. Print an output line showing the sum of the first 100 terms of

the series

7. Stop processing

Page 11: Pseudo Code PDF

You have to note that

is not a mathematical

correct grammatical form ( syntax) of the assignment statement is given

by

which means that at first we calculate the value of the expression then

store it in the variable which

Returning back to statement

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

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

This mean that we want to find Sum, where Sum is given by

Sum = 1 + 3 +

add 25 odd natural numbers

Note that the corresponding flowchart is given

in example 2.٥.

٤٦

N = N + 1

s not a mathematical expression, but it is an assignment statement. The

grammatical form ( syntax) of the assignment statement is given

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

that add the current value of Sum to N, then store the result into

Write a pseudocode to find the sum of first 25 odd natural numbers

This mean that we want to find Sum, where Sum is given by

+ 5 ……………………………………………………………here we

odd natural numbers.

the corresponding flowchart is given

expression, but it is an assignment statement. The

grammatical form ( syntax) of the assignment statement is given

which means that at first we calculate the value of the expression then

name is written to the left of the equal sign.

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

that add the current value of Sum to N, then store the result into

odd natural numbers.

This mean that we want to find Sum, where Sum is given by

……………………………………………………………here we

Page 12: Pseudo Code PDF

Solution:

Figure 3.٨ Pseudocode corresponding to

Example 3-4

Write a pseudocode to find the sum of all terms of the following

series greater than or equal to

��� � 1 �

Note that the corresponding flowchart is given in example

1. Initialize the Sum:

Sum =

2. Initialize the number of terms N, and the first term T:

N = 0

T = 1

3. Add the new term to Sum:

Sum = Sum + T

4. Increment the number of terms by

by 2:

5. IF N = 25 Then

Go to step

Else

Perform steps

END IF

6. Print an output line showing the sum of the

25 odd terms of the series

7. Stop processing

٤٧

Pseudocode corresponding to example 3.٣.

Write a pseudocode to find the sum of all terms of the following

series greater than or equal to 0.01.

1

2�

1

3�

1

4�

1

5� … … … … … … … … …

the corresponding flowchart is given in example

Initialize the Sum:

= 0

Initialize the number of terms N, and the first term T:

new term to Sum:

Sum = Sum + T

Increment the number of terms by 1, and the term value

N = N + 1

T = T + 2

Then

Go to step 6

Perform steps 3 through 4 until T is less than 0.01

Print an output line showing the sum of the first

odd terms of the series.

Stop processing.

Write a pseudocode to find the sum of all terms of the following

… …

the corresponding flowchart is given in example 2.٦.

Initialize the number of terms N, and the first term T:

and the term value

Page 13: Pseudo Code PDF

٤٨

Solution:

Figure 3.8 Pseudocode corresponding to problem 3.3.

Figure 3.9 Pseudocode corresponding to example 3.4.

Example 3-5

Write a pseudocode to find the largest of three numbers A, B,

and C.

Note that the corresponding flowchart is given in example 2.7.

1. Initialize the Sum:

Sum = 0

2. Initialize the number of terms N, then find the corresponding

term T:

N = 1

T = 1/N

3. Add the new term to Sum:

Sum = Sum + T

4. Increment the number of terms N, then find the

corresponding term T:

N = N + 1

T = 1/N

5. IF T<0.01 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 all terms until we

reach a term which value is less than 0.01

7. Stop processing.

Page 14: Pseudo Code PDF

٤٩

Solution:

Figure 3.10 Pseudocode corresponding to example 3.5.

Note that in the given pseudocode, after each printing statement ,

we jump to label 5 to terminate processing except at the printing

statement just before the “Stop Processing” statement, because the Stop

Processing statement will be executed automatically for this printing

statement.

1. Input (Read) the values of three numbers A, B, and C

Initialize the Sum:

2. IF the value of A > that of B Then

Go to step 3

ELSE

Go to step 4

END IF

3. IF the value of A > that of C Then

Print “The maximum value is that of A”

Go to step 5

ELSE

Print “The maximum value is that of C”

Go to step 5

END IF

4. IF the value of B > that of C Then

Print “The maximum value is that of B”

Go to step 5

ELSE

Print “The maximum value is that of C”

END IF

5. Stop Processing

Page 15: Pseudo Code PDF

٥٠

Example 3-6

Ahlia Company desires to generate its payroll via computer. The

information collected every payroll period includes the employee social

security number, pay rate, and hours worked. The payroll department

would like a report listing these items in addition to overtime pay, gross

pay, tax withheld (20% of gross pay), and net pay. Note that the

employee should be given overtime pay 1.5 of pay rate in case that he

worked a number of hours greater than 150 hours per month. Write a

pseudocode to process all employees and prints out Social Security

Number , Pay Rate, Hours Worked, Gross Pay, Tax, and Net Pay for

each employee.

Note that the corresponding flowchart is given in example 2.8.

Solution:

1. Input the employee’s payroll data:

• Social Security Number (SSN)

• Pay Rate (PR)

• Hours Worked (HW)

2. IF Hours Worked is greater than 150 Then

Overtime Pay = ((Hours Worked) – 150) * 1.5 * (Pay Rate)

Gross Pay = 150 * (Pay Rate) + (Overtime Pay)

ELSE

Gross Pay = (Pay Rate) * (Hours Worked)

END IF

Page 16: Pseudo Code PDF

Figure 3.11

3. Tax = 0.20 * (Gross Pay

4. Net Pay = (Gross Pay )

5. Write report for the employee including the following items:

1. Social Security Number

2. Pay Rate

3. Hours Worked

4. Gross Pay

5. Tax

6. Net Pay

6. IF the employee is the last one Then

Go To Step

ELSE

Perform steps

END IF

7. Stop Processing.

٥١

1 Pseudocode corresponding to example 3.6

.

Gross Pay)

Net Pay = (Gross Pay ) – TAX

Write report for the employee including the following items:

Social Security Number

Pay Rate

Hours Worked

Gross Pay

Net Pay

IF the employee is the last one Then

Go To Step 7

Perform steps 1 though 5 until the last employee is processed

Stop Processing.

6

Write report for the employee including the following items:

until the last employee is processed

Page 17: Pseudo Code PDF

٥٢

Example 3-7

The manager of your company has requested a report from the

Personnel Department about all employees . The report should

include the following information:

1- The total number of males and females.

2- The number of employees by the following age categories:

a. Less than 20

b. 20-29

c. 30-39

d. 40-49

e. 50-60

f. Over 60

3- The total number of people who have been working for the

company for 10 years or more.

4- The total number of engineers.

Write down the needed pseudocode.

Note that the corresponding flowchart is given in example 2.9.

Solution:

1. Set all counters to zero:

Male_count = Female_count = over_60 = 50_60 = 40_49 = 0

30_39 = 20_29 = under_20 = 10_YR = ENG = 0

2. Input today’s date and store it in variable Today

Page 18: Pseudo Code PDF

٥٣

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

Page 19: Pseudo Code PDF

٥٤

Figure 3.12 Pseudocode corresponding to example 3.7.

Note that Male_count, Female_count, 10_YR, and ENG are variables

used to store the number of males, females, employees who have been

working for the company for 10 years or more, and Engineers working

for the company.

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.

Page 20: Pseudo Code PDF

٥٥

Example 3-8

Write down a pseudocode to solve an equation of second order given

by:

��� � �� � � � 0

Given that its roots � and �� are given by

�,� ��� √�� � 4��

2� … … … … … … … … … … … … . �3.1�

where a, b, and c are real numbers. You have to take into

consideration the following:

2. If �� � 4�� is negative , you have to say that we have

complex roots.

3. If �� � 4�� is equal to zero the two roots are equal to –b/2a.

4. If ( �� � 4��) greater than zero the solution will given by

equation 3.1. Note that the corresponding flowchart is given in example 2.10.

Solution

� � �� � ���

�� � �� � √�� � ���

��

�� � �� � √�� � ���

��

1. Input the values of a, b, and c.

2. Calculate y where y is given by:

3. IF y ≥ 0 Then

IF y > 0 Then

Page 21: Pseudo Code PDF

٥٦

Figure 3.13 Pseudocode corresponding to example 3.8.

�� � �� � ��

��

ELSE

END IF

ELSE

Print “The roots are complex.”

Go To Step 5

END IF

4. Print the values of ��� ��.

5. Stop processing.

Page 22: Pseudo Code PDF

٥٧

3-7 Questions

Question 1:

Write a pseudocode for flowchart shown in Figure 3.14

Figure 3.14 Flowchart for problem 1.

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)

Page 23: Pseudo Code PDF

٥٨

Question 3:

Write a pseudocode that reads a temperature in Fahrenheit “°F” degrees

and convert it into Celsius “°C” degrees, using the formula

°°°°� � �

� �°°°°� � �

(The corresponding flowchart is the solution of problem 2-3 )

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

� � � � �

��

��

��

�� … … … … … ..

Where we add 100 terms.