Question 127 marks
Question
A program needs to take integer numbers as input, sort the numbers and then search for a specific number. (a) The integer numbers will be stored in the global 1D array, DataStored, with space for up to 20 integers. The global variable NumberItems stores the quantity of items the array contains. Write program code to declare DataStored and NumberItems. Save your program as Question1_J24. Copy and paste the program code into part 1(a) in the evidence document. [1] (b) The procedure Initialise(): • prompts the user to input the quantity of numbers the user would like to enter • reads the input and validates it is between 1 and 20 (inclusive) • prompts the user to input each number and stores each number in DataStored. Write program code for Initialise(). Save your program. Copy and paste the program code into part 1(b) in the evidence document. [5] (c) The main program stores 0 in NumberItems, calls Initialise() and then outputs the contents of DataStored. (i) Write program code for the main program. Save your program. Copy and paste the program code into part 1(c)(i) in the evidence document. [2] (ii) Test your program by inputting the following data in the order given: 30 5 3 9 4 1 2 Take a screenshot of the output(s). Save your program. Copy and paste the screenshot into part 1(c)(ii) in the evidence document. [2] (d) The procedure BubbleSort() uses a bubble sort to sort the data in DataStored into ascending numerical order. (i) Write program code for BubbleSort(). Save your program. Copy and paste the program code into part 1(d)(i) in the evidence document. [4] (ii) Write program code to amend the main program to call BubbleSort() and then output the contents of DataStored. Save your program. Copy and paste the program code into part 1(d)(ii) in the evidence document. [1] (iii) Test your program by inputting the following data in the order given: 5 3 9 4 1 2 Take a screenshot of the output(s). Save your program. Copy and paste the screenshot into part 1(d)(iii) in the evidence document. [1] (e) The function BinarySearch(): • takes the integer parameter DataToFind to search for in the array • performs an iterative binary search on the array DataStored • returns the index where DataToFind is found in DataStored. If DataToFind is not found, the function returns −1. (i) Write program code for the iterative function BinarySearch(). Save your program. Copy and paste the program code into part 1(e)(i) in the evidence document. [6] (ii) Write program code to amend the main program to: • take a number as input from the user • call BinarySearch() with the number input • output the value returned from the function call as its parameter. Save your program. Copy and paste the program code into part 1(e)(ii) in the evidence document. [3] (iii) Test your program twice with the following inputs: Test 1: 5 1 6 2 8 10 2 Test 2: 5 1 6 2 8 10 7 Take a screenshot of the output(s). Save your program. Copy and paste the screenshot into part 1(e)(iii) in the evidence document. [2]
Mark scheme
1(a) 1 mark for: 1 Declaration of (global) array with identifier DataStored (Integer and 20 spaces) and NumberItems (Integer) e.g. Java public static Integer[] DataStored = new Integer[20]; public static Integer NumberItems= 0; VB.NET Dim DataStored(19) As Integer Dim NumberStored As Integer = 0 Python global DataStored #integer global NumberItems #Integer 20 items 1(b) 1 mark each 5 Procedure heading (and close where appropriate) with no parameter. Prompt/output of suitable message to request the input of the quantity of numbers and reading in quantity of numbers and storing/using … … each input in next space in DataStored e.g. Java public static void Initialise(){ Scanner scanner = new Scanner(System.in); Integer Quantity = 0; do{ System.out.println("How many numbers will you enter up to 20?"); Quantity = Integer.parseInt(scanner.nextLine()); }while(Quantity <= 0 || Quantity > 20); for(Integer X = 0; X < Quantity; X++){ System.out.println("Enter number"); DataStored[NumberItems] = Integer.parseInt(scanner.nextLine()); NumberItems++; VB.NET Sub Initialise() Console.WriteLine("How many numbers will you enter?") Dim Quantity As Integer Do Quantity = Console.ReadLine() Loop Until (Quantity > 0 And Quantity < 21) For Count = 0 To Quantity - 1 Console.WriteLine("Enter number") DataStored(NumberStored) = Console.ReadLine() NumberStored += 1 Next End Sub 1(b) Python def Initialise(): global DataStored global NumberItems Valid = False while(Valid == False): NumberItems = int(input("How many numbers will you enter?")) #loop until < 20 if NumberItems > 0 and NumberItems< 21: Valid = True for Count in range(0, NumberItems): DataStored.append(int(input("Enter number"))) 1(c)(i) 1 mark each: 2 Storing 0 in NumberItems and then calling Initialise() Outputting all contents of array DataStored e.g. Java public static Integer NumberItems= 0; Initialise(); for(Integer X = 0; X < NumberItems; X++){ System.out.println(DataStored[X]); VB.NET NumberItems = 0 Initialise() For X = 0 To NumberItems - 1 Console.WriteLine(DataStored(X)) Next Python NumberItems = 0 Initialise() print(DataStored) 1(c)(ii) 1 mark each 2 Output showing quantity entered twice (30 and 5) with first being invalid Array output 3 9 4 1 2 e.g. 1(d)(i) 1 mark each 4 Procedure header (and end where appropriate) and looping through each array element Working inner loop … …comparison of elements… …swapping of elements e.g. Java public static void BubbleSort(){ Integer Temp = 0; for(Integer Count = 0; Count < NumberItems; Count++){ for(Integer Count2 = 0; Count2 < NumberItems - 1; Count2++){ if(DataStored[Count2] > DataStored[Count]){ Temp = DataStored[Count2]; DataStored[Count2] = DataStored[Count]; DataStored[Count] = Temp; } } } } VB.NET Sub BubbleSort() Dim Temp As Integer For Count = 0 To NumberStored - 1 For Count2 = 0 To NumberStored - 2 If (DataStored(Count2) > DataStored(Count)) Then Temp = DataStored(Count) DataStored(Count) = DataStored(Count2) DataStored(Count2) = Temp End If Next Next End Sub 1(d)(i) Python def BubbleSort(): global DataStored global NumberItems for Count in range(0, NumberItems): for Count2 in range(0, NumberItems-1): if DataStored[Count2] > DataStored[Count]: DataStored[Count2], DataStored[Count] = DataStored[Count], DataStored[Count2] 1(d)(ii) 1 mark for calling BubbleSort() and outputting array contents after 1 e.g. VB.NET BubbleSort() For X = 0 To NumberStored - 1 Console.WriteLine(DataStored(X)) Next e.g. Java BubbleSort(); for(Integer X = 0; X < NumberItems; X++){ System.out.println(DataStored[X]); } e.g. Python BubbleSort() print(DataStored) 1(d)(iii) 1 mark for screenshot showing the inputs and the values in the correct order 1 e.g. 1(e)(i) 1 mark each 6 Function header BinarySearch taking DataToFind as a parameter Calculating the mid value (First + Last) \ 2 or equivalent inside loop Checking if the data at mid is the parameter and returning mid inside loop If DataToFind < mid, updating Last/Upper with mid – 1 inside loop If DataToFind > mid, updating First/Lower with mid + 1 inside loop Returning -1 when not found and a suitable loop with end criteria e.g. Java public static Integer BinarySearch(Integer DataToFind){ Integer MidValue = 0; Integer First = 0; Integer Last = NumberItems; while (First <= Last){ MidValue = (First + Last) / 2; if(DataToFind == DataStored[MidValue]){ return MidValue; } if(DataToFind < DataStored[MidValue]){ Last = MidValue - 1; }else{ First = MidValue + 1; } } return -1; } 1(e)(i) VB.NET Function BinarySearch(DataToFind) Dim First As Integer = 0 Dim Last As Integer = NumberItems Dim MidValue As Integer While (First <= Last) MidValue = (First + Last) / 2 If DataToFind = DataStored(MidValue) Then Return MidValue End If If DataToFind < DataStored(MidValue) Then Last = MidValue - 1 Else First = MidValue + 1 End If End While Return -1 End Function Python def BinarySearch(DataToFind): global DataStored global NumberItems First = 0 Last= NumberItems while(First <= Last): MidValue = int((First + Last) / 2) if DataToFind == DataStored[MidValue]: return MidValue if DataToFind < DataStored[MidValue]: Last = MidValue - 1 else: First = MidValue + 1 return -1 1(e)(ii) 1 mark each: 3 Taking number as input … calling BinarySearch with input Outputting value returned e.g. Java Scanner scanner = new Scanner(System.in); System.out.println("Enter a number to find"); Integer Search = Integer.parseInt(scanner.nextLine()); System.out.println(BinarySearch(Search)); VB.NET Console.WriteLine("Enter a number to find") Dim Search As Integer = Console.ReadLine() Console.WriteLine(BinarySearch(Search)) Python Search = int(input("Enter a number to find")) print(BinarySearch(Search)) 1(e)(iii) 1 mark for each test 2 e.g. Test 1 – Accept found in index 16 Test 2