Skip to content
Book 2: Fundamental Problem-solving and Programming Skills

Book 2 · Chapter 12

Software Development

§12.1§12.2§12.3Core · ≈ 13 m · →B2·12

This chapter is in the ebook.

Read it in full on cswithzak.com. Every question has its mark scheme and every program is ready to run. The free practice for each section is below, with the short code the book prints beside it.

Open in the ebook

For the whole chapter

§12.1 Program development life cycle

Short code B2·12·1

§12.2 Program design

Short code B2·12·2

§12.3 Program testing · Program maintenance

Short code B2·12·3

The program development life cycle

Short code B2·12·10

Practice for this page of the book: the quiz, flashcards and past-paper questions for its section are listed above.

Waterfall, iterative and RAD

Short code B2·12·11

Practice for this page of the book: the quiz, flashcards and past-paper questions for its section are listed above.

Structure charts and pseudocode

Short code B2·12·20

FUNCTION GetDaysLate() RETURNS INTEGER
   DECLARE Days : INTEGER
   INPUT Days
   RETURN Days
ENDFUNCTION

FUNCTION CalculateFine(DaysLate : INTEGER) RETURNS INTEGER
   DECLARE Fine : INTEGER
   Fine ← DaysLate * 5
   IF Fine > 50 THEN
      Fine ← 50
   ENDIF
   RETURN Fine
ENDFUNCTION

PROCEDURE UpdateAccount(Fine : INTEGER, BYREF Balance : INTEGER)
   Balance ← Balance + Fine
ENDPROCEDURE

PROCEDURE ReturnBook()
   DECLARE DaysLate : INTEGER
   DECLARE Fine : INTEGER
   DECLARE Balance : INTEGER
   Balance ← 20
   DaysLate ← GetDaysLate()
   Fine ← CalculateFine(DaysLate)
   CALL UpdateAccount(Fine, Balance)
   OUTPUT "Fine ", Fine, ", balance now ", Balance
ENDPROCEDURE

CALL ReturnBook()
The ReturnBook structure chart as pseudocode. Each box is a module; DaysLate goes down to CalculateFine and Fine comes back up, so CalculateFine is a FUNCTION; Balance goes down and back up, so it is passed BYREF. Input: 4.

Output

› 4
Fine 20, balance now 40
Run and checked before printing
FUNCTION GetArrival() RETURNS INTEGER
   DECLARE Minutes : INTEGER
   INPUT Minutes
   RETURN Minutes
ENDFUNCTION

PROCEDURE MarkPresent()
   OUTPUT "Present"
ENDPROCEDURE

PROCEDURE MarkLate(MinutesLate : INTEGER)
   OUTPUT "Late by ", MinutesLate, " minutes"
ENDPROCEDURE

PROCEDURE RecordArrival(MinutesLate : INTEGER)
   IF MinutesLate = 0 THEN
      CALL MarkPresent()
   ELSE
      CALL MarkLate(MinutesLate)
   ENDIF
ENDPROCEDURE

PROCEDURE TakeRegister(ClassSize : INTEGER)
   DECLARE Student : INTEGER
   DECLARE MinutesLate : INTEGER
   FOR Student ← 1 TO ClassSize
      MinutesLate ← GetArrival()
      CALL RecordArrival(MinutesLate)
   NEXT Student
ENDPROCEDURE

CALL TakeRegister(3)
The register chart as pseudocode: the iteration arrow becomes the FOR loop in TakeRegister, the selection diamond becomes the IF in RecordArrival. Inputs: 0, 7, 0.

Output

› 0
Present
› 7
Late by 7 minutes
› 0
Present
Run and checked before printing
DECLARE Taken : ARRAY[1:40] OF BOOLEAN
DECLARE SeatsLeft : INTEGER
DECLARE Seat : INTEGER

FUNCTION GetAge() RETURNS INTEGER
   DECLARE Age : INTEGER
   INPUT Age
   RETURN Age
ENDFUNCTION

FUNCTION CheckSeat(SeatNo : INTEGER) RETURNS BOOLEAN
   RETURN NOT Taken[SeatNo]
ENDFUNCTION

PROCEDURE UpdateSeats(SeatNo : INTEGER, BYREF SeatsLeft : INTEGER)
   Taken[SeatNo] ← TRUE
   SeatsLeft ← SeatsLeft - 1
