0% found this document useful (0 votes)
28 views

computer-science-chapter-7

Chapter 10 of the CIE IGCSE Computer Science curriculum focuses on pseudocode and flowcharts as tools for representing algorithms. It covers the use of assignment statements, conditional statements (IF...THEN...ELSE and CASE), loop structures (FOR, REPEAT...UNTIL, WHILE), and input/output operations. The chapter also provides examples and activities to practice writing pseudocode for various scenarios.

Uploaded by

anish
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
28 views

computer-science-chapter-7

Chapter 10 of the CIE IGCSE Computer Science curriculum focuses on pseudocode and flowcharts as tools for representing algorithms. It covers the use of assignment statements, conditional statements (IF...THEN...ELSE and CASE), loop structures (FOR, REPEAT...UNTIL, WHILE), and input/output operations. The chapter also provides examples and activities to practice writing pseudocode for various scenarios.

Uploaded by

anish
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 17

lOMoARcPSD|41220776

Computer Science Chapter 10

Computer Science (University of Cambridge)

Scan to open on Studocu

Studocu is not sponsored or endorsed by any college or university


Downloaded by jsa thomas ([email protected])
lOMoARcPSD|41220776

CIE IGCSE COMPUTER SCIENCE


Practical problem-solving and programming
Chapter 10 – Pseudocode and flowcharts
10.1 Introduction
Using pseudocode is a clear and concise way to represent an algorithm. Data items to be processed by the
algorithm are given meaningful names in the same way that variables and constants are in a high-level
programming language. Pseudocode is not bound by the strict syntax rules of a programming language. It
does what its name says; it pretends to be programming code!

