Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
86 views2 pages

A2 P4 Basic Test

Uploaded by

muntasir3m
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
86 views2 pages

A2 P4 Basic Test

Uploaded by

muntasir3m
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

Computer Science A2 Level: P4 Test 1

Name: Date: Marks: /50


1. The file StudentScores.txt contains a list of 50 integer values, each between 0 and 100,
representing student scores. You need to write a program that reads these scores, performs sorting,
searching, and calculates specific information about the data.
(a) Write program code to declare a global 1D array, ScoreArray, with space for 50 integer values.
Save your program as Question1_Scores.
Copy and paste the program code into part 1(a) in the evidence document.
[2]
(b) Write a procedure LoadScores() to read each score from StudentScores.txt and store it in
ScoreArray. Implement exception handling to manage cases where the file is missing or a value is
invalid.
Write program code for LoadScores().
Save your program.
Copy and paste the program code into part 1(b) in the evidence document.
[6]
(c) Write a function CountOccurrences() that prompts the user to enter a score to search for in the
array. Ensure the input is between 0 and 100. The function should return the number of times the
score appears in ScoreArray.
Write program code for CountOccurrences().
Save your program.
Copy and paste the program code into part 1(c) in the evidence document.
[7]
(d)
(i) Write program code in the main program to call LoadScores() and CountOccurrences(). The return
value from CountOccurrences() should be output with an appropriate message.
Save your program.
Copy and paste the program code into part 1(d)(i) in the evidence document.
[3]
(ii) Test your program using the score 75 as input.
Take a screenshot to show the output.
Copy and paste the screenshot into part 1(d)(ii) in the evidence document.
[1]
(e) Write a procedure SortScores() to perform a bubble sort on ScoreArray and print the sorted
contents. Call this procedure from the main program.
Write program code for SortScores().
Save your program.
Copy and paste the program code into part 1(e) in the evidence document.
[6]
2 A file called ProductPrices.txt contains 30 floating-point numbers representing product prices
in dollars. Your task is to create a program that reads the prices, performs sorting, and allows the
user to search for specific values within the list.
(a) Write program code to declare a global 1D array, PriceArray, with space for 30 float values.
Save your program as Question2_Prices.
Copy and paste the program code into part 2(a) in the evidence document.
[2]
(b) Write a procedure ReadPrices() to read each price from ProductPrices.txt and store it in
PriceArray. Use appropriate exception handling to handle cases where the file is not found or a
value is not a valid float.
Write program code for ReadPrices().
Save your program.
Copy and paste the program code into part 2(b) in the evidence document.
[6]
(c) Write a function BinarySearchPrice() that asks the user to input a price to search for in the
array. Ensure the input is a valid floating-point number. The function should perform a binary search
to check if the price is in the sorted PriceArray and return the index or -1 if not found.
Write program code for BinarySearchPrice().
Save your program.
Copy and paste the program code into part 2(c) in the evidence document.
[7]
(d)
(i) In the main program, call ReadPrices() and BinarySearchPrice(). If the price is found,
display the index with a suitable message; otherwise, display "Price not found."
Save your program.
Copy and paste the program code into part 2(d)(i) in the evidence document.
[3]
(ii) Test your program with the price $49.99 as input.
Take a screenshot to show the output.
Copy and paste the screenshot into part 2(d)(ii) in the evidence document.
[1]
(e) Write a procedure InsertionSortPrices() to sort PriceArray using an insertion sort
algorithm and print the sorted prices. Call this procedure from the main program.
Write program code for InsertionSortPrices().
Save your program.
Copy and paste the program code into part 2(e) in the evidence document.
[6]

You might also like