Nothing Special   »   [go: up one dir, main page]

Pseudocode Notes

Download as pdf or txt
Download as pdf or txt
You are on page 1of 32

Beaconhouse School System

Pseudocode
There are 3 programming/Pseudocode constructs:
1. Sequence: it refers that instructions should be executed one after another.
2. Selection: This construct is used to make a decision in choosing an option from many
available options on the basis of a condition. So, if a condition is true then one option
would be chosen while if a condition is false then another option will be chosen.
3. Repetition: this construct is used to repeat a block of code as per the given condition.
Algorithm is step by step solution to a given problem. It is written in plain English statements.
Algorithm is usually transformed into pseudocode or program flowchart.
Once the algorithm is tested in pseudocode or program flowchart then finally it is written in a
specific programming language.

Pseudocode is a false code which consists of plain English statements, mathematical notations
and keywords that are commonly found in high level languages.
It does not follow strict rules and style of any particular programming language. Pseudocode is
used for learning programming concept and to describe ideas before coding begins.
Arithmetic Operators:
In pseudocode arithmetic operators are used to perform arithmetic operations. These
operations are listed below.

Arithmetic Operators Meaning


+ Addition
- Subtraction
* Multiplication
/ Division
^ Show Power of a number
Comparison Operators: these operators are used to compare different values.

Operators Comparison
> Greater than
< Less than
= Equal
>= Greater than or equal
<= Less than or equal
<> Not equal
() Group

Tahir Ali BMTL Boys Branch 1


Beaconhouse School System
AND Both
OR Either
NOT not

Assignment Operator:
Assignment operator is used to assign the value or expression to a variable. The value or
expression is on the right side of assignment operator while the variable is on the left side of
the assignment operator.

It is denoted by either of the following.


 ←
 =
 :=
For example:
Num 1 ← 5
Num2 ← 10
Sum ← num1 + num2
Sum = num1 + num2
Sum := num1 + num2

Note:
In CAIE exams, mostly ← is used for assignment operator:
Example 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 Sellilngprice has the value 22.4
Gender ← “M” Gender has the value M
Chosen ← False Chosen has the value false

Input in Pseudocode:

Tahir Ali BMTL Boys Branch 2


Beaconhouse School System
In pseudocode we indicate the operation of taking input from users by either of the following
keywords:
 INPUT
 READ
 ENTER

OUTPUT:
In pseudocode we indicate the operation of displaying a value or an output or any message by
using either of the following keywords:
 OUTPUT
 WRITE
 PRINT

Note:
In CAIE exam, mostly INPUT, OUTPUT and READ, WRITE keywords are used.

Variable:
It is a named memory space which is used to store values.
Variable usually stores two kind of information.
1. All the input values from user must be stored in a variable.
2. Result of some mathematical operation must be stored in a variable.
These are the rules while assigning name to a variable:
 There is no space in a variable name
 Variable name should not be a keyword of pseudocode.
 Variable name should be relevant.

Example 1:
Write pseudocode that will take two numbers as input, calculates their sum and displays
output.
Solution:
PRINT “Please enter tow numbers to add”
INPUT num1
INPUT num2

Tahir Ali BMTL Boys Branch 3


Beaconhouse School System
Sum = num1 + num2
PRINT sum
Or
PRINT “The Answer is: “; sum
In the above written pseudocode, num1 and num2 are the variable names where the two input
number given by user will get store. The sum of num1 and num2 is a mathematical operation
and the answer is saved in a variable named as sum. It is not necessary that name of variable
should be same but it should be relevant.
For example, the variable name sum could be answer, ans, result etc.

Example 2:
Write pseudocode that will take two numbers as input, calculates their product and displays
output.

Example 3:
Write down pseudocode that will take marks of physics, chemistry and math as input,
calculates the average and displays output.
Solution:
PRINT “Please enter marks of physics”
INPUT Phy_marks
PRINT “Please enter marks of chemistry”
INPUT Chem_marks
PRINT “Please enter marks of maths”

