Practice

Section 4: Algorithm design and problem-solving

Trace line by line and add a row only when a value changes. Check the first and the last pass of every loop.

A lesson for this topic

Quick checks: multiple choice

Question 1
Which list gives the stages of the program development life cycle in order?
Question 2
Which task belongs to the design stage of the program development life cycle?
Question 3
In the analysis stage, what is abstraction?
Question 4
A bike hire kiosk asks the customer for the number of hours, works out the cost, shows the cost on screen and saves the hire record. Which part is a process?
Question 5
A library's self-service machine scans a book, checks the member's card, prints a receipt and keeps a record of the loan. Which part is storage?
Question 6
Which flowchart symbol is used for a question with two exits, Yes and No?
Question 7
A flowchart must show the step INPUT Age. Which symbol is used?
Question 8
A flowchart calls a separate routine called CheckPin that has its own flowchart. Which symbol shows this?
Question 9
What does a structure diagram show?
Question 10
A self-checkout system has a sub-system for taking payment. What can be said about the payment sub-system?
Question 11
During the coding stage, what is iterative testing?
Question 12
Values is an array of six integers. What is the purpose of this algorithm?01 Biggest ← Values[1] 02 FOR i ← 2 TO 6 03 IF Values[i] > Biggest 04 THEN 05 Biggest ← Values[i] 06 ENDIF 07 NEXT i 08 OUTPUT Biggest
Question 13
What is the purpose of this algorithm?01 DECLARE Mark : INTEGER 02 Count ← 0 03 FOR Student ← 1 TO 30 04 INPUT Mark 05 IF Mark >= 50 06 THEN 07 Count ← Count + 1 08 ENDIF 09 NEXT Student 10 OUTPUT Count
Question 14
The array Codes holds 14, 9, 27, 9, 30, 5. What is output when 9 is input?01 DECLARE Target : INTEGER 02 Found ← FALSE 03 Position ← 0 04 i ← 1 05 INPUT Target 06 WHILE Found = FALSE AND i <= 6 DO 07 IF Codes[i] = Target 08 THEN 09 Found ← TRUE 10 Position ← i 11 ENDIF 12 i ← i + 1 13 ENDWHILE 14 OUTPUT Position, " ", i
Question 15
The array Nums holds 7, 3, 9, 2, 5. What is output after this single pass of a bubble sort?01 FOR i ← 1 TO 4 02 IF Nums[i] > Nums[i + 1] 03 THEN 04 Temp ← Nums[i] 05 Nums[i] ← Nums[i + 1] 06 Nums[i + 1] ← Temp 07 ENDIF 08 NEXT i 09 OUTPUT Nums[1], " ", Nums[2], " ", Nums[3], " ", Nums[4], " ", Nums[5]
Question 16
The array List holds 6, 4, 8, 1. What is output?01 Swaps ← 0 02 FOR i ← 1 TO 3 03 IF List[i] > List[i + 1] 04 THEN 05 Temp ← List[i] 06 List[i] ← List[i + 1] 07 List[i + 1] ← Temp 08 Swaps ← Swaps + 1 09 ENDIF 10 NEXT i 11 OUTPUT Swaps
Question 17
What is the difference between totalling and counting in an algorithm?
Question 18
The array Scores holds 12, 15, 9, 10. What is output?01 Total ← 0 02 FOR i ← 1 TO 4 03 Total ← Total + Scores[i] 04 NEXT i 05 Average ← Total / 4 06 OUTPUT Average
Question 19
The array Heights holds 150, 172, 160, 172, 155. What is output?01 MaxPos ← 1 02 FOR i ← 2 TO 5 03 IF Heights[i] >= Heights[MaxPos] 04 THEN 05 MaxPos ← i 06 ENDIF 07 NEXT i 08 OUTPUT MaxPos
Question 20
A form accepts a percentage from 0 to 100. Which validation check rejects an entry of 140?
Question 21
A phone number must be entered with exactly 11 digits. Which validation check is designed for this?
Question 22
A membership code must be two letters followed by four digits, such as KT4821. Which validation check makes sure the code follows this pattern?
Question 23
Which validation check makes sure the email address box on a sign-up form is not left empty?
Question 24
A program asks for a person's age and the user types twelve. Which validation check rejects this?
Question 25
A shop's product codes have four digits and then a check digit. To find the check digit, each digit is multiplied by its position (1, 2, 3, 4), the four results are added, and the check digit is MOD(total, 11). What is the check digit for 3527?
Question 26
What is the difference between validation and verification?
Question 27
A new password is typed twice and the program compares the two entries. Which check is this?
Question 28
A program accepts the number of guests at a table from 1 to 8. Which value is extreme test data?
Question 29
A program accepts the number of guests at a table from 1 to 8. Which pair is boundary test data at the top of the range?
Question 30
A program accepts heights from 50 to 250 centimetres. Which value is abnormal test data?
Question 31
What is output?01 Number ← 1 02 Count ← 0 03 REPEAT 04 Number ← Number * 3 05 Count ← Count + 1 06 UNTIL Number > 50 07 OUTPUT Count, " ", Number
Question 32
What is output?01 Total ← 0 02 Value ← 20 03 WHILE Value < 10 DO 04 Total ← Total + Value 05 Value ← Value + 5 06 ENDWHILE 07 OUTPUT Total
Question 33
What is output when the character b (lower case) is input?01 DECLARE Code : CHAR 02 Points ← 10 03 INPUT Code 04 CASE OF Code 05 'A' : Points ← Points + 5 06 'B' : Points ← Points * 2 07 OTHERWISE Points ← 0 08 ENDCASE 09 OUTPUT Points
Question 34
What is output?01 Age ← 16 02 Member ← TRUE 03 IF Age >= 18 04 THEN 05 Price ← 12 06 ELSE 07 IF Member = TRUE 08 THEN 09 Price ← 5 10 ELSE 11 Price ← 8 12 ENDIF 13 ENDIF 14 OUTPUT Price
Question 35
This algorithm is meant to output the lowest value in Temps, which holds 18, 12, 21, 15, 19. What does it actually output?01 Lowest ← 0 02 FOR i ← 1 TO 5 03 IF Temps[i] < Lowest 04 THEN 05 Lowest ← Temps[i] 06 ENDIF 07 NEXT i 08 OUTPUT Lowest
Question 36
This algorithm should output the total of ten numbers. Which line contains the error?01 DECLARE Number : INTEGER 02 Total ← 0 03 FOR Count ← 1 TO 10 04 INPUT Number 05 Total ← Number 06 NEXT Count 07 OUTPUT Total
Question 37
How many values are output?01 FOR i ← 2 TO 14 STEP 3 02 OUTPUT i 03 NEXT i
Question 38
What is output?01 Number ← 4706 02 Sum ← 0 03 WHILE Number > 0 DO 04 Sum ← Sum + MOD(Number, 10) 05 Number ← DIV(Number, 10) 06 ENDWHILE 07 OUTPUT Sum