ENDPROCEDURE

FOR Seat ← 1 TO 40
   Taken[Seat] ← FALSE
NEXT Seat
SeatsLeft ← 40
OUTPUT "Age ", GetAge()
IF CheckSeat(12) THEN
   CALL UpdateSeats(12, SeatsLeft)
ENDIF
OUTPUT CheckSeat(12), " ", SeatsLeft
The Mark Ladder's three headers, run with simple bodies: GetAge and CheckSeat are functions (one value back up), UpdateSeats is a procedure (SeatsLeft goes down and comes back up, so BYREF). Input: 15.

Output

› 15
Age 15
FALSE 39
Run and checked before printing

State-transition diagrams and tables

Short code B2·12·21

Output

Closed + Valid ticket -> Open (raise barrier)
Open + Car passed -> Closed (lower barrier)
Closed + Invalid ticket -> Closed (show SEE STAFF)
Closed + Staff key -> Off (show CLOSED)
Off + Valid ticket -> no change
Off + Staff key -> Closed (hide CLOSED)
Run and checked before printing

Output

Locked + Wrong code -> Locked (red light)
Locked + Correct code -> Unlocked (green light)
Unlocked + Door closed -> Locked (none)
Locked + Third wrong code -> Alarm (siren on)
Alarm + Correct code -> no change
Alarm + Reset key -> Locked (siren off)
Run and checked before printing

Syntax, run-time and logic errors

Short code B2·12·30

DECLARE Temp : INTEGER
INPUT Temp
IF Temp > 30
   OUTPUT "Hot day"
ENDIF
A syntax error: the translator rejects the program before a single line runs.

Output

ERROR: Line 4: IF needs THEN after the condition — found the keyword OUTPUT instead
Run and checked before printing
DECLARE Total : INTEGER
DECLARE Count : INTEGER
DECLARE Mark : INTEGER
Total ← 0
Count ← 0
INPUT Mark
WHILE Mark <> -1
   Total ← Total + Mark
   Count ← Count + 1
   INPUT Mark
ENDWHILE
OUTPUT "Mean mark ", Total / Count
A run-time error: this program is fine for most data, but if the first mark entered is −1 it divides by zero while it is running.

Output

ERROR: Line 12: Division by zero
Run and checked before printing

Output

› 60
› 70
› -1
Mean mark 65
Run and checked before printing
DECLARE Mark : INTEGER
DECLARE Passes : INTEGER
Passes ← 0
INPUT Mark
WHILE Mark <> -1
   IF Mark > 50 THEN
      Passes ← Passes + 1
   ENDIF
   INPUT Mark
ENDWHILE
OUTPUT "Passes: ", Passes
A logic error: a pass is 50 or more, but the program counts only marks above 50. It runs, and quietly gives the wrong answer. Inputs: 50, 72, 49, -1 (two passes).

Output

› 50
› 72
› 49
› -1
Passes: 1
Run and checked before printing

Dry runs and correcting errors

Short code B2·12·31

Output

› 6
1 6 0
› 4
2 4 0
› 9
3 9 0
› 5
4 5 0
0
Run and checked before printing
DECLARE Temp : INTEGER
DECLARE Lowest : INTEGER
DECLARE Day : INTEGER
INPUT Lowest
FOR Day ← 2 TO 4
   INPUT Temp
   IF Temp < Lowest THEN
      Lowest ← Temp
   ENDIF
NEXT Day
OUTPUT Lowest
The correction: start Lowest at the first value, so every later value is compared with a real reading. Inputs: 6, 4, 9, 5.

Output

› 6
› 4
› 9
› 5
4
Run and checked before printing

Output

ERROR: Line 7: FUNCTION CountPasses never executes RETURN — a function must RETURN a value of type INTEGER
Run and checked before printing

Output

ERROR: Line 12: Index 0 is out of range — Marks was declared ARRAY[1:30] (the first element is Marks[1])
Run and checked before printing
DECLARE Marks : ARRAY[1:30] OF INTEGER
DECLARE N : INTEGER
FOR N ← 1 TO 30
   Marks[N] ← N * 3
NEXT N