Tahir Ali BMTL Boys Branch 4


Beaconhouse School System
INPUT math_marks
Avg = (Phy_marks + Chem_marks + math_marks) / 3
PRINT “Average marks of three subjects = ” ; avg

Totalling and Counting:


Totalling is a process to add up the series of number. A variable named as total or sum is used
to hold on to the running total. So
Total = Total + number
It actually means:
Total (New Value) = Total (old value) + value of number
Note: To perform the totaling, total variable must be total = 0 at the start of pseudocode.
Counting is a process to count how many times something happens.
Count = Count + 1
Count = Count – 1
Count = Count + 5
A variable named as count is used for counting purpose.

Sometimes, it is also represented as


Count incremented by 1
Or
Count Decremented by 1

Repetition:
The process is repeating a block of pseudocode is called as repetition. It is also known as
Looping or Iteration.
There are three types of repetition statements
1. FOR…TO…NEXT
2. WHILE…DO…ENDWHILE
3. REPEAT…UNTIL

Tahir Ali BMTL Boys Branch 5


Beaconhouse School System
FOR…TO…NEXT:
This repetition statement is used when we know how many times an instruction or set of
instructions is to be repeated.
Few things to remember about FOR…TO…NEXT loop are:

 There is a variable in FOR…TO…NEXT loop for controlling the number of iterations and is
known as a control variable. The name of the control variable is usually “Count”.
 We specify the initial (lower) and final (higher) values of the control variable in the
opening statement of the loop. These initial and final values are not restricted to the
numerical values only, they can be variables as well.
 The control variable is automatically incremented by ‘1’ each time the loop ends.
 The value of the control variable is tested at the beginning of the loop and loop is
repeated until the value of control variable is less than or equal to the specified final
value.

Example 4:
Write pseudocode that will take 10 numbers as input and print their average by using FOR … TO
… NEXT loop.
Solution:
Total = 0
FOR count = 1 TO 10
PRINT “Enter Number”
INPUT num
Total = Total + num
NEXT
Avg = Total / 10
PRINT “Average of 10 Numbers is: “; Total
Few things to remember:

 Whenever average is to be calculated then totaling must be performed.


 Assign total = 0 always. As in the above example it is assigned 0.
 Take care of the initial and final limits of count in the condition of a loop. For example,
FOR Count = 1 TO 10 will repeat the code for 10 times but FOR count = 0 TO 10 will
repeat the code for 11 times.
 FOR loop must be terminated by the NEXT keyword.

Tahir Ali BMTL Boys Branch 6


Beaconhouse School System
Example 5:
A geography class decided to measure daily temperature and hours of sunshine per day over a
12 months’ period (365 days). Write pseudocode that inputs the temperature and hours of
sunshine for all 365 days and give output as average temperature for the year and avg.
numbers of hours per day over the year.
Solution:
Total_temp = 0
Total_Shrs = 0

FOR count = 1 TO 365


PRINT “Please enter temperature”
INPUT temp
Total_temp = Total_temp + temp
PRINT “Please enter sunshine hours”
INPUT Shours
Total_Shrs = Total_Shrs + Shours
NEXT

Avgtemp = Total_temp / 365


AvgShrs = Total_Shrs / 365S

PRINT “Average temperature for whole year is :“ ; Avgtemp


PRINT “Average Sunshine hours for whole years is :” ; AvgShrs

Example 6:
Mr. Ali wants to calculate the average marks for his class of 28 students for Urdu, Islamiat and
Pak. Studies. Write down pseudocode that will help him to accomplish this task.

Tahir Ali BMTL Boys Branch 7


Beaconhouse School System
Solution: (Solve at your own)

Tahir Ali BMTL Boys Branch 8


