OCR H446 A-Level Computer Science

REVISION BOOKLET – MARK SCHEME

2.3 ALGORITHMS

Content in H446 A-Level Computer Science: 1.1 The characteristics of contemporary processors, input, output and storage devices 1.2 Software and software development 1.3 Exchanging data 1.4 Data types, data structures and algorithms 1.5 Legal, moral, cultural and ethical issues 2.1 Elements of computational thinking 2.2 Problem solving and programming 2.3 Algorithms

www.learn-computerscience.com

EXAM QUESTIONS

QUESTION 1 The array queue shown below is up to hold a small queue. Assume that there is sufficient storage to hold all necessary additions to the queue. Queue

The table below shows variables that are used to maintain the queue: Variable Type Purpose pointer to the front element of front integer the queue pointer to the rear element of rear integer the queue indicates whether the queue is queue_full Boolean full the maximum size of the max integer queue

Shown below is an algorithm that is intended to add an item to the queue.

Identify the parameter that is passed to this procedure.

• Item (1). 1 For 1 mark.

Describe the logical decision that is made.

• The queue is checked to determine if it is full (1), if it is, then stop (1) Up to 2 marks for valid description that

otherwise continue (1). 2 demonstrates application of knowledge and understanding to given context.

This algorithm contains a logic mistake. Explain what the mistake is.

• The front of the queue is incremented (1), an item is placed at (near)

the front of the queue (1), it should be placed at the rear (1). 2 Up to 2 marks for a valid explanation.

Rewrite the algorithm to correct the mistake.

2 For 2 marks. rear = rear − 1 queue[rear] = item

QUESTION 2 A car has a feature in its engine management system that is intended to save fuel and emissions produced when the car is waiting at traffic lights or in a traffic jam. The default option is that the gears are disengaged, and the car is not moving, the engine is switched off. There is a display on the dashboard that indicates when the engine has been switched off in this way. However, sometimes it is necessary to keep the engine running when the car is stationary, in order to provide electric power to charge the battery, run the heater, run the air conditioning system or keep the lights on. This, in turn, is affected by the external and internal temperatures, the settings chosen by the driver and the intensity of light outside. Write an algorithm that would produce the results described for this engine management system feature.

• Use of endless loop while ignition is turned on (1). • If external temp < target then override (1). • If external light < target then override (1). • If internal temperature < set temperature and heater on then override Up to 8 marks – 1 mark for each correct step in (1). process.

• If internal temperature > set temperature and air conditioning on then 8 override (1). Various approaches could be correctly used. Allow • If battery output < target then override (1). credit for evidence of each step as given, however • If wheels not moving and gears not engaged and not override then expressed. stop engine (1). • If override and engine not running then start engine (1).

QUESTION 3 Consider the following algorithm in Fig.2, expressed in pseudocode, as function S:

State the name of the algorithm implemented in Fig.2.

• Binary search (1). 1 For 1 mark.

Describe the purpose of this algorithm

• To locate an item (1) in a list (1). The list is in some order (1). 2 Up to 2 marks for a valid description.

QUESTION 4 It is possible to use XOR together with a key to encrypt a plain text message. For example, to encrypt the bit pattern 111100001111 using the number 5 as a key, the key is repeated as often as necessary to match the length of the message. An XOR operation is performed to generate the encoded message. 111100001111 <- Message to be encrypted 101101101101 <- Key: repeated as necessary 010001100010 <- Encrypted message Write an algorithm to accept a text message and a key that would use the method above to generate an encoded message. Annotate your pseudocode with comments to show how it solves the problem.

Individual steps in pseudocode:

• Input text and key • Get a length of text • Method to convert text to binary such as • ASCII / lookup table • Convert key to binary or accept key as binary Up to 4 marks for valid pseudocode (AO3.2). • Use of loop to iterate through message

duplicate key element Up to 4 marks for annotated comments used (AO2.2). • Perform XOR operation

• Convert back to text Example pseudocode:

