Question 120 marks
Question
A program stores integers in a stack. The stack is represented as a 1D array of 30 elements with the identifier Stack The global pointer TopOfStack stores the index of the last element inserted into the stack. TopOfStack is initialised to –1 (a) Write program code to declare Stack, initialise each element in the array with a null value and declare and initialise TopOfStack Save your program as Question1_N25. Copy and paste the program code into part 1(a) in the evidence document. [2] (b) The function Push() takes an integer parameter. If the stack is full, the function returns FALSE. If the stack is not full, the parameter is inserted into the stack, the pointer is updated and the function returns TRUE Write the program code for Push() Save your program. Copy and paste the program code into part 1(b) in the evidence document. [4] (c) The function Pop() returns the next integer in the stack and updates the pointer as appropriate. If there is no data in the stack, the function returns the value –999 Write program code for Pop() Save your program. Copy and paste the program code into part 1(c) in the evidence document. [4] (d) The main program generates 40 random integers between 0 and 1000 (inclusive) and attempts to insert each one into the stack using the appropriate function. If the return value from the function call indicates the stack is full, no more integers are generated and "Stack full" is output. Write program code for the main program. Save your program. Copy and paste the program code into part 1(d) in the evidence document. [4] (e) The procedure FindValues(): • pops each integer from the stack until the stack is empty • finds and outputs the largest number that was in the stack in an appropriate message • finds and outputs the smallest number that was in the stack in an appropriate message. Write program code for FindValues() Save your program. Copy and paste the program code into part 1(e) in the evidence document. [4] (f) (i) Extend the main program to call FindValues() Save your program. Copy and paste the program code into part 1(f)(i) in the evidence document. [1] (ii) Test your program. Take a screenshot of the output(s). Save your program. Copy and paste the screenshot(s) into part 1(f)(ii) in the evidence document. [1]
Mark scheme
1(a) 1 mark each 2 • (Global) 1D array initialised with 30 null values • (Global) TopofStack initialised with –1 Example program code Java public static Integer[] Stack = new Integer[30]; public static Integer TopOfStack; public static void main(String args[]){ for(Integer X = 0; X < 30; X++){ Stack[X] = null; } TopOfStack = -1; } VB.NET Dim Stack(29) As Integer Dim TopOfStack As Integer Sub Main(args As String()) For x = 0 To 29 Stack(x) = Nothing Next TopOfStack = -1 End Sub Python Stack = [None for x in range(30)] TopOfStack = -1 1(b) 1 mark each 4 • Function header (and end) taking one parameter, returning Boolean in all instances • Checking if stack is full (TopOfStack = 29) and, if it is, returning FALSE • Incrementing TopofStack • (Otherwise) Storing parameter in incremented TopOfStack position and returning TRUE Example program code Java public static Boolean Push(Integer DataToPush){ if(TopOfStack < 29){ TopOfStack++; Stack[TopOfStack] = DataToPush; return true; } return false; } VB.NET Function Push(DataToPush) If TopOfStack < 29 Then TopOfStack = TopOfStack + 1 Stack(TopOfStack) = DataToPush Return True End If Return False End Function 1(b) Python def Push(DataToPush): global Stack global TopOfStack if TopOfStack < 29: TopOfStack = TopOfStack + 1 Stack[TopOfStack] = DataToPush return True else: return False 1(c) 1 mark each 4 • Function header (and end) returning integer in all cases • Checking if stack empty (TopofStack = –1) and returning -999 when true • (Otherwise) Accessing and returning item at TopOfStack (before it's decremented) • Decrementing TopofStack Example program code Java public static Integer Pop(){ Integer DataReturn; if(TopOfStack == -1){ return -999; } DataReturn = Stack[TopOfStack]; TopOfStack--; return DataReturn; } VB.NET Function Pop() If TopOfStack = -1 Then Return -999 Else Dim DataReturn As Integer = Stack(TopOfStack) TopOfStack = TopOfStack - 1 Return DataReturn End If End Function 1(c) Python def Pop(): global Stack global TopOfStack if TopOfStack == -1: return -999 else: DataReturn = Stack[TopOfStack] TopOfStack = TopOfStack - 1 return DataReturn 1(d) 1 mark each 4 • Looping 40 times • Generating random number between 0 and 1000 inclusive inside the loop • Calling Push() with each random number and storing/using return value … … if return value is FALSE output Stack full and breaking out of loop Example program code Java for(Integer X = 0; X < 40; X++){ Pushed = Push(RandomNumber.nextInt(1001)); if(Pushed == false){ System.out.println("Stack full"); X = 40; } } VB.NET For x = 0 To 39 Pushed = Push(RandomNumber.Next(0, 1000)) If Pushed = False Then Console.WriteLine("Stack full") x = 40 End If Next Python for x in range(40): Pushed = Push(random.randint(0,1000)) if Pushed == False: print("Stack full") break 1(e) 1 mark each 4 • Procedure header (and end) and output of highest and lowest include appropriate messages • Calls Pop() until there are no items left in stack (return value = –999 // TopOfStack = -1) and storing/using return values • Finds and outputs highest value from returned values • Finds and outputs lowest value from returned values Example program code Java public static void FindValues(){ Integer Highest; Integer Lowest; Highest = Pop(); Lowest = Highest; Integer ReturnValue = Highest; while(ReturnValue != -999){ if(ReturnValue > Highest){ Highest = ReturnValue; } if(ReturnValue < Lowest){ Lowest = ReturnValue; } ReturnValue = Pop(); } System.out.println("The highest value is " + Highest + " and the lowest value is " + Lowest); } 1(e) VB.NET Sub FindValues() Dim Highest, Lowest As Integer Highest = Pop() Lowest = Highest Dim ReturnValue As Integer = Highest While ReturnValue <> -999 If ReturnValue > Highest Then Highest = ReturnValue End If If ReturnValue < Lowest Then Lowest = ReturnValue End If ReturnValue = Pop() End While Console.WriteLine("The highest value is " & Highest & " and the lowest value is " & Lowest) End Sub Python def FindValues(): Highest = Pop() Lowest = Highest ReturnValue = Lowest while(ReturnValue != -999): if ReturnValue > Highest: Highest = ReturnValue if ReturnValue < Lowest: Lowest = ReturnValue ReturnValue = Pop() print("The highest value is", Highest, "and the lowest value is", Lowest) 1(f)(i) 1 mark for calling FindValues() 1 Example program code Java FindValues(); VB.NET FindValues() Python FindValues() 1(f)(ii) 1 mark for a screenshot of output showing 1 Stack full output once Lowest value output in an appropriate message Highest value output in an appropriate message Lowest and Highest must be 0–1000 inclusive