Beaconhouse School System
WHILE…DO…ENDWHILE:
This repetition statement is used when we don’t know how many times an instruction or set of
instructions is to be repeated.
Few things to remember about WHILE…DO…ENDWHILE loop are:

 The loop is repeated until a condition is true and halted when the condition is false.
 Condition is tested at eh beginning of the loop.
 The statements/instruction between DO and ENDWHILE keywords are repeated.

Example 7:
Write pseudocode that will take numbers input and add them while the input number is greater
than or equal to 0. Print the final result.
Solution:
Total = 0
PRINT “Input Number “
INPUT num

WHILE num >= 0


DO
Total = Total + num
INPUT num
ENDWHILE

PRINT “ Total is = “; Total

Example 8:
Write pseudocode that will take numbers input, add them and calculates the average while the
input number is greater than or equal to 0. Print the average of all input numbers.
Solution:
Total = 0

Tahir Ali BMTL Boys Branch 9


Beaconhouse School System
Count = 1

PRINT “Input Number “


INPUT num

WHILE num >=0


DO
Total = Total + num
INPUT num
Count = Count + 1
ENDWHILE

Avg = Total /Count


PRINT “The average is = “, Avg

Note:
Although there is a difference of operation between FOR…TO…NEXT loop and
WHILE…DO…ENDWHILE Loop but still there is a possible way to rewrite a code written in
FOR…TO…NEXT loop into WHILE…DO…ENDWHILE loop.

Example 9:
Rewrite the pseudocode given below by using WHILE…DO...ENDWHILE loop.
Total = 0
FOR count = 1 TO 10
PRINT “Enter Number”
INPUT num
Total = Total + num
NEXT

Tahir Ali BMTL Boys Branch 10


Beaconhouse School System
Avg = Total / 10
PRINT “Average of 10 Numbers is = “, Total

Solution:
Total = 0
Count = 1
WHILE Count <= 10
DO
PRINT “Enter Number”
INPUT num
Total = Total + num
Count = Count + 1
ENDWHILE
Avg = Total / 10
PRINT “Average of 10 Numbers is : “, Total

Example 10:
Rewrite the pseudocode given below by using FOR…TO…NEXT loop.
Total = 0
Count = 1
WHILE Count <= 15
DO
PRINT “Enter a number”
INPUT num
Total = Total + num
Count = Count + 1
ENDWHILE

Tahir Ali BMTL Boys Branch 11


Beaconhouse School System

Avg = Total / Count


PRINT “The average is : “, Avg

Solution:
Total = 0
FOR Count = 1 TO 15
PRINT “Enter a Number “
INPUT num
Total = Total + num
NEXT

AVg = Total / Count


PRINT “The Average is : “, Avh

Example 11:
A pseudocode is given below whih will take age of 18 students and print out the minimum age
of a student. Rewrite all the pseudocode using WHILW…DO…ENDWHILE loop.

PRINT “Enter age of a student”


Min_age = 100
FOR count = 1 TO 18
INPUT age
IF age < Min_age
THEN
Min_age = age
ENDIF

Tahir Ali BMTL Boys Branch 12


Beaconhouse School System
NEXT
PRINT “Minimum age: “, Min_age
Solution: (Solve it at your own)

Example 12:
Rewrite the code written below using WHILE…DO…ENDWHILE Loop.
Total_temp = 0
Total_Shrs = 0
FOR Count = 1 To 365
PRINT “Please enter the temperature”
INPUT temp
Total_temp = Total_temp + temp
PRINT “Please enter sunshine hours”
INPUT Shours
Total_Shrs = Total_Shrs + Shours
NEXT
Avgtemp = Total_temp / 365
AvgShrs = Total_Shrs / 365

PRINT “Average temperature for whole year is :”, Avgtemp


PRINT “Average Shunhine hours for whole year is :”, AvgShrs

Solution:

Tahir Ali BMTL Boys Branch 13


Beaconhouse School System

REPEAT…. UNTIL:
It is a repetition statement that is used when we don’t know how many times an instruction or
set of instructions is to be repeated.

 It is different from WHILE…DO…ENDWHILE because this loop will be repeated until a


