A program reads data from a text file, splits the data depending on its content and stores the separated data into different files. The text file TheData.txt contains 72 lines of data. Each line of data has an integer number and a string colour that are separated by a comma. For example, the first line in the file is: 10,red The integer is 10 and the string is "red" The file contains six different colours: red, green, blue, orange, yellow, pink.
- (a)The function ReadData(): • prompts the user to enter a filename and reads this filename from the user • opens the file and reads each line of data into a 1D array • returns the populated 1D array. The function needs to work for a file that contains an unknown number of lines. Write program code for ReadData()[7]
- (b)The procedure SplitData() takes a 1D string array as a parameter with the identifier DataArray The procedure declares six 1D arrays: one array for each colour that appears in the file (red, green, blue, orange, yellow, pink). The procedure accesses each string in DataArray. The data in each string is split into the integer and the colour. The integer is stored in the array that matches the colour. For example, the first string in DataArray has the integer 10 and the colour red, so the integer 10 is stored in the array for the colour red. Write program code for SplitData()[6]
- (c)The procedure StoreData(): • takes two parameters: a 1D array DataToStore and a filename • opens the text file with the filename that is passed as a parameter • appends each item of data from DataToStore to a new line in the text file • uses exception handling when opening and writing data to the text file. Write program code for StoreData()[5]
- (d)Each of the six colours has a blank text file where the numbers will be stored. The names of these six text files are: • Blue.txt • Green.txt • Orange.txt • Pink.txt • Red.txt • Yellow.txt The procedure SplitData() needs amending to call StoreData() six times, with each of the six colour arrays and the name of the text file that corresponds to that colour. For example, StoreData() will be called with the red array and the file name "Red.txt" Write program code to amend SplitData()[2]
- (e)(i)The main program calls ReadData() and then SplitData() Write program code for the main program.[3]
- (e)(ii)Test your program. Input the text "TheData.txt" when prompted. Take a screenshot of the output(s) and a screenshot showing the content of the file that stores the red numbers. Copy and paste the screenshot(s) into part 2(e)(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.
- [7] 1 mark each to max 7
- [7] • Function header (and end)
- [7] • Prompt to enter filename and reading input
- [7] • Opening the file (to read) and closing the file in an appropriate place
- [7] • Looping until EOF ...
- [7] • ... reading each line in the file ...
- [7] • (removing line break and) inserting in array
- [7] • Returning populated array
- [7] • Exception handling try catch with appropriate output
- [6] 1 mark each
- [6] • Procedure header (and end) taking (1D array) DataArray (of strings) as a parameter
- [6] • Declaration/use of 6 1D arrays (equivalent), one for each colour
- [6] • Looping through each line in parameter DataArray ...
- [6] • splitting by comma
- [6] • Comparing 2nd value/colour to each colour to select array ...
- [6] • storing 1st value/integer in correct array
- [5] 1 mark each
- [5] • Procedure header taking (1D) array and filename as parameters, opening file to append and closing file (in appropriate place)
- [5] • Looping through each item in array parameter ...
- [5] • writing to the file
- [5] • with new line break between each line
- [5] • Using exception handling try and catch with suitable output
- [2] 1 mark each
- [2] • Calling StoreData with one array and filename
- [2] • Calling StoreData with remaining 5 arrays and filename
- [3] 1 mark each
- [3] • Calling ReadData() ...
- [3] • and storing/using return value
- [3] • Calling SplitData() with returned array as a parameter
- [2] 1 mark screenshots showing
- [2] • Prompt and input of filename TheData.txt
- [2] • Screenshot of data in red file. Filename must be shown in same screenshot as 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 →