To ensure that pseudocode is easily understandable by others it is useful to be consistent in the way that it is
written. The pseudocode here is written in the following way to help understand the algorithm more easily:

 Courier New font is used throughout


 all keywords (words used to describe a specific action (e.g. INPUT) are written in capital letters
 all names given to data items and sub-routines start with a capital letter
 where conditional and loop statements are used, repeated or selected statements are indented by two
spaces.

10.2 Assignment
Values are assigned to an item/variable using the ← operator. The variable on the left of the ← is assigned the
value of the expression on the right. The expression on the right can be a single value or several values
combines with mathematical operators.

Examples of pseudocode assignments:

Cost ← 10 Cost has the value 10

Price ← Cost * 2 Price has the value 20

Tax ← Price * 0.12 Tax has the value 2.4

SellingPrice ← Price + Tax SellingPrice has the value 22.4

Gender ← “M” Gender has the value M

Chosen ← False Chosen has the value False

Activity 10.1
What values will the following variables have after the assignments have been completed?

1
Downloaded by jsa thomas ([email protected])
lOMoARcPSD|41220776

Amount ← 100 Amount = 100

TotalPrice ← Amount * 3.5 TotalPrice = 350

Discount ← 0.2 Discount = 0.2

FinalPrice ← TotalPrice – TotalPrice * Discount FinalPrice = 280

Name ← “Nikki” Name = Nikki

Message ← “Hello” + Name Message = Hello Nikki

10.3 Conditional Statements


When different actions are performed by an algorithm according to the values of the variables, conditional
statements can be used to decide which action should be taken.

There are two types of conditional statements as shown below with an example of each.

 A condition that can be true or false: IF … THEN … ELSE … ENDIF, for example
IF Age < 18
THEN PRINT “Child”
ELSE PRINT “Adult”
ENDIF
 A choice between several different values: CASE … OF … OTHERWISE … ENDCASE, for example
CASE Grade OF
“A” : PRINT “Excellent”
“B” : PRINT “Good”
“C” : PRINT “Average”
OTHERWISE PRINT “Improvement is needed”
ENDCASE

10.3.1 IF … THEN … ELSE … ENDIF


For an IF condition the THEN path is followed if the condition is true and the ELSE path is followed if the
condition is false. There may or may not be an ELSE path. The end of the statement is shown by ENDIF.

A condition can be set up in different ways:

 Using a Boolean variable that can have the value TRUE or FALSE. For example
IF Found
THEN PRINT “Your search was successful”
ELSE PRINT “Your search was unsuccessful”
ENDIF
 Using comparison operators, as shown in the below table. comparisons are made from left to right, for
example A > B means is A greater than B. comparisons can be simple or more complicated. For
example
IF ((Height > 1) OR (Weight > 20) OR (Age > 5)) AND (Age < 70)
THEN PRINT “You can ride”
ELSE PRINT “Too small, too young or too old”
ENDIF

2
Downloaded by jsa thomas ([email protected])
lOMoARcPSD|41220776

The algorithm below checks if a percentage mark is valid and a pass or a fail. This makes use of two IF
statements. The second IF statement is part of the ELSE path of the first IF statement. This is called a nested
IF.

Activity 10.2
Change the algorithm to check for a mark between 0 and 20 and a pass mark of 10.
 INPUT PercentageMark

IF PercentageMark < 0 OR PercentageMark > 20

THEN PRINT “Invalid Mark”

ELSE

IF PercentageMark > 10

THEN PRINT “Pass”

ELSE PRINT “Fail”

ENDIF
3
Downloaded by jsa thomas ([email protected])
lOMoARcPSD|41220776

ENDIF

10.3.2 CASE … OF … OTHERWISE … ENDCASE


For a CASE condition the value of the variable decides the path to be taken. Several values are usually
specified. OTHERWISE is the path taken for all other values. The end of the statement is shown by ENDCASE.

The algorithm below specifies what happens if the value of Choice is 1, 2, 3 or 4.

CASE Choice OF

1 : Answer ← Num1 + Num2

2 : Answer ← Num1 – Num2

3 : Answer ← Num1 * Num2

4 : Answer ← Num1 / Num2

OTHERWISE PRINT “Please enter a valid choice”

ENDCASE

Activity 10.3
Use a CASE statement to display the day of the week if the variable DAY has the value 1 to 7 and an error
otherwise.
 CASE Day OF
1 : PRINT “Monday”
2 : PRINT “Tuesday”
3 : PRINT “Wednesday”
4 : PRINT “Thursday”
5 : PRINT “Friday”
6 : PRINT “Saturday”
7 : PRINT “Sunday”
OTHERWISE PRINT “Invalid Day”
ENDCASE

10.4 Loop structures


When some actions performed as part of an algorithm need repeating, this is called ‘iteration’. Loop structures
are used to perform the iteration.

There are three types of loop structure:

All types of loops can perform the same task, for example printing 10 stars.
FOR Counter ← 1 TO 10

PRINT “*”
4
Downloaded by jsa thomas ([email protected])
lOMoARcPSD|41220776

NEXT

FOR … TO … NEXT
Counter ← 0

REPEAT

PRINT “*”

Counter ← Counter + 1

UNTIL Counter > 10

REPEAT … UNTIL
Counter ← 0

WHILE Counter < 10 DO

PRINT “*”

Counter ← Counter + 1

ENDWHILE

WHILE … DO … ENDWHILE

The FOR …. TO … NEXT loop is the most efficient for this type of task.

10.4.1 FOR … TO … NEXT


A variable is set up with a start value and an end value and then incremented in steps of one until the end
value is reached and the iteration finishes. The variable can be used within the loop so long as its value is not
changed. This type of loop is very useful for reading values into lists.

10.4.2 REPEAT … UNTIL


This loop structure is used when the number of repetitions/iterations is not known and the actions are repeated
UNTIL, a given condition becomes true. The actions in this loop are always completed at least once.

5
Downloaded by jsa thomas ([email protected])
lOMoARcPSD|41220776

10.4.3 WHILE … DO … ENDWHILE


This loop structure is used when the number of repetitions/iterations is not known and the actions are only
repeated WHILE a given condition is true. If the WHILE condition is untrue when the loop is first entered then
the actions in the loop are never performed.

Activity 10.4
a) Write pseudocode to input 10 positive numbers and find the total and the average.
 Total ← 0
Average ← 0
FOR Counter ← 1 to 10
PRINT “Enter positive number”
Input PositiveNumber
Total ← Total + PositiveNumber
NEXT
Average ← (Average + Total) / 10
b) Write pseudocode to input positive numbers, -1 to finish, and find the total and average.
 Total ← 0
Average ← 0
PRINT “Enter positive number, -1 to finish”
Input PositiveNumber[Counter]
WHILE PositiveNumber <> -1 DO
Total ← Total + PositiveNumber
6
Downloaded by jsa thomas ([email protected])
lOMoARcPSD|41220776

PRINT “Enter positive number, -1 to finish”


Input PositiveNumber[Counter]
ENDWHILE
Average ← (Average + Total) / [Counter]

10.5 Input and output statements


INPUT and OUTPUT are used for the entry of data and display of information. Sometimes READ can be used
instead of INPUT; this is usually used for reading from files. Frequently PRINT is used instead of OUTPUT.