condition is false and it will stop executing once the condition is true.
 The condition is tested at the end of loop and even if a condition is true the loop will
execute at least once.

Example 13:
Write the pseudocode that will take numbers as input, add and gives the total as output. The
loop will continue until “0” is given as input.
Solution:
Total = 0
REPEAT
PRINT “Enter a number to add”
INPUT num
Total = Total + num
UNTIL num is “0”
PRINT Total

Example 14:

Tahir Ali BMTL Boys Branch 14


Beaconhouse School System
Write a pseudocode that will take anything as input and will display it as output until A or B is
pressed.
Solution:
REPEAT
PRINT “enter a number, value, character or symbol to display “
INPUT anyvalue
UNTIL anyvalue is “A” OR “B”

Selection Statements:
These are also known as conditional statements.
There are two types of selection statements.

1. IF… THEN … ELSE … ENDIF statement.


2. CASE … OF… OTHERWISE … ENDCASE Statements

IF… THEN … ELSE … ENDIF:


The selection statement is used when we want to perform one operation when a condition is
true and another operation when a condition is false, or another condition is true.

It is used to choose a route from all available routs in an algorithm / pseudocode.

Example 15:
Write pseudocode that will take a number as input and tells whether a number is positive,
negative or zero.

Solution:
PRINT “Enter a number”
INPUT num

Tahir Ali BMTL Boys Branch 15


Beaconhouse School System
IF num > 0
THEN
PRINT “The number is positive”
ELSE IF num = 0
THEN
PRINT “The number is Zero”
ELSE
PRINT “The number is negative “
ENDIF
ENDIF

Exmple 16:
Write pseudocode that will take marks in an exam as input and will tell the grade A* for 90 to
100, A for 80 to 89, B for 70 to 79, C for 60 to 69 and F grade for 0 to 59.
PRINT “Enter Marks”
INPUT marks
IF maks >= 90 AND marks <= 100
THEN
PRINT “The Grade is A* “
ELSE IF marks >= 80 AND marks < 90
THEN
PRINT “ The Grade is A “
ELSE IF marks >= 70 AND marks < 80
THEN
PRINT “ The Grade is B “
ELSE IF marks >= 60 AND marks < 70
THEN

Tahir Ali BMTL Boys Branch 16


Beaconhouse School System
PRINT “ The Grade is C “
ELSE IF marks >= 0 AND marks < 60
THEN
PRINT “ The Grade is F “
ELSE
PRINT “ Invalid Marks “
ENDIF
ENDIF
ENDIF
ENDIF
ENDIF

Note: Each IF statement and ELSE IF statement must have an ENDIF statement to terminate.

Example 17:
Write pseudocode that performs the following: Ask a user to enter a number. If the number is
between 0 and 10, write the word blue. If the number is between 10 and 20, write the word
red. If the number is between 20 and 30, write the word green. If it is any other number, write
that it is not a correct color option.

Solution : (Solve at your own)

Tahir Ali BMTL Boys Branch 17


Beaconhouse School System

Example 18:
Write down pseudocode that will take three numbers as input and tells
a. The Largest Number
b. The Smallest Number
Solution: (a)
PRINT “Enter three numbers “
INPUT num1
INPUT num2
INPUT num3

IF num1 > num2 AND num1 > num3


THEN
PRINT “ The largest number is “, num1
ELSE IF num2 > num1 AND num2 > num3
THEN
PRINT “ The largest number is “, num2
ELSE
PRINT “ The largest number is “, num3
ENDIF
ENDIF

Solution: (B) Solve at you own

Tahir Ali BMTL Boys Branch 18


Beaconhouse School System

CASE … OF… OTHERWISE …. ENDCASE Statement:


When there are too many available routes in an algorithm / pseudocode then it requires too
many IF… THEN… ELSE … ENDIF statements to make a selection among these routes which is
not an easy and it makes pseudocode difficult to manage. To overcome this issue CASE… OF….
OTHERWISE Statement is used.

