Question 127 marks
Question
A program stores lower-case letters in two stacks. One stack stores vowels (a, e, i, o, u) and one stack stores consonants (letters that are not vowels). Each stack is implemented as a 1D array. (a) (i) Write program code to declare two 1D global arrays: StackVowel and StackConsonant. Each array needs to store up to 100 letters. The index of the first element in each array is 0. If you are writing in Python, include declarations using comments. Save your program as Question1_N23. Copy and paste the program code into part 1(a)(i) in the evidence document. [2] (ii) The global variable VowelTop is a pointer that stores the index of the next free space in StackVowel. The global variable ConsonantTop is a pointer that stores the index of the next free space in StackConsonant. VowelTop and ConsonantTop are both initialised to 0. Write program code to declare and initialise the two variables. If you are writing in Python, include declarations using comments. Save your program. Copy and paste the program code into part 1(a)(ii) in the evidence document. [1] (b) (i) The procedure PushData() takes one letter as a parameter. If the parameter is a vowel, it is pushed onto StackVowel and the relevant pointer updated. If the stack is full, a suitable message is output. If the parameter is a consonant, it is pushed onto StackConsonant and the relevant pointer updated. If the stack is full, a suitable message is output. You do not need to validate that the parameter is a letter. Write program code for PushData(). Save your program. Copy and paste the program code into part 1(b)(i) in the evidence document. [6] (ii) The file StackData.txt stores 100 lower-case letters. The procedure ReadData() reads each letter from the file and uses PushData() to push each letter onto its appropriate stack. Use appropriate exception handling if the file does not exist. Write program code for ReadData(). Save your program. Copy and paste the program code into part 1(b)(ii) in the evidence document. [6] (c) The function PopVowel() removes and returns the data at the top of StackVowel and updates the relevant pointer(s). The function PopConsonant() removes and returns the data from the top of StackConsonant and updates the relevant pointer(s). If either stack is empty, the string "No data" must be returned. Write program code to declare PopVowel() and PopConsonant(). Save your program. Copy and paste the program code into part 1(c) in the evidence document. [5] (d) The program first needs to call ReadData() and then: 1. prompt the user to input their choice of vowel or consonant 2. take, as input, the user’s choice 3. depending on the user’s choice, call PopVowel() or PopConsonant() and store the return value. The three steps are repeated until 5 letters have been successfully returned and stored. If either stack is empty at any stage, an appropriate message must be output. Once 5 letters have been successfully returned and stored, they are output on one line, for example: abyti (i) Write program code for the main program. Save your program. Copy and paste the program code into part 1(d)(i) in the evidence document. [6] (ii) Test your program with the following inputs: vowel consonant consonant vowel vowel Take a screenshot of the output. Save your program. Copy and paste the screenshot into part 1(d)(ii) in the evidence document. [1]
Mark scheme
1(a)(i) One mark each 2 • Two arrays with correct identifiers of type string/character • Each has 100 elements Example program code: Java public static String[] StackVowel = new String[100]; public static String[] StackConsonant = new String[100]; VB.NET Dim StackVowel(0 To 99) As Char Dim StackConsonant(0 To 99) As Char Python StackVowel = [] #string 100 StackConsonant = [] #string 100 1(a)(ii) One mark for 1 • Declaring both variables as type integer global and initialised to 0 Example program code: Java public static Integer VowelTop = 0; public static Integer ConsonantTop = 0; VB.NET Dim VowelTop As Integer = 0 Dim ConsonantTop As Integer = 0 Python global VowelTop #integer global ConsonantTop #integer #main VowelTop = 0 ConsonantTop = 0 1(b)(i) One mark each 6 • Procedure PushData() heading (and end where appropriate) taking one parameter • Checking if parameter is a (lowercase) vowel … • … checking if StackVowel is full and outputting suitable message • … otherwise inserting parameter in next position • … incrementing VowelTop • Repeated for Consonant Example program code: Java public static void PushData(String Letter){ if(Letter.equals("a") || Letter.equals("e") || Letter.equals("i") || Letter.equals("o") || Letter.equals("u")){ if(VowelTop == 100){ System.out.println("Vowel stack full"); }else{ StackVowel[VowelTop] = Letter; VowelTop++; } }else{ if(ConsonantTop == 100){ System.out.println("Consonant stack full"); }else{ StackConsonant[ConsonantTop] = Letter; ConsonantTop++; } } } 1(b)(i) VB.NET Sub PushData(Letter As Char) If Letter = "a" Or Letter = "e" Or Letter = "i" Or Letter = "o" Or Letter = "u" Then If VowelTop = 100 Then Console.WriteLine("Vowel stack full") Else StackVowel(VowelTop) = Letter VowelTop += 1 End If Else If ConsonantTop = 100 Then Console.WriteLine("Consonant stack full") Else StackConsonant(ConsonantTop) = Letter ConsonantTop += 1 End If End If End Sub Python def PushData(Letter): global VowelTop global ConsonantTop if Letter == "a" or Letter == "e" or Letter == "i" or Letter == "o" or Letter == "u": if VowelTop == 100: print("Vowel stack full") else: StackVowel.append(Letter) VowelTop = VowelTop + 1 else: if ConsonantTop == 100: print("Consonant stack full") else: StackConsonant.append(Letter) ConsonantTop = ConsonantTop + 1 1(b)(ii) One mark each 6 • Procedure header ReadData() with no parameter • Opening StackData.txt to read and closing file • Looping until EOF // Looping 100 times • Read each item from the file • Calling PushData() with each value as parameter • Appropriate exception handling with suitable output Example program code: Java private static void ReadData(){ try{ Scanner Scanner1 = new Scanner(new File("StackData.txt")); while(Scanner1.hasNextLine()){ PushData(Scanner1.next()); } Scanner1.close(); }catch(FileNotFoundException ex){ System.out.println("No file found"); } } VB.NET Sub ReadData() Try Dim DataReader As New System.IO.StreamReader("StackData.txt") Do Until DataReader.EndOfStream PushData(DataReader.ReadLine()) Loop DataReader.Close() 1(b)(ii) Catch ex As Exception Console.WriteLine("File not found") End Try End Sub Python def ReadData(): try: DataFile = open("StackData.txt") for Line in DataFile: PushData(Line.strip()) DataFile.close() except: print("File not found") 1(c) One mark each 5 • One function header with no parameter • Checking if stack is empty and returning "No data" • …otherwise, decrementing correct pointer • Returning value at top of stack • 2nd function fully correct Example program code: Java public static String PopVowel(){ String DataToReturn = ""; if(VowelTop - 1 >= 0){ VowelTop --; DataToReturn = StackVowel[VowelTop]; return DataToReturn; }else{ return "No data"; } } public static String PopConsonant(){ String DataToReturn = ""; if(ConsonantTop - 1 >= 0){ ConsonantTop--; DataToReturn = StackConsonant[ConsonantTop]; return DataToReturn; }else{ return "No data"; } } 1(c) VB.NET Function PopVowel() If VowelTop - 1 >= 0 Then VowelTop -= 1 Dim DataToReturn As Char = StackVowel(VowelTop) Return DataToReturn Else Return "No data" End If End Function Function PopConsonant() If ConsonantTop - 1 >= 0 Then ConsonantTop -= 1 Dim DataToReturn As Char = StackConsonant(ConsonantTop) Return DataToReturn Else Return "No data" End If End Function Python def PopVowel(): global VowelTop global ConsonantTop if VowelTop - 1 >= 0: VowelTop = VowelTop - 1 DataToReturn = StackVowel[VowelTop] del StackVowel[-1] return DataToReturn else: return "No data" 1(c) def PopConsonant(): global VowelTop global ConsonantTop if ConsonantTop - 1 >= 0: ConsonantTop = ConsonantTop - 1 DataToReturn = StackConsonant[ConsonantTop] del StackConsonant[-1] return DataToReturn else: return "No data" 1(d)(i) One mark each to max 6 6 • Calling ReadData() • Looping until 5 letters successfully accessed • Prompt and read in input of choice … • … if vowel is input calling PopVowel() and if consonant calling PopConsonant() … • … storing return values • Outputting appropriate message if no vowels and if no consonants (stacks full) within loop • Outputting the five returned letters on one line Example program code: Java public static void main(String args[]){ VowelTop = 0; ConsonantTop = 0; ReadData(); String Letters = ""; Boolean Flag = false; String Choice = ""; String DataAccessed = ""; for(Integer X = 0; X < 5; X++){ Flag = false; while(Flag == false){ System.out.println("Vowel or Consonant"); Scanner scanner = new Scanner(System.in); Choice = (scanner.nextLine()).toLowerCase(); if(Choice.equals("vowel")){ DataAccessed = PopVowel(); if(DataAccessed.equals("No data") == false){ Letters = Letters + DataAccessed; Flag = true; }else{ System.out.println("No vowels left"); } 1(d)(i) }else if(Choice.equals("consonant")){ DataAccessed = PopConsonant(); if(DataAccessed.equals("No data") == false){ Letters = Letters + DataAccessed; Flag = true; }else{ System.out.println("No consonants left"); } } } } System.out.println(Letters); } VB.NET Sub Main(args As String()) VowelTop = 0 ConsonantTop = 0 ReadData() Dim Letters As String = "" Dim Flag As Boolean = False Dim Choice As String Dim DataAccessed As String For x = 0 To 4 Flag = False While Flag = False Console.WriteLine("Vowel or Consonant?") Choice = Console.ReadLine().ToLower() If Choice = "vowel" Then DataAccessed = PopVowel() If DataAccessed <> "No data" Then Letters = Letters & DataAccessed Flag = True Else Console.WriteLine("No vowels left") 1(d)(i) End If ElseIf Choice = "consonant" Then DataAccessed = PopConsonant() If DataAccessed <> "No data" Then Letters = Letters & DataAccessed Flag = True Else Console.WriteLine("No consonants left") End If End If End While Next Console.WriteLine(Letters) End Sub Python #main VowelTop = 0 ConsonantTop = 0 ReadData() Letters = "" Flag = False for x in range(0, 5): Flag = False while Flag == False: Choice = input("Vowel or Consonant").lower() if Choice == "vowel": DataAccessed = PopVowel() if DataAccessed != "No data": Letters = Letters + DataAccessed Flag = True else: print("No vowels left") 1(d)(i) elif Choice == "consonant": DataAccessed = PopConsonant() if DataAccessed != "No data": Letters = Letters + DataAccessed Flag = True else: print("No consonants left") print(Letters) 1(d)(ii) One mark showing input in order vowel, cons, cons, vowel, vowel. Output is then utxoe 1 e.g.