Skip to content
IGCSE·Tuition
Computer Science · Lessons

Define inputs, outputs and constraints

Many wrong algorithms start from a task that was never stated precisely enough to solve.

On this page
  1. What goes into a specification?
  2. Worked example
  3. The mistake to watch for
  4. Check yourself
  5. Where this leads next

Before you write steps, write a short specification: what comes in, what must come out, and what is allowed. A clear specification turns “pack the eggs” into something you can trace and test.

This lesson follows decomposing a task. It also prepares you for tracing with a state table, because you need to know which values to track.

What goes into a specification?

Write three lists.

  • Inputs: each value the algorithm receives, with its type (whole number, real number, text).
  • Outputs: each value it must produce, with a description of how it relates to the inputs.
  • Constraints: the limits on inputs and any rules the outputs must obey.

If a value can be too small, too big, negative or not a whole number, the constraints say what should happen.

Worked example

A farm packs eggs into trays that hold exactly 12. Given the number of eggs, output how many full trays can be made and how many eggs are left over.

Step 1, inputs: Eggs, a whole number.

Step 2, outputs: Trays, the number of full trays. Left, the eggs not in a full tray.

Step 3, constraints: Eggs must be a whole number and at least 0. Left must be from 0 to 11, and Trays × 12 + Left must equal Eggs.

Step 4, algorithm:

INPUT Eggs
Trays ← Eggs DIV 12
Left ← Eggs MOD 12
OUTPUT Trays, Left

Step 5, trace with three values:

EggsTraysLeftCheck Trays × 12 + Left
1008496 + 4 = 100
110110 + 11 = 11
0000 + 0 = 0

For 100: 12 × 8 = 96 and 100 − 96 = 4, so DIV gives 8 and MOD gives 4. Each row satisfies the rule in the constraints, which is the check that matters.

The mistake to watch for

A common slip is using ordinary division and ignoring the constraint that trays must be whole.

Mistaken algorithm: Trays ← Eggs / 12, with Eggs = 100.

This gives 8.333…, which is not a number of trays.

The correction is to read the output description: “number of full trays” is a whole number, so DIV is the operation. A second slip is skipping the constraints, so no one decides what happens for −5 eggs or 3.5 eggs. Naming the constraint tells you to add a validation step or to state the assumption.

Check yourself

1. Write the inputs, outputs and constraints for an algorithm that gives the average of 5 test marks, each from 0 to 100.

Show answer

Inputs: five marks, each a whole number (or real number) from 0 to 100. Output: Average, the total of the marks divided by 5. Constraints: each mark must be from 0 to 100, so Average is also from 0 to 100.

2. Given 250 eggs, what are Trays and Left?

Show answer

12 × 20 = 240, so 250 DIV 12 = 20. 250 − 240 = 10, so 250 MOD 12 = 10. Check: 20 × 12 + 10 = 250.

3. Convert a number of days into whole weeks and spare days. Give the output for 30 days.

Show answer

Weeks ← Days DIV 7 and Spare ← Days MOD 7. 7 × 4 = 28, so Weeks = 4 and Spare = 2. Check: 4 × 7 + 2 = 30.

Where this leads next

Next, learn to trace a sequence with a state table so you can prove an algorithm meets its specification. The pseudocode trace trainer is useful for checking DIV and MOD results.

When a specification is vague, it is hard to know what to test, and a teacher can help you tighten it. That is part of our online one-to-one Computer Science tuition, and the practice set has more specifications to write.

Questions people ask

What is a constraint in an algorithm?

A constraint is a limit on what the algorithm accepts or must do. For example, a number of eggs must be a whole number that is zero or more. Stating constraints tells you what to validate and which test values to try.

What do DIV and MOD do?

DIV gives the whole-number part of a division and MOD gives the remainder. For example, 100 DIV 12 is 8 and 100 MOD 12 is 4. They are useful when a quantity is shared into equal groups and leftovers matter.

Can an algorithm have more than one output?

Yes. An algorithm can output several values, such as the number of full trays and the number of eggs left over. List each output separately in your specification so you can check each one in a trace.

Updated:

Your next step

If your algorithms work on the example in the question but fail on other values, a one-to-one teacher can help you write a specification first so the gaps show up before the code does.

Paid one-hour trial at your assigned teacher’s confirmed rate, starting from RM80. Other fees, schedules and ongoing arrangements are confirmed directly with your teacher after the trial class.

Tuition is arranged with a parent or guardian. Send them this page on WhatsApp and they can enquire for you.

Parent or guardian? Enquire here

9,000+ students helped through our service