解題
### Part (a) Solution
Parallel arrays are two or more arrays of the same size where elements at the same index are logically related to each other (i.e., they represent different attributes of the same entity, such as `IDS[i]`, `DESTINATIONS[i]`, and `WEIGHTS[i]` describing the same package).
A key disadvantage of using parallel arrays compared to an array of objects is data integrity maintenance during sorting, deletion, or insertion. If you need to reorder or sort the data (e.g., sorting packages by weight), every array must be reordered in the exact same sequence. If one array is modified without identical changes to the others, the data relationship is permanently lost/corrupted. An array of objects binds all properties together natively, avoiding this risk.
### Part (b) Pseudocode Solution
```
processPackages(targetDest, IDS, DESTINATIONS, WEIGHTS)
totalWeight = 0.0
maxWeight = -1.0
heaviestID = -1
underFiveCount = 0
found = false
loop I from 0 to 499
if DESTINATIONS[I] == targetDest then
found = true
totalWeight = totalWeight + WEIGHTS[I]
if WEIGHTS[I] > maxWeight then
maxWeight = WEIGHTS[I]
heaviestID = IDS[I]
end if
if WEIGHTS[I] < 5.0 then
underFiveCount = underFiveCount + 1
end if
end if
end loop
if found == true then
output "Total Weight: ", totalWeight
output "Heaviest Package ID: ", heaviestID
output "Packages under 5kg: ", underFiveCount
else
output "No packages found for destination: ", targetDest
end if
end processPackages
```
### Part (c) Solution
- **Improvement:** By sorting the `IDS` array, we can use a Binary Search algorithm instead of a Linear Search.
- **Reason:** This improves search time complexity from \(O(N)\) to \(O(\log N)\), making searches significantly faster as the dataset scales.
- **Overhead:** Sorting the `IDS` array requires simultaneously reordering the corresponding elements in both the `DESTINATIONS` and `WEIGHTS` arrays to maintain index-level alignment. This parallel movement of data introduces significant computational overhead whenever a package is added, removed, or sorted.
評分準則
### Part (a) [3 marks]
- **1 mark:** Correct definition of 'parallel arrays' as multiple arrays where elements at the same index index correspond to the same real-world entity.
- **1 mark:** Identifying a valid disadvantage (e.g., maintaining alignment during operations like sorting, insertion, or deletion).
- **1 mark:** Developing the explanation contextually (e.g., sorting package IDs requires manually shifting destinations and weights; failure to do so results in mismatched package information).
### Part (b) [9 marks]
- **1 mark:** Initializing all tracking variables correctly (e.g., `totalWeight` to 0, `maxWeight` to a low/sentinel value, `found` to false).
- **1 mark:** Loop correctly configured to iterate through all 500 elements (e.g., `0 to 499` or `1 to 500` depending on index convention).
- **1 mark:** Correct conditional check comparing array elements to `targetDest` (e.g., `DESTINATIONS[I] == targetDest`).
- **1 mark:** Correct accumulation of `totalWeight` within the matching block.
- **2 marks:** Correct logic to determine the heaviest package (1 mark for updating the running maximum weight, 1 mark for storing the corresponding ID from the parallel array `IDS[I]`).
- **1 mark:** Correct counter increment for packages weighing under 5.0 kg.
- **1 mark:** Implementing a mechanism (such as a boolean flag or checking counter/total) to verify if any matches were found.
- **1 mark:** Correct final output of calculated values and appropriate error message handling when no matches are found.
### Part (c) [3 marks]
- **1 mark:** Suggesting Binary Search (or logarithmic search).
- **1 mark:** Explaining that it reduces the search time complexity to \(O(\log N)\), which is faster than \(O(N)\) linear search.
- **1 mark:** Explaining the overhead constraint: any sort of `IDS` requires the system to mirror every swap operation across both `DESTINATIONS` and `WEIGHTS` to prevent data corruption.