FUNCTION CountPasses(PassMark : INTEGER) RETURNS INTEGER
   DECLARE Index : INTEGER
   DECLARE Count : INTEGER
   Count ← 0
   FOR Index ← 1 TO 30
      IF Marks[Index] >= PassMark THEN
         Count ← Count + 1
      ENDIF
   NEXT Index
   RETURN Count
ENDFUNCTION

OUTPUT CountPasses(45)
The corrected function, run on 30 test marks (3, 6, 9 … 90). Marks of 45 or more are 45, 48 … 90: sixteen of them.

Output

16
Run and checked before printing

Test methods

Short code B2·12·32

FUNCTION Band(Mark : INTEGER) RETURNS STRING
   IF Mark >= 70 THEN
      RETURN "Merit"
   ELSE
      IF Mark >= 50 THEN
         RETURN "Pass"
      ELSE
         RETURN "Fail"
      ENDIF
   ENDIF
ENDFUNCTION

OUTPUT "85 ", Band(85)
OUTPUT "60 ", Band(60)
OUTPUT "30 ", Band(30)
White-box testing: one test for each path through the code. Three paths need three marks: one from each band.

Output

85 Merit
60 Pass
30 Fail
Run and checked before printing
FUNCTION ReadSensor() RETURNS INTEGER
   // stub: the real sensor module is not written yet
   OUTPUT "ReadSensor stub called"
   RETURN 15
ENDFUNCTION

PROCEDURE ControlHeater()
   DECLARE Temp : INTEGER
   Temp ← ReadSensor()
   IF Temp < 18 THEN
      OUTPUT "Heater ON"
   ELSE
      OUTPUT "Heater OFF"
   ENDIF
ENDPROCEDURE

CALL ControlHeater()
A stub stands in for a module that is not written yet. It returns a fixed value, so the module that calls it can be tested now.

Output

ReadSensor stub called
Heater ON
Run and checked before printing

Test data and test plans

Short code B2·12·33

FUNCTION ValidAge(Age : INTEGER) RETURNS BOOLEAN
   IF Age >= 8 AND Age <= 16 THEN
      RETURN TRUE
   ELSE
      RETURN FALSE
   ENDIF
ENDFUNCTION

OUTPUT "12 ", ValidAge(12)
OUTPUT "8 ", ValidAge(8)
OUTPUT "16 ", ValidAge(16)
OUTPUT "7 ", ValidAge(7)
OUTPUT "17 ", ValidAge(17)
OUTPUT "-3 ", ValidAge(-3)
OUTPUT "40 ", ValidAge(40)
The ValidAge test plan, run. Each line is one test: the value, then what ValidAge returned.

Output

12 TRUE
8 TRUE
16 TRUE
7 FALSE
17 FALSE
-3 FALSE
40 FALSE
Run and checked before printing

Maintenance and enhancing a program

Short code B2·12·35

DECLARE Total : INTEGER
DECLARE Count : INTEGER
DECLARE Mark : INTEGER
DECLARE Highest : INTEGER
Total ← 0
Count ← 0
Highest ← -1
INPUT Mark
WHILE Mark <> -1
   Total ← Total + Mark
   Count ← Count + 1
   IF Mark > Highest THEN
      Highest ← Mark
   ENDIF
   INPUT Mark
ENDWHILE
IF Count = 0 THEN
   OUTPUT "No marks entered"
ELSE
   OUTPUT "Mean mark ", Total / Count
   OUTPUT "Highest mark ", Highest
ENDIF
The mean-mark program after maintenance: it now also reports the highest mark and refuses to divide when no marks were entered. Inputs: 60, 85, 70, 65, -1.

Output

› 60
› 85
› 70
› 65
› -1
Mean mark 70
Highest mark 85
Run and checked before printing

Command-word coach

Short code B2·12·36

Practice for this page of the book: the quiz, flashcards and past-paper questions for its section are listed above.

Workbook 2: Software Development

Short code W2·12 · Problem-solving and Programming Skills Workbook

The write-in workbook for this chapter has drills, exam-style practice, trace tables, fix-the-mistake and mark-it-yourself pages. Its full mark schemes and an interactive version arrive here with the chapter.

Ask Z about Software Development
Buy ZAK's books — WhatsApp the books desk: Buy the booksPrint or ebook