A program sorts string data using different sorting methods. One source file is used to answer Question 1. The file is called Data.txt
- (a)The text file Data.txt stores string data items. Each data item is on a new line in the text file. The function ReadData(): • has a local array of strings that can store 45 items • reads each line of data and stores it in the array • returns the array. Write program code for the function ReadData(). Save your program as Question1_N24. Copy and paste the program code into part 1(a) in the evidence document.[6]
- (b)(i)The function FormatArray() takes an array of strings as a parameter. It concatenates the contents of the array into one string with a space between each array element. The function returns the concatenated string. Write program code for FormatArray(). Save your program. Copy and paste the program code into part 1(b)(i) in the evidence document.[2]
- (b)(ii)The main program: • calls ReadData() and stores the returned array • calls FormatArray() with the returned array and outputs the returned string. Write program code for the main program. Save your program. Copy and paste the program code into part 1(b)(ii) in the evidence document.[3]
- (b)(iii)Test your program. Take a screenshot of the output. Save your program. Copy and paste the screenshot into part 1(b)(iii) in the evidence document.[1]
- (c)The function CompareStrings(): : takes two strings as parameters • compares each string, one character at a time, to identify which string comes first alphabetically. If the first two characters are the same, the second character of each string is compared. This continues until the two characters are different. The function: • returns 1 if the first parameter comes before the second alphabetically • returns 2 if the second parameter comes before the first alphabetically. Write program code for CompareStrings(). Assume that all strings are in lower case. Assume that a difference between two strings will always be identified before the end of one string is reached. Do not use an in-built string comparison function. The strings must be compared one character at a time. Save your program. Copy and paste the program code into part 1(c) in the evidence document.[4]
- (d)(i)The function Bubble() takes an array of strings as a parameter and sorts the data into ascending alphabetical order, using a bubble sort. The bubble sort uses CompareStrings() to compare each string. The function returns the sorted list. Write program code for Bubble(). Save your program. Copy and paste the program code into part 1(d)(i) in the evidence document.[3]
- (d)(ii)Write program code to amend the main program to: • call Bubble() with the unsorted array as a parameter • call FormatArray() with the sorted array and output the returned string. Save your program. Copy and paste the program code into part 1(d)(ii) in the evidence document.[2]
- (d)(iii)Test your program. Take a screenshot of the output. Save your program. Copy and paste the screenshot into part 1(d)(iii) in the evidence document.[1]
Mark scheme
Marking points from the official mark scheme (Oct/Nov 2024). The mark or mark type is shown in brackets.
- [6] 1 mark each to max 6: Function declaration (and close where appropriate)
- [6] Declaration/use of an array (with space/initialised with 45 spaces/strings)
- [6] Opening the file Data.txt for read and closing in an appropriate place
- [6] Looping through all file contents/Looping 45 times and reading each line ...
- [6] ... storing all items from file into array
- [6] Returning the populated array
- [6] Exception handling with suitable try, catch and output
- [2] 1 mark each: Function header (and end where appropriate) taking (min) one parameter
- [2] Looping through each parameter array element, concatenating with space and returning
- [3] 1 mark each: Calling ReadData() and storing returned array
- [3] ... calling FormatArray() with returned array
- [3] Outputting return value from FormatArray()
- [1] 1 mark for output showing all colours in one string
- [4] 1 mark each: Function header (and close where appropriate) taking (min) two parameters and returns a value in all cases
- [4] Looping through each character in each string parameter ...
- [4] ... return 1 when first parameter < second
- [4] ... return 2 when first parameter > second
- [3] 1 mark each: Bubble sort function header taking array parameter and returns sorted array in all cases
- [3] Comparing strings using CompareStrings() and correctly swapping values when needed
- [3] Correct bubble sort that sorts the data correctly
- [2] 1 mark each: Calling Bubble () with array as parameter and using/storing return value
- [2] Calling FormatArray() with return value from Bubble () and outputting return value
- [1] 1 mark for sorted data
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 →