In Short, CASE … OF… OTHERWISE … ENDCASE is used in place of IF …. THEN … ELSE … ENDIF
statement when we have to make a selection of route from too many available routes.

Example 19:
Write down pseudocode that will take a number as input (from 1 to 7) and print the day name
for corresponding number e.g 1 for Monday 2 for Tuesday and so on.

Solution:
PRINT “Enter a number to display the day name. “
INPUT daynum
CASE daynum OF
1: dayname = “Monday”
2: dayname = “Tuesday”
3: dayname = “Wednesday”
4: dayname = “Thursday”
5: dayname = “Friday”
6: dayname = “Saturday”
7: dayname = “Sunday”

OTHERWISE
PRINT “Error”
daynum = “unknown”

Tahir Ali BMTL Boys Branch 19


Beaconhouse School System

ENDCASE
PRINT “The day is : “, dayname

Example 20:
Write a pseudocode that will take two numbers as input perform any of the basic operation (+,
-, *, /) as per user requirements.

Solution:
PRINT “Enter two numbers”
INPUT num1
INPUT num2
PRINT “Enter the operation”
INPUT Operator
CASE Operator OF
+ : Ans = num1 + num2
- : Ans = num1 - num2
/ : Ans = num1 / num2
*: Ans = num1 * num2
OTHERWISE
PRINT “Invalid Operator”
Ans = “Invalid”
ENDCASE
PRINT “Answer is: “, Ans

Tahir Ali BMTL Boys Branch 20


Beaconhouse School System
Practice Questions (Past Paper):
In CAIE exam, mostly the pseudocodes are asked that can select maximum from many values,
minimum from many values, can calculate average of given values, can count the number of
digits in a given input or number or number and then performing various operations on its
basis.

Question 1:
a. Write an algorithm, using pseudocode or a flowchart, which
a. Inputs a set of positive numbers (which end with -1)
b. Outputs the average (mean) value of the input numbers
c. Outputs the value of the largest (highest) number input.
b. Writhe an algorithm, using pseudocode or a flowchart, which
a. Inputs a whole number (which is > 0)
b. Calculates the number of digits in the number
c. Outputs the number of digits and the original number
(E.g. 147 would give an output of 3, 147)

Solution: (a)
Highest = -100; total = 0; count = 0 (1 mark) initialize values highest can’t be 0
INPUT number (1 mark) inputs in correct place
WHILE number <> -1 DO (1 mark) loop until -1 is input
Total = total + number (1 mark) calculate number total
Count = count + 1 and count numbers input
If number > highest then highest then highest = number (1 mark) highest
INPUT number
ENDWHILE
Average = total / count (1 mark) calculate average values
PRINT average, highest and output average and
highest value

Tahir Ali BMTL Boys Branch 21


Beaconhouse School System
[4]

Solution: (b)
D=0 (1 mark) initialize value
INPUT number (1 mark) input number and set variable to
this
T = number Number
REPEAT (1 mark) correct loop
T = T / 10 (1 mark) method to find number of digits
D=D+1 (1 mark) counting number of digits
UNTIL T < 1
PRINT number, D (1 mark) correct output outside the loop
(Note: there are other ways of finding number of digits e.g.
IF number > 0 THEN d = 1
ELSE IF number > 9 THEN d = 2
…………………………………………………………………………..
ELSE IF number > 999999 THEN d = 7 etc)
[4]

Question 2:
Write a program that should get 50 values from user,
Output how many values are greater than 100.
Output total of those values that are less than 50.
Solution:
FOR I = 1 TO 50
INPUT num
IF num > 100
THEN C = C + 1

Tahir Ali BMTL Boys Branch 22


Beaconhouse School System
IF num < 75
THEN sum = sum + num
NEXT i
PRINT “Greater then 100”, c
PRINT “sum of less than 75”, sum

