A program P reads in 500 integers in the range [0..100] representing the…
2005
A program P reads in 500 integers in the range [0..100] representing the scores of 500 students. It then prints the frequency of each score above 50. What would be the best way for P to store the frequencies?
- A.
An array of 50 numbers
- B.
An array of 100 numbers
- C.
An array of 500 numbers
- D.
A dynamically allocated array of 550 numbers
Attempted by 1317 students.
Show answer & explanation
Correct answer: A
Answer: Use an array of 50 integers, one counter for each score from 51 through 100.
Create an array named frequencies with 50 elements and initialize all elements to 0.
For each of the 500 input scores s: if s > 50 then increment frequencies[s - 51].
After processing, frequencies[i] contains the count of score (i + 51). Print each score (i + 51) with its corresponding frequencies[i].
Why this is best:
Uses minimal fixed space (50 counters) because only scores 51–100 need tracking.
Updates are constant time (O(1)) per score; overall time is O(500) for the inputs.
Avoids unnecessary memory use compared to larger arrays sized for unrelated ranges or per-student storage.
Implementation note: If you instead needed frequencies for the entire 0–100 range, use an array of 101 counters and index directly by the score value.
A video solution is available for this question — log in and enroll to watch it.