Read a scenario and plan an algorithm
From a scenario to a contract
A booking system reads a requested group size. It must accept the whole group or refuse it; refusing must not change the available seats.
| Identifier | Tipo | Purpose |
|---|---|---|
| Remaining | INTEGER | Available seats |
| Tamaño | INTEGER | Requested seats |
| Accepted | BOOLEAN | Whether the request fits |
Separate the entrada, the decisión, the state change and the salida. Write down the inclusive boundary: a group using every remaining seat still fits.
Worked example: one request
Known: seven seats remain and the request is seven. Compare the request against remaining seats because accepting depends on the current capacity.
DECLARE Remaining, Size : INTEGER
Remaining ← 7
Size ← 7
IF Size <= Remaining THEN
Remaining ← Remaining - Size
OUTPUT "accepted"
ELSE
OUTPUT "rejected"
ENDIF
OUTPUT Remaining
The output is accepted, then 0. Changing Size to 8 must leave Remaining at 7. That second test checks the refused branch.
Guided, then independent
First finish the booking algorithm. Then solve the lift problem from a blank editor. In the reasoning box, explain why an oversized request must not change the total, and trace a request that exactly reaches the limit.
For booking seats, each request has a name, integer quantity and accepted/rejected status. Choose the types for those THREE fields in order from STRING, INTEGER, BOOLEAN, REAL.
Finish the starter: read N positive group sizes. Capacity is 15. Accept a group if it fits, output accepted/rejected, then remaining seats.
Haz clic en Ejecutar para ver la salida aquí.
A lift has a maximum total weight of 300. Read N (1 to 5) then N positive integer weights. Output the number admitted and final weight. Skip a person who would exceed 300; later people may still fit.
Haz clic en Ejecutar para ver la salida aquí.
Explain your choice of data structure and loop. Identify one boundary case and predict its result. For a subroutine, explain what is returned or changed in the caller.
Saved for this sitting on this browser. Review this yourself or with your teacher; these explanations are not automatically marked.
Review your explanation against these criteria
- My types and data structure match the data and the question.
- I justified the loop and checked its first and last iterations.
- Initial values and rejected operations preserve the intended state.
- My test includes input, purpose and expected result.
- I distinguished returning a value from printing and changing caller state.