Written questions in the papers’ style

Quiz scoresPaper 2 style · 12 marks
This algorithm processes quiz scores. The user enters −1 to stop.01 DECLARE Score : INTEGER 02 Count ← 0 03 Total ← 0 04 Highest ← -1 05 Lowest ← 101 06 OUTPUT "Enter a score, or -1 to stop" 07 INPUT Score 08 WHILE Score <> -1 DO 09 IF Score > Highest 10 THEN 11 Highest ← Score 12 ENDIF 13 IF Score < Lowest 14 THEN 15 Lowest ← Score 16 ENDIF 17 Total ← Total + Score 18 Count ← Count + 1 19 INPUT Score 20 ENDWHILE 21 OUTPUT Highest, " ", Lowest, " ", Total / Count
0 words · a sentence or two

(b) The scores input are 64, 82, 47, 90 and then −1. Complete the trace table: one row each time line 18 runs, and a last row for line 21. The first row is done for you. [4]

ScoreCountTotalHighestLowestOUTPUT
641646464Enter a score, or -1 to stop

Leave a cell empty where nothing changes or nothing is output.

0 words · about 80 to 120 words
Sorting scoresPaper 2 style · 14 marks
The array Scores[1:5] holds 31, 17, 45, 8, 22. This bubble sort puts the scores in order.01 Last ← 5 02 REPEAT 03 Swapped ← FALSE 04 FOR Index ← 1 TO Last - 1 05 IF Scores[Index] > Scores[Index + 1] 06 THEN 07 Temp ← Scores[Index] 08 Scores[Index] ← Scores[Index + 1] 09 Scores[Index + 1] ← Temp 10 Swapped ← TRUE 11 ENDIF 12 NEXT Index 13 Last ← Last - 1 14 UNTIL NOT Swapped OR Last = 1

(a) Complete the table to show Scores, Swapped and Last each time line 13 runs. The first row is done for you. [3]

Scores[1]Scores[2]Scores[3]Scores[4]Scores[5]SwappedLast
173182245TRUE4

Leave a cell empty where nothing changes or nothing is output.

0 words · a sentence or two
0 words · a sentence or two
0 lines
Race resultsPaper 2 scenario question · 15 marks
A school records the results of a 100 metre race for 30 runners. Write a program that: - inputs the name and the time in seconds of each of the 30 runners and stores them in the arrays RunnerName[1:30] and RaceTime[1:30] - validates each time so that only times from 9.0 to 30.0 seconds are accepted, asking again until a valid time is entered - finds and outputs the name and time of the fastest runner - calculates and outputs the average time - counts and outputs how many runners finished in under 15.0 seconds. The program must use the arrays RunnerName and RaceTime as given; you may assume no two runners have the same time.
0 lines