A program reads data from a text file and stores it in a queue. The linear queue Queue is stored as a 1D array of up to 50 elements. The queue has the following pointers: • HeadPointer – this stores the index of the first element in the queue, initialised to -1 • TailPointer – this stores the index of the last element in the queue, initialised to -1
- (a)Queue is a global array of 50 integers with all elements initialised to -1 in the main program. The two pointers are declared as global variables and initialised to -1 Write program code to declare and initialise Queue, HeadPointer and TailPointer[3]
- (b)The function Enqueue(): • takes an integer parameter to store in the next position in the queue • returns FALSE if the queue is full and the parameter cannot be stored in the queue • returns TRUE if the parameter is stored in the queue • updates the pointers where appropriate. Write program code for Enqueue()[6]
- (c)The function Dequeue(): • returns the next item in the queue, if the queue is not empty • returns -1 if the queue is empty • updates the pointers where appropriate. Write program code for Dequeue()[5]
- (d)The text file QueueData.txt stores positive integers. Each integer is on a new line in the file. The procedure CreateQueue(): • opens the file QueueData.txt • reads in each line from the text file and uses Enqueue() to insert each line into the queue • outputs "Queue full" if any item cannot be inserted into the queue • uses exception handling when opening and reading from the text file. The procedure needs to work for a file that contains an unknown number of lines. Write program code for CreateQueue()[6]
- (e)(i)The main program needs extending to call CreateQueue().The main program then adds together all of the integers stored in the queue, using Dequeue() to access each integer. The total is then output. Write program code to extend the main program.[5]
- (e)(ii)Test your program. Take a screenshot of the output(s).[1]
Mark scheme
Marking points from the official mark scheme (May/June 2025). The mark or mark type is shown in brackets.
- [1 mark each] (a) • (Global) Queue array with 50 integer elements • ...all initialised to -1 • (Global) HeadPointer and TailPointer initialised with -1
- [1 mark each] (b) • Function Enqueue() header (and end) taking one (integer) parameter • Checking if Queue is full ... • ...returning FALSE if full and TRUE if not full • (Otherwise) storing data item at TailPointer + 1 (check incrementing) • Incrementing TailPointer • Checking if this is the first element and incrementing/storing 0 in HeadPointer
- [1 mark each] (c) • Function Dequeue() header (and close) and returning appropriate value in all cases. • Checking if queue is empty ... • ...and returning -1 if empty • Accessing and returning element at Queue[HeadPointer] (before HeadPointer is incremented) • Incrementing HeadPointer
- [1 mark each] (d) • CreateQueue() header (and end) and opening the file to read and closing file in appropriate place • Looping until end of file • Reading in each/all lines (and converting to integer and removing new line) • ...calling Enqueue() once with each value .. • ...checking return value and outputting "Queue full" if full (can output once or many times) • Exception try, catch with appropriate output. All file access within try
- [1 mark each] (e)(i) • Calling CreateQueue() • Calling Dequeue() and storing/using return value ... • ...repeatedly until return value is -1 • ...adding together all return values to create a total within the loop ... • ...outputting the total
- (e)(ii) 1 mark for screenshot showing 3059
Write your answer and get it marked
On Oracle Prep you can write or photograph your own answer and have it marked by AI against this mark scheme, with feedback on what you missed.
Sign up free →