Question 3:
Write a pseudocode that prints the sale of a shopkeeper for a week.
Solution:
FOR I = 1 TO 7
INPUT Customer
FOR b = 1 TO Customer
INPUT sale
Total = total + sale
NEXT b
PRINT “Total sale for day “, I , “=”, total
NEXT i

Question 4:
Write a pseudocode that print table of 7 till 100, using REPEAT UNTIL LOOP.
I=0
REPEAT
PRINT i
I = I + 7
UNTIL (I < 100)

Question 5:
Write a pseudocode that print table of 7, using FOR TO NEXT LOOP.

Tahir Ali BMTL Boys Branch 23


Beaconhouse School System
In your program ask from user starting value of table and ending value of table through INPUT
command.
INPUT “Table number “, t
INPUT “starting value”, st
INPUT “Ending value”, ev

FOR I = st TO ev
Ans = t * st
PRINT t * st “ = “ ans
NEXT i

Tahir Ali BMTL Boys Branch 24


Beaconhouse School System
Past Papers Question
Q. 4 a 0478 w 18 QP 23

This is a section of program code.

1 Total = 100.00
2 PRINT 'Enter the height of each member of your class, one at a
time, when prompted'
3 FOR Count = 1 TO 30
4 PRINT 'Enter a height in metres'
5 INPUT Height
6 Total = Total + Height
7 PRINT Total / 30
8 Count = Count + 1
9 NEXT Count

(a) There are three errors in this code.


State the line numbers that contain the errors and describe how to correct each error.

Error 1 .......................................................................................................................................

...................................................................................................................................................

...................................................................................................................................................

Error 2 .......................................................................................................................................

...................................................................................................................................................

...................................................................................................................................................

Error 3 .......................................................................................................................................

...................................................................................................................................................

.................................................................................................................................................[3]

(b) State the purpose of this program.

...................................................................................................................................................

...................................................................................................................................................

............................................................................................................................................... [1]

Q. 5 a 0478 w 18 QP 23

Tahir Ali BMTL Boys Branch 25


Beaconhouse School System
The algorithm allows a number to be entered. It then calculates and outputs the next number in
the mathematical series.
Fib = 1
Prev2 = 0
Prev1 = 1
INPUT Number
IF Number = 0
THEN Fib = 0
ENDIF
WHILE Number > 2
Fib = Prev2 + Prev1
Prev2 = Prev1
Prev1 = Fib
Number = Number - 1
ENDWHILE
OUTPUT Fib

(a) Complete the trace table for the input data: 7 [4]

Fib Prev2 Prev1 Number OUTPUT

(b) Complete the trace table for the input data: 2 [2]

Fib Prev2 Prev1 Number OUTPUT

Tahir Ali BMTL Boys Branch 26


Beaconhouse School System
2210 w 18 QP 22
Q. 2 (a) Write an algorithm, using pseudocode, to input three different numbers, multiply the two larger
numbers together and output the result. Use the variables: Number1, Number2 and Number3 for your
numbers and Answer for your result. [5]

(b) Give two sets of test data to use with your algorithm in part (a) and explain why you chose each set.
Test data set 1 ..........................................................................................................................

Reason .....................................................................................................................................

...................................................................................................................................................

Test data set 2 ..........................................................................................................................

Reason .....................................................................................................................................

..............................................................................................................................................[4]

0478 w 18 QP 21

Q. 4 An algorithm is written in pseudocode:


Total
0
FOR Count
1 TO 50
INPUT Num
Total
Total + Num
NEXT Count
OUTPUT Total

(a) Describe the purpose of the algorithm.


...................................................................................................................................................[3]
(b) Re-write the algorithm in pseudocode using a different type of loop.
...................................................................................................................................................[3]

0478 S 18 QP 23

Q. 3 This section of program code reads the contents of the array, totals the numbers and prints
out the sum and average of the numbers. Assume the array is full. Complete the four missing
items by writing them in the spaces provided in this code.

1 Numbers[1:30]
2 Total = 0
3 .............................................. = 0

Tahir Ali BMTL Boys Branch 27


