Practice

Initialise DataStored

2024/May/June·Variant 1·Q1(b/c)
MEDIUMArrays

Adapted from Cambridge International AS & A Level Computer Science (9618), May/June 2024 Paper 41, Question 1.

Declare a global 1D array DataStored with space for up to 20 integers and a global variable NumberItems.

Write procedure Initialise() to:

  • input NumberItems
  • validate that it is between 1 and 20 inclusive
  • input and store that many integers in DataStored

The main program must set NumberItems to 0, call Initialise(), then output NumberItems followed by the stored values in order.

Input: Quantity of numbers, then each number. Invalid quantities may appear before a valid one. Output: NumberItems, then each stored value on a new line.

Example:

Input:  5
        10
        4
        7
        2
        9
Output: 5
        10
        4
        7
        2
        9
Premium is coming soon. All grading features are currently unlocked.

Sample Test Cases

Test 1: Five valid numbers
Inputs: 5, 10, 4, 7, 2, 9
Expected: 5 10 4 7 2 9
Test 2: Invalid counts rejected before valid count
Inputs: 0, 21, 3, 8, 6, 4
Expected: 3 8 6 4