INPUT is used for data entry. It is usually followed by a variable where the data input is stored, for example:

INPUT Name

Input StudentMark

OUTPUT/PRINT is used to display information either on a screen or printed on paper. It is usually followed by
a single value that is a string or a variable or a list of values separated by commas, for example:
PRINT Name

PRINT “Your name is”, Name

OUTPUT Name1, Name2, Name3

10.6 Standard actions


The ability to repeat actions is very important in the design of algorithms. When an algorithm is turned into a
program the same set of actions may be repeated many thousands of times, for example, keeping a running
total of the value of goods sold in a supermarket.
RunningTotal ← RunningTotal + Value

Keeping a count of the number of times an action is performed is another standard action, for example:
Count ← Count + 1a

Counting is also used to count down until a certain value is reached, for example, the number of items in stock
in a supermarket:
NumberInStock ← NumberInStock + 1

10.7 Examples of algorithms in pseudocode


Example 1

Tickets are sold for a concert at $20 each. If 10 tickets are bought then the discount is 10%; if 20 tickets are
bought the discount is 20%. No more than 25 tickets can be bought in a single transaction.

a) Use pseudocode to write an algorithm to calculate the cost of buying a given number of tickets.
 REPEAT
PRINT “How many tickets would you like to buy?”
INPUT NumberOfTickets
UNTIL NumberOfTickets > 0 AND NumberOfTickets < 26
IF NumberOfTickets < 10
THEN Discount ← 0
ELSE
IF NumberOfTickets < 20
THEN Discount ← 0.1
ELSE Discount ← 0.2

7
Downloaded by jsa thomas ([email protected])
lOMoARcPSD|41220776

ENDIF
ENDIF
Cost ← NumberOfTickets * 20 * (1 – Discount)
PRINT “Your tickets cost”, Cost
b) Explain how you would test your algorithm.
 Would use test data with values of
0, 26 Expected result rejected
1, 25 Expected results 20, 400
9, 10 Expected results 180, 180
19, 20 Expected results 342, 320

Example 2

A school with 600 students wants to produce some information from the results of the four standard tests in
Maths, Science, English and IT. Each test is out of 100 marks. The information should be the highest, lowest
and average mark for each test and the highest, lowest and average mark overall. All the marks need to be
input.

a) Use pseudocode to write an algorithm to complete this task.


 OverallHighest ← 0
OverallLowest ← 100
OverallTotal ← 0
FOR Test ← 1 to 4
SubjectHighest ← 0
SubjectLowest ← 100
SubjectTotal ← 0
CASE Test OF
1 : SubjectName ← “Maths”
2 : SubjectName ← “Science”
3 : SubjectName ← “English”
4 : SubjectName ← “IT”
OTHERWISE
ENDCASE
FOR StudentNumber ← 1 to 600
REPEAT
PRINT “Enter Student”, StudentNumber, “’s mark for”,
SubjectName
Input Mark
UNTIL Mark < 101 AND Mark > -1
IF Mark < OverallLowest THEN OverallLowest ← Mark
IF Mark < SubjectLowest THEN SubjectLowest ← Mark
IF Mark > OverallHighest THEN OverallHighest ← Mark
IF Mark > SubjectHighest THEN SubjectHighest ← Mark
OverallTotal ← OverallTotal + Mark
SubjectTotal ← SubjectTotal + Mark
NEXT
SubjectAverage ← SubjectTotal / 600
PRINT SubjectName
PRINT “Average is”, SubjectAverage
PRINT “Highest Mark is”, SubjectHighest
PRINT “Lowest Mark is”, SubjectLowest
NEXT

8
Downloaded by jsa thomas ([email protected])
lOMoARcPSD|41220776

OverallAverage ← OverallTotal / 2400


PRINT “Overall Average is”, OverallAverage
PRINT “Overall Highest Mark is”, OverallHighest
PRINT “Overall Lowest Mark is”, OverallLowest
b) Explain how you would test your algorithm.
 For the algorithm to be tested by dry running, it would be a good idea to reduce the number of
students to 5 and the number of subject to 2.

Activity 10.5
a) Identify the changes you would need to make to the algorithm for Example 2 to reduce the number of
students to 5 and the number of subjects to 2.
 OverallHighest ← 0