Beaconhouse School System
4 FOR Count = 1 TO .........................................................
5 Number = Numbers[Count]
6 Total = ......................................................... + Number
7 Counter = Counter + 1
8 .................................................................................................. Count
9 PRINT ′The sum of the numbers you entered is ′, Number
10 PRINT ′The average of the numbers you entered is ′, Number / Counter
[4]

Q. 4 An algorithm is written in pseudocode:

INPUT Number
IF Number > 100
THEN OUTPUT ″The number is too large″
ELSE OUTPUT ″The number is acceptable″
ENDIF
(a) Describe the purpose of the algorithm. [2]

(b) (i) The algorithm only allows one attempt at inputting an acceptable value.
State how you would change the algorithm so that it continues until a suitable input is supplied.
[1]
(ii) Re-write the algorithm in full, using pseudocode, to implement your answer to part (b)(i).
[3]
0478 S 18 QP 22

Q. 3 This pseudocode algorithm inputs two non-zero numbers and a sign, and then performs the
calculation shown by the sign. An input of zero for the first number terminates the process.

INPUT Number1, Number2, Sign


WHILE Number1 <> 0
IF Sign = '+' THEN Answer Number1 + Number2 ENDIF
IF Sign = '-' THEN Answer Number1 - Number2 ENDIF
IF Sign = '*' THEN Answer Number1 * Number2 ENDIF
IF Sign = '/' THEN Answer Number1 / Number2 ENDIF
IF Sign <> '/' AND Sign <> '*' AND Sign <> '-' AND Sign <> '+'
THEN Answer 0
ENDIF
IF Answer <> 0 THEN OUTPUT Answer ENDIF
INPUT Number1, Number2, Sign
ENDWHILE

(a) Complete the trace table for the input data: [3]
5, 7, +, 6, 2, –, 4, 3, *, 7, 8, ?, 0, 0, /
Number 1 Number 2 Sign Answer OUTPUT

Tahir Ali BMTL Boys Branch 28


Beaconhouse School System

(b) Show how you could improve the algorithm written in pseudocode by writing an alternative
type of conditional statement in pseudocode. [3]

0478 S 18 QP 21

Q. 2 (a)
Write an algorithm to input 1000 numbers. Count how many numbers are positive and how many
numbers are zero. Then output the results. Use either pseudocode or a flowchart.
[6]
(b) Give one change you could make to your algorithm to ensure initial testing is more
manageable. [1]

Q. 3 The global trade item number (GTIN-8) barcode has seven digits and a check digit.
This pseudocode algorithm inputs seven digits and calculates the eighth digit, then outputs the
GTIN-8.
DIV(X,Y), finds the number of divides in division for example DIV(23,10) is 2.
MOD(X,Y), finds the remainder in division for example MOD(23,10) is 3.

FOR Count 1 TO 7
INPUT Number
Digit(Count) Number
NEXT
Sum (Digit(1)+Digit(3)+Digit(5)+Digit(7))*3+Digit(2)+Digit(4)+Digit(6)
IF MOD(Sum,10) <> 0
THEN Digit(8) DIV(Sum,10)*10 + 10 - Sum
ELSE Digit(8) 0
ENDIF
OUTPUT "GTIN-8"
FOR Count 1 TO 8
OUTPUT Digit(Count)
NEXT

(a) Complete the trace table for the input data: 5, 7, 0, 1, 2, 3, 4

Digit(1) Digit(2) Digit(3) Digit(4) Digit(5) Digit(6) Digit(7) Digit(8) Sum OUTPUT

Complete the trace table for the input data: 4, 3, 1, 0, 2, 3, 1

Digit(1) Digit(2) Digit(3) Digit(4) Digit(5) Digit(6) Digit(7) Digit(8) Sum OUTPUT

Tahir Ali BMTL Boys Branch 29


Beaconhouse School System

[5]

Q. 3 (b) Explain how you would change the algorithm to input eight digits (seven digits and the check
digit) and output if the check digit entered is correct or not. [3]