Programming marks to be awarded as follows

• pseudocode shows the input of text and the length being established (1-AO3.2) • pseudocode shows a method to convert text and key to binary and binary to text with the

need for the key to be duplicated (1-AO3.2) 8 • pseudocode shows a loop iterating through the message (1-AO3.2) • pseudocode shows the XOR operation being correctly applied (1-AO3.2)

Possible annotated comments:

• The pseudocode will prompt for an input for a message and key and store it as a variable. (1 – AO 2.2) • The length of the message variable is checked and then converted to binary (1 – AO 2.2).

• The key is then converted to binary if needed (1 – AO 2.2). • The loop then duplicates the key and performs a XOR operation, the output of this is then converted back to text which is now encrypted (1 – AO 2.2). QUESTION 5 The layout for a 2-player board game is shown in Fig 2.1.

The game is played by rolling two 6-sided dice and moving that number of spaces. Both players start on the START space. If a player lands on a space occupied by the other player, they move to the next available space. The board is to be stored as a 2-dimensional array. The board shown in Fig 2.1 is a visualisation of the problem. Explain what visualisation means in this example.

2 marks, 1 for defining visualisation, 1 for application to the 2-d array and gridd

• Presents data in an easy-to-grasp way (1) 2 • An array is not actually a grid / table (1)

Each time a player moves, a series of obstacles are to be added to the board. On their turn, each player rolls two dice. The smaller number from the two dice is taken, and that many obstacles will appear on the board in random locations. For example, if a 3 and 6 are rolled, then 3 obstacles will appear. A recursive function is written in pseudocode to perform this task.

The code new obstacle() generates an instance of the object obstacle. Explain the purpose of the code in line 01 in the algorithm.

1 mark per bullet to max 2

• Declares a function called (1) 2 • Has parameter (1)

Identify the line of code where recursion occurs.

• 08 (1) 1

The recursive function could have been written using iteration. Describe the benefits and drawbacks of using recursion instead of iteration.

max 3 marks for benefit, max 3 for drawback, max 4 marks overall Benefit

• More natural to read (1) • Quicker to write / less lines of code. (1) As some functions are naturally recursive (1) • Suited to certain problems (1) For example those using trees (1) • Can reduce the size of a problem with each call. (1)

Drawback 4

• Can run out of stack space / memory (1) (due to too many calls (1)) causing it to crash (1) This can be avoided with tail recursion (1) • More difficult to trace / follow (1) as each frame on the stack has its own set of variables (1) • Requires more memory than the equivalent iterative algorithm. • Usually slower than iterative methods (1) due to maintainence of the stack (1)

Rewrite the function so it uses iteration instead of recursion.

1 mark per bullet

• Loop start and end in correct positions (1) • With correct number of iterations (1)

• Returns a value (1) 4 • All other code correct, in the right place (1)

If a position on the board is not occupied, its value is set to a blank string (“”). The current algorithm does not check if the random space generated is currently occupied. Write a subroutine that takes the generated position of the board, checks if it is free and returns true if free, or false if occupied.

1 mark per bullet, to max 3

• Appropriate declaration of function, taking 2 parameters (1) • Checks position in board against “” correctly (1) • Returns and true correctly (1) e.g. 3

The programmer has been told the recursive function has the of O(n). State the purpose of Big O notation.

1 from

• Evaluate the complexity of the algorithm (1)

• Show how the time / memory / resources increase as the data size 1 increases (1) • Evaluate worst case scenario for the algorithm (1)

Explain what the Big O notation O(n) means for this recursive function.

• As the dice number increases, the time the function takes to run

increases proportionally / linearly (1) 1

QUESTION 6 A 1-dimensional array stores a set of numbered cards from 0 to 7. An example of this data is shown in Fig 4.1.

The programmer wants to search for a specific card in the array. State whether a binary search or a linear search would be the most appropriate method to search for a specific card and justify your answer.

1 mark for linear search, 2 for justification Justification:

