Skip to main content
A-LevelComputer ScienceData types and structures9618/42 · May/June 2025 · Q127 Marks

A program reads data from a text file and stores it in a stack. The stack Stack is stored as a 1D array of up to 20 elements. The pointer TopOfStack stores the index of the last element stored in the stack.

  1. (a)Stack is a global array of strings with all elements initialised to "-1" TopOfStack is a global variable initialised to -1 Write program code to declare and initialise Stack and TopOfStack Save your program as Question1_J25. Copy and paste the program code into part 1(a) in the evidence document.[2]
  2. (b)The function Push () takes a string parameter and attempts to store it on the stack. The function returns -1 if the stack is full. The function returns 1 if the parameter is successfully pushed onto the stack. Write program code for Push() Save your program. Copy and paste the program code into part 1(b) in the evidence document.[4]
  3. (c)The function Pop () returns the next item from the stack. The function returns "-1" if the stack is empty. Write program code for Pop() Save your program. Copy and paste the program code into part 1(c) in the evidence document.[4]
  4. (d)The text file StackData.txt stores numbers and mathematical operators. Each number and operator are on a new line in the text file. The mathematical operators used in this program are: mathematical operator + - / * Λ meaning addition subtraction division multiplication power of The procedure ReadData(): • takes a string filename as a parameter • opens the file and reads in each line of data • uses the Push () function to insert each line of data onto the stack • outputs "Stack full" if any data cannot be stored on the stack because the stack is full • 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 ReadData() Save your program. Copy and paste the program code into part 1(d) in the evidence document.[6]
  5. (e)The values stored in the stack are used to perform mathematical operations. A total is initialised to the first value in the stack. The first value in the stack will always be a number. The next value will be a mathematical operator and the next value will be the number to apply to the calculation. This is repeated until there are no values left in the stack. There will always be a number following a mathematical operator. For example, the contents of a stack are: 10 + 3 TopOfStack 2 The total is initialised to the first value in the stack: 10 total = 10 The next two values are: + 3 total = 10 + 3 = 13 The next two values are: - 2 total = 13- 2 = 11 The final total is 11 and this is returned. Write program code for the function Calculate() to: • take each value from the stack • calculate and return the final total. Save your program. Copy and paste the program code into part 1(e) in the evidence document.[7]
  6. (f)(i)Write program code to extend the main program to: • take a filename as input from the user • call ReadData() with the input filename • call Calculate() • output the final total. Save your program. Copy and paste the program code into part 1(f)(i) in the evidence document.[2]
  7. (f)(ii)Test your program twice with the file names: StackData.txt SecondStack.txt Take a screenshot of the output(s). Save your program. Copy and paste the screenshot into part 1(f)(ii) in the evidence document.[2]

Mark scheme

Marking points from the official mark scheme (May/June 2025). The mark or mark type is shown in brackets.

  1. [1] (Global) Stack as 1D array (of strings) with 20 elements initialised to string "-1"
  2. [1] (Global) TopOfStack initialised to -1
  3. [1] Push function header (and end where appropriate) taking one (string) parameter
  4. [1] Checking if stack is full and returning integer -1
  5. [1] (Otherwise) Incrementing TopOfStack
  6. [1] Storing parameter in the incremented the stack at TopOfStack and returning integer 1
  7. [1] Function header (and end where appropriate) and returning a value in all cases.
  8. [1] Checking if stack is empty and returning string "-1"
  9. [1] (Otherwise) Decrementing TopOfStack
  10. [1] Returning element in stack at TopOfStack before TopOfStack is decremented
  11. [1] Procedure header (and end where appropriate) taking one (string) parameter
  12. [1] Opening the file with the filename parameter and closing the file in an appropriate place
  13. [1] Looping through to end of file and reading in each line
  14. [1] calling Push () once with each read in value ...
  15. [1] if any return value from Push () is integer -1 outputting "Stack full"
  16. [1] Exception handling for opening and reading from file with appropriate catch and output
  17. [1] Calculate() function header (and end where appropriate)
  18. [1] Looping until the stack is empty
  19. [1] Calling Pop () repeatedly within loop and storing/using return value
  20. [1] working out if return value from Pop () call is an operator or a number / alternating between operator and number
  21. [1] Select to determine if the operator is +, -, /, * or ^ and attempt the matching calculation
  22. [1] performing correct calculation using operator, number
  23. [1] updating total from previous loops and returning this final value
  24. [1] Taking a filename as input and calling ReadData() with input
  25. [1] Calling Calculate() and outputting the return value
  26. [1] 1 mark for screenshot showing input of StackData.txt and output of 131
  27. [1] 1 mark for screenshot showing input of SecondStack.txt and output of 320

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 →

About this question

Topic
Data types and structures, A-Level Computer Science (syllabus 9618). Structured question, 27 marks.
Source
A-Level Computer Science, May/June 2025 Paper 4 Variant 2, question 1.

Questions and mark schemes are from past examination papers and remain the copyright of the exam board. How Oracle Prep builds its content

Related Computer Science questions

© 2026 Oracle Prep