File Processing and Exception Handling
41 questions· page 1 of 5
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()
Save your program as Question2_J25.
Copy and paste the program code into part 2(a) in the evidence document.
The procedure StoreData():
- takes two parameters: a 1D array
DataToStoreand a filename - opens the text file with the filename that is passed as a parameter
- appends each item of data from
DataToStoreto a new line in the text file - uses exception handling when opening and writing data to the text file.
Write program code for StoreData()
Save your program.
Copy and paste the program code into part 2(c) in the evidence document.
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.txtGreen.txtOrange.txtPink.txtRed.txtYellow.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()
Save your program.
Copy and paste the program code into part 2(d) in the evidence document.
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.
Save your program.
Copy and paste the screenshot(s) into part 2(e)(ii) in the evidence document.
The procedure ReadFile() must read in the numbers from the text file and store each one in the array. Use appropriate exception handling.
Write program code for the procedure ReadFile().
Save your program.
Copy and paste the program code into part 1(b) in the evidence document.
The function FindValues() asks the user to enter a number to search for in the array. The number input must be a whole number between 1 and 100 inclusive. The function then returns the number of times the number input appears in the array.
Write program code for the function FindValues().
Save your program.
Copy and paste the program code into part 1(c) in the evidence document.
The procedure ReadFile() must read in the numbers from the text file and store each one in the array. Use appropriate exception handling.
Write program code for the procedure ReadFile().
Save your program.
Copy and paste the program code into part 1(b) in the evidence document.
The function FindValues() asks the user to enter a number to search for in the array. The number input must be a whole number between 1 and 100 inclusive. The function then returns the number of times the number input appears in the array.
Write program code for the function FindValues().
Save your program.
Copy and paste the program code into part 1(c) in the evidence document.
The text file CardValues.txt stores the data for 30 cards, in the order: number, colour.
For example, the first card in the text file:
1 is the number
red is the colour.
A 1D array of type Card is declared to store all the cards read in from CardValues.txt
Write the main program to:
- declare an array of type
Cardwith 30 elements - read in the data for the 30 cards from
CardValues.txtand assign each to the array.
Save your program.
Copy and paste the program code into part 3(c) in the evidence document.
The program needs to allow all players (maximum of 5) to select 4 cards from the 30 available. A card can only be selected once, so the program needs to record which cards have already been selected.
The function, ChooseCard():
- takes as input an integer to represent an array index from 1 to 30
- validates that the value is between 1 and 30 inclusive
- checks if the card is available (it has not already been selected)
- loops until an available card is selected
- returns the index of the card if it is available.
Amend the program to store which cards have already been selected and write program code for the function ChooseCard().
Save your program.
Copy and paste the program code into part 3(d) in the evidence document.
Test your program with the following test data:
Test 1: 1 5 9 10
Test 2: 2 2 3 4 4 5
Take a screenshot to show the output.
Copy and paste the screenshot into part 3(e)(ii) in the evidence document.
Amend the main program to read the contents of Data.txt into DataArray
Save your program.
Copy and paste the program code into part 1(a)(ii) in the evidence document.
Amend the main program to:
- prompt the user to input a whole number between 0 and 100 inclusive
- read and validate the input from the user
- call
LinearSearch()withDataArrayand the validated input value - output the result in the format:
The number 7 is found 2 times.
Save your program.
Copy and paste the program code into part 1(d)(i) in the evidence document.
Test your program by inputting the number 12.
Take a screenshot of the output.
Save your program.
Copy and paste the screenshot into part 1(d)(ii) in the evidence document.
Amend the main program to read the contents of Data.txt into DataArray
Save your program.
Copy and paste the program code into part 1(a)(ii) in the evidence document.
Amend the main program to:
- prompt the user to input a whole number between 0 and 100 inclusive
- read and validate the input from the user
- call
LinearSearch()withDataArrayand the validated input value - output the result in the format:
The number 7 is found 2 times.
Save your program.
Copy and paste the program code into part 1(d)(i) in the evidence document.
Test your program by inputting the number 12.
Take a screenshot of the output.
Save your program.
Copy and paste the screenshot into part 1(d)(ii) in the evidence document.
The procedure ReadHighScores() opens the file HighScore.txt and reads the data into the data structure(s) declared in part 1(a).
Write program code to declare the procedure ReadHighScores().
Save your program.
Copy and paste the program code into part 1(b) in the evidence document.
Test your program.
Take a screenshot to show the output from part 1(d)(i).
Copy and paste the screenshot into part 1(d)(ii) in the evidence document.
Amend the main program to ask the user to input a 3-character player name and an integer score that must be between 1 and 100 000 inclusive.
Save your program.
Copy and paste the program code into part 1(e)(i) in the evidence document.
The procedure WriteTopTen() stores the new top ten player names and scores in a text file called NewHighScore.txt
Write program code to declare the procedure WriteTopTen().
Save your program.
Copy and paste the program code into part 1(f) in the evidence document.
The text file TreeData.txt stores 50 integers. Each integer is on a new line in the file.
The main program reads each integer from the file and stores each integer in the binary tree in the order they are read.
Write program code for the main program.
Save your program.
Copy and paste the program code into part 3(c) in the evidence document.
The procedure WriteAllToFile() stores the content of TreeArray in a new text file with the filename Tree.txt. The file Tree.txt is not provided.
Each node in TreeArray is stored on one line with a comma separating each value.
For example, the current contents of TreeArray are:
| Index | 0 | 1 | 2 |
|---|---|---|---|
| 0 | -1 | 20 | 1 |
| 1 | -1 | 30 | -1 |
| ... | |||
| 49 | -1 | -1 | -1 |
After writing TreeArray to the text file, Tree.txt will contain:
-1,20,1
-1,30,-1
...
-1,-1,-1
Write program code for WriteAllToFile()
Include exception handling when writing to the file.
Save your program.
Copy and paste the program code into part 3(d) in the evidence document.
Test your program.
Take a screenshot that shows all of the content stored in the file Tree.txt
In this screenshot you need to make sure the filename is visible.
Save your program.
Copy and paste the screenshot(s) into part 3(e)(ii) in the evidence document.
The subroutine StoreItems() takes ten 7-character strings as input from the user and uses the check digit to validate each input.
Each valid input has the check digit removed and is stored in the queue using Enqueue().
An appropriate message is output if the item is inserted. An appropriate message is output if the queue is already full.
Invalid inputs are not stored in the queue.
The subroutine counts and outputs the number of invalid items that were entered.
StoreItems() can be a procedure or a function as appropriate.
Write program code for StoreItems().
Save your program.
Copy and paste the program code into part 3(d)(i) in the evidence document.
Test the program with the following inputs in the order given:
999999X
1251484
5500212
0033585
9845788
6666666
3258746
8111022
7568557
0012353
Take a screenshot of the output(s).
Save your program.
Copy and paste the screenshot into part 3(d)(iii) in the evidence document.
The main program has a 1D array of characters. Each character is stored as an object of type Character.
The game has a maximum of 10 characters. The character names, x coordinates and y coordinates are stored in the file Characters.txt in the order:
- name
- x coordinate
- y coordinate.
For example, the first character in the file is named Amal, with the x coordinate 0 and the y coordinate 2.
Amend the main program by writing program code to:
- declare the array
- read in all 10 characters from
Characters.txt - store each character as an object in the array.
Save your program.
Copy and paste the program code into part 2(d) in the evidence document.
The user will enter a letter to identify the direction the chosen character from part 2(e) should move.
- If ‘A’ is input, the character moves left (x coordinate minus 1).
- If ‘W’ is input, the character moves up (y coordinate plus 1).
- If ‘S’ is input, the character moves down (y coordinate minus 1).
- If ‘D’ is input, the character moves right (x coordinate plus 1).
Amend the main program by writing program code to:
- take a letter as input until it is a valid move (A, W, S or D)
- change the position of the character using the appropriate method.
Save your program.
Copy and paste the program code into part 2(f) in the evidence document.