• The array is not sorted (1) 3 • Linear does not need ordered / linear goes through all elements from beginning / binary needs a sorted array (1)

A programmer is writing a computer program to sort the cards into the correct order (0 to 7). Show how an would sort the array in Fig 4.1 into the correct order. Draw the array after each move.

1 mark for each set of 2 moves

• 02174356 01274356 (1)

• 01247356 3 Allow follow through if one move is incorrect 01234756 (1) • 01234576 • 01234567 (1)

Describe how a quick sort algorithm works with the data in Fig 4.2.

1 mark per bullet to max 6

• Uses divide-and-conquer (1) • First item becomes pivot / 2 is the pivot (1) • Compare each item to the pivot (e.g. compare 0 to 2, then 1 to 2) (1) • Make two lists, 1 with less than the pivot… (0, 1) (1) • … 1 with more than the pivot (7,4,3,5,6) (1) • Quick sort the new lists (1) • Recombine the sub-lists (1)

OR Example of alternative method

If no description i.e. the candidate has just shown 6 the quick sort, max 4 marks.

marks for:

• Uses divide-and-conquer (1) • Highlight first list element as start pointer, and last list element as end pointer • Repeatedly compare numbers being pointed to… • …if incorrect, swap and move end pointer • …else move start pointer • Split list into 2 sublists • Quick sort each sublist • Repeat until all sublists have only 1 number • Combine sublists

Two other sorting algorithms the programmer could have used are a and . The worst-case scenario for Merge is O(n log(n)) and for Bubble is O(n^2). Compare the use of a merge sort and a bubble sort on this array, evaluating the performance of each sort, making reference to the worst case Big O notation.

AO1: Knowledge and Understanding Indicative Mark Band 3 – High level content (7–9 marks) The candidate demonstrates a thorough knowledge and understanding of merge • Merge sort uses sub-lists and bubble sorts; the material is generally accurate and detailed. • Bubble sort uses a temp element The candidate is able to apply their knowledge and understanding directly and • Bubble sort moves through the list consistently to the context provided. Evidence / examples will be explicitly repeatedly relevant to the explanation. • Merge sort divides the list into smaller There is a well-developed line of reasoning which is clear and logically lists structured. The information presented is relevant and substantiated. • Merge is a recursive algorithm

• Worst case is logarithmic, scales up well Mark Band 2 – Mid level • Worst case is exponential, does not scale (4–6 marks) up well The candidate demonstrates reasonable knoledge and understanding of merge and bubble sorts; the material is generally accurate but at times underdeveloped. AO2: Application The candidate is able to apply their knowledge and understanding directly to the context provided although one or two opportunities are missed. Evidence / • Small data set examples are for the most part implicitly relevant to the explanation. • Few changes are needed The candidate provides a reasonable discussion, the majority of which is • Demonstrates use of merge and / or • focused. Evaluative comments are, for the most part appropriate, although one 9 bubble on the array or two opportunities for development are missed. • Calculations of average speed / best There is a line of reasoning presented with some structure. The information speed / worse speed presented is in the most part relevant and supported by some evidence.

AO3: Evaluation Mark Band 1 – Low Level Candidates will need to evaluate the benefits and (1–3 marks) drawbacks of each The candidate demonstrates a basic knowledge of merge and bubble sorts with e.g. limited understanding shown; the material is basic and contains some inaccuracies. The candidates makes a limited attempt to apply acquired • Merge is fast on large data sets knowledge and understanding to the context provided. • Bubble is intuitive (easier to program) The candidate provides a limited discussion which is narrow in focus. • Both are fast (or even) on smaller data Judgements if made are weak and unsubstantiated. sets The information is basic and comunicated in an unstructured way. The • Bubble's average speed is worse than information is supported by limited evidence and the relationship to the evidence merge may not be clear. • Bubble will be easier to write for such a

small data set 0 marks • Accept argument for either way as long No attempt to answer the question or response is not worthy of credit. as justified