AQA · GCSE · Computer Science · Higher
C# function for counting visitor days
Using C#, write a subroutine to help a museum review the number of visitors in a month.
- have the identifier countDays
- have the number of days a museum was open in the last month as a parameter
- get the user to enter the number of visitors to the museum for each of those days
- count how many of those days the museum had more than 200 visitors
- return the count
You should use meaningful variable name(s) and C# syntax in your answer.
Write your answer first. You can study the marking guidance whenever you need it.
Study the marking See what earns credit and compare it with a full-mark answer.
Marking points
- 1 Uses a definite iteration structure to process the specified number of days.
- 2 Uses selection to test each visitor number.
- 3 Correctly defines countDays with the number of open days as a parameter.
- 4 Inputs one visitor number for each day using correct iteration boundaries.
- 5 Initialises the counter to 0 and increments it only when visitors are greater than 200.
- 6 Returns the calculated counter rather than only displaying it.
Full-mark answer
static int countDays(int days) { int count = 0; for (int i = 0; i < days; i++) { int visitors = Convert.ToInt32(Console.ReadLine()); if (visitors > 200) { count = count + 1; } } return count; }
Why this answer loses marks
I looped through the inputs, counted matching days and displayed the count.
The task requires a named parameterised subroutine that returns the count; displaying it is not the same as returning it.