0478 W 17 QP 23

Section B
Q. 2 This section of program code asks for 80 numbers between 100 and 1000 to be entered. It
checks that the numbers are in the correct range, and stores them in an array. It counts how
many of the numbers are larger than 500 and then outputs the result when the program is
finished.
1 Count = 0
2 FOR Index = 1 TO 80
3 INPUT 'Enter a number between 100 and 1000', Number
4 WHILE Number = 99 AND Number = 1001
5 INPUT 'This is incorrect, please try again', Number
6 ENDWHILE
7 Num[80] = Number
8 IF Number > 500 THEN Count = Count + 1
9 UNTIL Index = 80
10 PRINT Index
11 PRINT ' numbers were larger than 500'
There are four lines of code that contain errors.
State the line number for each error and write the correct code for that line.
Error 1 Line Number .............................
Correct Code .................................................................................................................................
Error 2 Line Number .............................
Correct Code .................................................................................................................................
Error 3 Line Number .............................
Correct Code .................................................................................................................................
Error 4 Line Number .............................
Correct Code ............................................................................................................................ [4]

0478 W 17 QP 22

Q. 2 Write an algorithm using either pseudocode or a flowchart, to:

• input a positive integer

• use this value to set up how many other numbers are to be input

• input these numbers

• calculate and output the total and the average of these numbers. [6]

Tahir Ali BMTL Boys Branch 30


Beaconhouse School System

0478 S 17 QP 23
Q. 2 This section of program code asks for 50 numbers to be entered. The total and average of
the numbers are calculated.
1 Total = 0
2 Counter = 50
3 PRINT 'When prompted, enter 50 numbers, one at a time'
4 REPEAT
5 PRINT 'Enter a number'
6 INPUT Number
7 Total + Number = Total
8 Number = Number + 1
9 UNTIL Counter = 50
10 Average = Number * Counter
11 PRINT 'The average of the numbers you entered is ', Average

There are four errors in this code.


State the line number for each error and write the correct code for that line.
Error 1 Line number .............................
Correct code ...................................................................................................................................
Error 2 Line number .............................
Correct code ...................................................................................................................................
Error 3 Line number .............................
Correct code ...................................................................................................................................
Error 4 Line number .............................
Correct code .............................................................................................................................. [4]

Q. 5 (a) Describe the purpose of each statement in this algorithm.

FOR I = 1 TO 300
INPUT Name[I]
NEXT I
............................................................................................................................................... [2]

(b) Identify, using pseudocode, another loop structure that the algorithm in part (a) could have
used.
............................................................................................................................................... [1]

(c) Write an algorithm, using pseudocode, to input a number between 0 and 100 inclusive. The
algorithm should prompt for the input and output an error message if the number is outside this
range.
.................................................................................................................................................[3]

0478 S 17 QP 22

Section B

Tahir Ali BMTL Boys Branch 31


Beaconhouse School System
Q. 2 (a) Write an algorithm to input three different numbers, and then output the largest number.
Use either pseudocode or a flowchart. [4]

(b) Give two sets of test data to use with your algorithm in part (a) and explain why you chose
each set.
Test data set 1 ...........................................................................................................................
Reason ......................................................................................................................................
Test data set 2 ...........................................................................................................................
Reason ...................................................................................................................................[4]

Q .4 An algorithm has been written in pseudocode to input 100 numbers and print out the sum.
A REPEAT … UNTIL loop has been used.
Count = 0
Sum = 0
REPEAT
INPUT Number
Sum = Sum + Number
Count = Count + 1
UNTIL Count > 100
PRINT Sum

(a) Find the error in the pseudocode and suggest a correction.


Error ...........................................................................................................................................
Correction ................................................................................................................................[2]

(b) Rewrite the correct algorithm using a more suitable loop structure.
............................................................................................................................................... [3]

Tahir Ali BMTL Boys Branch 32

You might also like