OverallLowest ← 100
OverallTotal ← 0
FOR Test ← 1 to 2
SubjectHighest ← 0
SubjectLowest ← 100
SubjectTotal ← 0
CASE Test OF
1 : SubjectName ← “Maths”
2 : SubjectName ← “Science”
OTHERWISE
ENDCASE
FOR StudentNumber ← 1 to 5
REPEAT
PRINT “Enter Student”, StudentNumber, “’s mark for”,
SubjectName
Input Mark
UNTIL Mark < 101 AND Mark > -1
IF Mark < OverallLowest THEN OverallLowest ← Mark
IF Mark < SubjectLowest THEN SubjectLowest ← Mark
IF Mark > OverallHighest THEN OverallHighest ← Mark
IF Mark > SubjectHighest THEN SubjectHighest ← Mark
OverallTotal ← OverallTotal + Mark
SubjectTotal ← SubjectTotal + Mark
NEXT
SubjectAverage ← SubjectTotal / 5
PRINT SubjectName
PRINT “Average is”, SubjectAverage
PRINT “Highest Mark is”, SubjectHighest
PRINT “Lowest Mark is”, SubjectLowest
NEXT
OverallAverage ← OverallTotal / 10
PRINT “Overall Average is”, OverallAverage
PRINT “Overall Highest Mark is”, OverallHighest
PRINT “Overall Lowest Mark is”, OverallLowest
b) Identify the test data needed to test Example 2 with the reduced number of students and subjects.
 89, 72, 53, 46, 97, 83, 90, 78, 56, 94
c) With the set of data you have chosen, set up a trace table so that you can compare your expected results
with the actual results when you dry run the algorithm.

Stud Maths Science Ove Over Ove Over


9
Downloaded by jsa thomas ([email protected])
lOMoARcPSD|41220776

ent Ma Tota Ave Low High Ma Tot Aver Low High rall all rall all
Num rk l rag est est rk al age est est Tota Aver Low High
ber e l age est est
0 100 0 0 100 0 0 100 0
1 89 89 89 89 89 89 89
2 72 161 72 161 72
3 53 214 53 214 53
4 46 260 46 260 46
5 97 357 97 357 97
71.4 46 97
1 83 83 83 83 440
2 90 173 90 530
3 78 251 78 608
4 56 307 56 664
5 94 401 94 758
80.2 56 94 75.8 46 97

10.8 Standard flowchart symbols


Flowcharts are drawn using standard symbols.

10.8.1 Begin/End
Terminator symbols are used at the beginning and end of each flowchart.

10.8.2 Process
Process symbols are used to show when values are assigned to an item/variable like an assignment in
pseudocode.

10.8.3 Input/Output
Input/Output symbols are used show input and output of information.

10
Downloaded by jsa thomas ([email protected])
lOMoARcPSD|41220776

10.8.4 Decision
Decision symbols are used to decide which actions is to be taken next. These can be used for selection and
repetitive/iteration.

10.8.5 Flow lines


Flow lines are used to show the direction of flow which is usually, but not always, top to bottom and left to right.

Example 1 (continued)

Tickets are sold for a concert at $20 each, if 10 tickets are bought then the discount is 10%, if 20 tickets are
bought the discount is 20%. No more than 25 tickets can be bought in a single transaction.

c) Draw a flowchart for the algorithm to calculate the cost of buying a given number of tickets.

11
Downloaded by jsa thomas ([email protected])
lOMoARcPSD|41220776

Activity 10.6
Draw a flowchart for the algorithm given in Example 2.

12
Downloaded by jsa thomas ([email protected])
lOMoARcPSD|41220776

13
Downloaded by jsa thomas ([email protected])
lOMoARcPSD|41220776

End-of-chapter questions
1) (7010/0420 P11 Q17 J2011)
Daniel lives in Italy and travels to Mexico, India and New Zealand. The time differences are:

Country Hours Minutes


Mexico -7 0
India +4 +30
New Zealand +11 0
Thus, if it is 10:15 in Italy it will be 14:45 in India.
a. Write an algorithm, using pseudocode or otherwise, which:
 inputs the name of the country
 inputs the time in Italy in hours (H) and minutes (M)
 calculates the time in the country input using the data from the table
 outputs the country and the time in hours and minutes. [4]
 Input CountryName
Input H, M
IF CountryName = “Mexico”
THEN H ← H – 7
ELSE IF CountryName = “India”
THEN H ← H + 4
M ← M + 30
ELSE IF CountryName = “New Zealand”
THEN H ← H + 11
ELSE
PRINT “Error”
PRINT H, M
b. Describe, with examples, two sets of data you would use to test your algorithm. [2]
 Normal hours: (hours which do not change the day) e.g. 8
 Hours which change the day (e.g. 13 + country = New Zealand)
