computer-science-chapter-7
computer-science-chapter-7
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:
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.
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
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
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
ELSE
IF PercentageMark > 10
ENDIF
3
Downloaded by jsa thomas ([email protected])
lOMoARcPSD|41220776
ENDIF
CASE Choice OF
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
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
REPEAT … UNTIL
Counter ← 0
PRINT “*”
Counter ← Counter + 1
ENDWHILE
WHILE … DO … ENDWHILE
The FOR …. TO … NEXT loop is the most efficient for this type of task.
5
Downloaded by jsa thomas ([email protected])
lOMoARcPSD|41220776
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
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
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
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.
8
Downloaded by jsa thomas ([email protected])
lOMoARcPSD|41220776
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.
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.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.
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:
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])