Skip to main content
CSP LabBy Educator Today
Demonstration view (not a real account role)
Demo — saved on this device
Sample lesson · public practiceAbout 12 minutes

Designing a useful program

Before writing any code, a good programmer asks: who is this for, what goes in, what happens to it, and what comes out? You will answer those questions for a small program that helps students choose a school club.

Uses ideas from 1.2 Program Function and Purpose; 3.8 Iteration; 3.10 Lists. This sample does not cover those topics in full.

Loading saved work…

Purpose

Clubs at a school have a limited number of places. A student — or a group of friends who want to join together — needs to know how many clubs still have enough room.

Purpose statement: help a student find out how many clubs have at least the number of remaining places they need.

A clear purpose tells you what “correct” means. Here, a correct program gives the right count of suitable clubs; it does not need to name them or sign anyone up.

Input: a list of capacities

The program needs two inputs:

  • capacities — a list holding the number of remaining places for each club, for example [0, 5, 2, 8].
  • minimum — the number of places the student needs, for example 3.

Using a list means the program does not care whether the school has four clubs or forty. The same steps work for any length, including an empty list. Each element has a value (places left) and a position (where it sits in the list). In AP pseudocode, positions start at 1. Keep the two ideas separate: in [0, 5, 2, 8], the element at position 3 has the value 2.

Processing

The program examines every capacity in turn. For each one it asks a yes-or-no question: does this club have at least minimum places? If yes, it adds 1 to a running count.

AP-style pseudocode · countAvailable
PROCEDURE countAvailable(capacities, minimum){  count ← 0  FOR EACH places IN capacities  {    IF (places ≥ minimum)    {      count ← count + 1    }  }  RETURN count}

FOR EACH is iteration: it repeats once per element. IF is selection: it decides whether to run the increment. The parameter minimum makes the procedure reusable — the same code answers “clubs with 3 places?” and “clubs with 6 places?”.

Output

The procedure returns a single number: how many clubs meet the condition. Returning the count, rather than printing a message, lets other parts of a program use the result — for example to show “2 clubs have room for you”.

Worked example

Trace countAvailable([0, 5, 2, 8], 3). count starts at 0.

Trace of countAvailable with capacities [0, 5, 2, 8] and minimum 3
Positionplaces (value)ConditionResultcount after
100 ≥ 3false0
255 ≥ 3true1
322 ≥ 3false1
488 ≥ 3true2

The procedure returns 2. Notice that count changes only on rows where the condition is true, and that it goes up by 1 — not by the value of places.

Prediction checkpoint

Make a prediction before you read the explanation.

Prediction checkpointA club has exactly 3 places left and the minimum is 3. Will the program count that club?

Testing the program

One example is not enough. Good tests are chosen on purpose, each checking a different kind of situation:

Planned tests for countAvailable
SituationCallExpected
No matchescountAvailable([0, 5, 2, 8], 9)0
Several matchescountAvailable([0, 5, 2, 8], 3)2
A value equal to the thresholdcountAvailable([0, 5, 2, 8], 2)3

The third test matters most. If someone accidentally wrote > instead of ≥, the first two tests would still pass but this one would return 2. Tests that sit exactly on a boundary catch errors that ordinary examples miss.

Reflection

Saved on this device only. Nothing is sent to a teacher in this preview.

Answer the prediction checkpoint and write a short reflection to mark this practice complete.

Next: the sample lab

In the lab you will step through the same procedure yourself, change the minimum, try teacher-authored test datasets and record your reasoning.

Open Club capacity investigation