(Extra Answers)
 Normal minutes (which do not change the hour) e.g. 25
 Minutes which change the hours (e.g. 40 + country = India)
2) (7010/0420 P11 Q17 N2010)
A school is doing a check on the heights and weights of all its students. The school has 1000 students. [5]
Write an algorithm, using pseudocode or a flowchart, which
 inputs the height and weight of all 1000 students
 outputs the average (mean) height and weight
 includes any necessary error traps for the input of height and weight.
 TotalHeight ← 0
TotalWeight ← 0
FOR Student = 1 TO 1000
Input Height, Weight
IF Height > 2 OR Height < 0
THEN PRINT “error”: Input Height
IF Weight > 130 OR Weight < 0
THEN PRINT “error”: Input Weight
ELSE TotalHeight ← TotalHeight + Height
TotalWeight ← TotalWeight + Weight
NEXT Student
AverageHeight ← TotalHeight / 1000
AverageWeight ← TotalWeight / 1000
PRINT AverageHeight, AverageWeight
14
Downloaded by jsa thomas ([email protected])
lOMoARcPSD|41220776

3) (7010/0420 P13 Q16 N2012)


A small café sells five types of items:
 bun 0.50 dollars
 coffee 1.20 dollars
 cake 1.50 dollars
 sandwich 2.10 dollars
 dessert 4.00 dollars
Write an algorithm, using pseudocode or a program flowchart only, which
 inputs every item sold during the day
 uses an item called ‘end’ to finish the day’s input
 adds up the daily amount taken for each type of item
 outputs the total takings (for all items added together) at the end of the day
 outputs the type of item that has the highest takings at the end of the day. [6]
 HighestTaking ← 0
TotalBun ← 0
TotalCoffee ← 0
TotalCake ← 0
TotalSandwich ← 0
TotalDessert ← 0
REPEAT
Input Item
IF Item = “bun”
THEN TotalBun ← TotalBun + 0.50
ELSE IF Item = “coffee”
THEN TotalCoffee ← TotalCoffee + 1.20
ELSE IF Item = “cake”
THEN TotalCake ← TotalCake + 1.50
ELSE IF Item = “sandwich”
THEN TotalSandwich ← TotalSandwich + 2.10
ELSE IF Item = “dessert”
THEN TotalDessert ← TotalDessert + 4.00
ELSE PRINT “error”
UNTIL Item = “end”
IF TotalBun > HighestTaking
THEN HighestTaking ← TotalBun
IF TotalCoffee > HighestTaking
THEN HighestTaking ← TotalCoffee
IF TotalCake > HighestTaking
THEN HighestTaking ← TotalCake
IF TotalSandwich > HighestTaking
THEN HighestTaking ← TotalSandwich
IF TotalDessert > HighestTaking
THEN HighestTaking ← TotalDessert
TotalTakings ← TotalBun + TotalCoffee + TotalCake + TotalSandwich +
TotalDessert
PRINT TotalTakings, HighestTaking
4) (7010/0420 P13 Q15 N2013)
500 numbers are being input which should have either one digit (e.g. 5), two digits (e.g. 36), three digits
(e.g.149) or four digits (e.g.8567). [6]
Write an algorithm, using pseudocode or a flowchart only, which
 inputs 5000 numbers
15
Downloaded by jsa thomas ([email protected])
lOMoARcPSD|41220776

 outputs how many numbers had one digit, two digits, three digits and four digits
 outputs the percentage of numbers which were outside the range.
 OneDigit ← 0
TwoDigit ← 0
ThreeDigit ← 0
FourDigit ← 0
OutOfRange ← 0
FOR Digits ← 1 TO 5000
Input Number
IF Number > 999 AND Number < 10000
THEN FourDigit ← FourDigit + 1
ELSE IF Number > 99
THEN ThreeDigit ← ThreeDigit + 1
ELSE IF Number > 9
THEN TwoDigit ← TwoDigit + 1
ELSE IF Number > 0
THEN OneDigit ← OneDigit + 1
ELSE OutOfRange = OutOfRange + 1
NEXT Digits
Percentage ← (OutOfRange / 5000) * 100
PRINT OneDigit, TwoDigit, ThreeDigit, FourDigit, Percentage

16
Downloaded by jsa thomas ([email protected])

You might also like