12.2.3 Turning a Structure Chart into Pseudocode
A structure chart describes the organisation of a modular solution. It identifies the modules, their relationships, the values passed between them and any selection or repetition used by the design.
To implement that design, each module must be represented by pseudocode and the parent modules must contain suitable calls, arguments and control structures.
By the end of this section, you should be able to:
- Translate module boxes into procedure or function definitions.
- Convert module connections into calls with suitable arguments.
- Represent returned values and updated parameters correctly.
- Translate selection and repetition notation into pseudocode.
- Check that pseudocode preserves the meaning of a structure chart.
- Derive a complete modular pseudocode solution from a given chart.
What Does βEquivalent Pseudocodeβ Mean?
Equivalent pseudocode implements the design expressed by the structure chart. It should preserve the same:
- module responsibilities;
- parent and child relationships;
- parameter flow;
- returned or updated values;
- selection conditions;
- repetition conditions.
Equivalent does not necessarily mean that only one answer is possible. A returned result might be represented by a function, while another valid design might use a procedure with a reference parameter.
The important question is whether the pseudocode has the same effect as the chart.
Mapping Structure-Chart Features to Pseudocode
| Structure-chart feature | Pseudocode representation | Example |
|---|---|---|
| Top-level module | Main procedure or controlling module | PROCEDURE ProcessDonationBatch |
| Child module | Procedure or function definition | PROCEDURE ReadDonation(...) |
| Parent-to-child connection | Procedure call or function call | CALL DisplayBatchTotal(TotalPackets) |
| Parameter arrow into a module | Argument in the call and parameter in the header | PacketCount |
| Value returned by a function | Assignment containing a function call |
Accepted β DonationIsValid(PacketCount, GerminationRate)
|
| Selection marker | IF ... THEN ... ELSE ... ENDIF |
IF DonationAccepted = TRUE THEN |
| Repetition marker | WHILE, REPEAT or FOR |
WHILE MoreDonations = TRUE DO |
| Updated parameter | Reference parameter or a returned value assigned back to the variable | BYREF TotalPackets : INTEGER |
| Boolean control flag | Boolean variable used in a condition | DonationAccepted |
Start with the Top-Level Module
The highest module represents the complete task. It normally becomes the main procedure that coordinates the lower-level modules.
Suppose the top-level box is:
ProcessDonationBatch
An initial pseudocode skeleton can be written as:
PROCEDURE ProcessDonationBatch
ENDPROCEDURE
At this stage, the module body is intentionally empty. Calls and control structures will be added by reading the lower levels of the chart.
Declare local variables
Values that must be retained by the controlling procedure should normally be declared locally inside it.
PROCEDURE ProcessDonationBatch
DECLARE SeedType : STRING
DECLARE PacketCount : INTEGER
DECLARE GerminationRate : INTEGER
DECLARE TotalPackets : INTEGER
DECLARE DonationAccepted : BOOLEAN
DECLARE MoreDonations : BOOLEAN
ENDPROCEDURE
Turn Child Modules into Calls
A lower-level module used by a parent must normally be called from the parent's pseudocode.
Consider this simplified hierarchy:
ProcessDonationBatch
βββ ReadDonation
βββ DonationIsValid
βββ DisplayBatchTotal
The controlling procedure might contain:
CALL ReadDonation(SeedType, PacketCount, GerminationRate)
DonationAccepted β DonationIsValid(
PacketCount,
GerminationRate
)
CALL DisplayBatchTotal(TotalPackets)
| Module type | Typical call form |
|---|---|
| Procedure performing an action | CALL ProcedureName(Arguments) |
| Function returning a value |
Result β FunctionName(Arguments)
|
| Function used directly in a condition |
IF FunctionName(Arguments) = TRUE THEN
|
Deciding Between a Procedure and a Function
A structure-chart box identifies a module but does not always state whether that module must be implemented as a procedure or a function.
| Use a procedure when... | Use a function when... |
|---|---|
| The module performs an action such as input or output. | The module calculates and returns one value. |
| The module needs to update several reference parameters. | The returned result belongs naturally inside an expression. |
| No single value represents the module's main result. | The function has one clear result with a suitable data type. |
Procedure example
PROCEDURE DisplayBatchTotal(
BYVALUE TotalPackets : INTEGER
)
OUTPUT "Accepted packets: ", TotalPackets
ENDPROCEDURE
Function example
FUNCTION DonationIsValid(
BYVALUE PacketCount : INTEGER,
BYVALUE GerminationRate : INTEGER
) RETURNS BOOLEAN
RETURN PacketCount > 0
AND GerminationRate >= 70
ENDFUNCTION
Translate Parameter Arrows into Interfaces
A labelled arrow identifies a value that crosses a module boundary. That value must appear in the module header and in the corresponding call.
Argument: the actual value or expression supplied in a call.
Structure-chart information
DonationIsValid receives:
PacketCount
GerminationRate
DonationIsValid returns:
DonationAccepted
Equivalent pseudocode
DonationAccepted β DonationIsValid(
PacketCount,
GerminationRate
)
FUNCTION DonationIsValid(
BYVALUE NumberOfPackets : INTEGER,
BYVALUE Rate : INTEGER
) RETURNS BOOLEAN
RETURN NumberOfPackets > 0
AND Rate >= 70
ENDFUNCTION
In the call, PacketCount and GerminationRate are arguments.
In the function header, NumberOfPackets and Rate are
parameters.
Translate Selection into an IF Statement
A selection marker and its labelled branches become a conditional statement.
Structure-chart meaning
DonationAccepted?
βββ TRUE: AddAcceptedPackets
β DisplayAcceptance
βββ FALSE: DisplayRejection
Equivalent pseudocode
IF DonationAccepted = TRUE THEN
CALL AddAcceptedPackets(
PacketCount,
TotalPackets
)
CALL DisplayAcceptance(
SeedType,
PacketCount
)
ELSE
CALL DisplayRejection(SeedType)
ENDIF
All modules below the true branch are placed inside the THEN block.
Modules below the false branch are placed inside the ELSE block.
IF statement. Only the selected branch should execute.
Translate Repetition into a Loop
A repetition arrow identifies the module or modules that must be called repeatedly. The label identifies the controlling condition.
Structure-chart meaning
Repeat while MoreDonations = TRUE:
ReadDonation
DonationIsValid
selected acceptance or rejection modules
CheckForMoreDonations
Equivalent pseudocode
MoreDonations β CheckForMoreDonations()
WHILE MoreDonations = TRUE DO
CALL ReadDonation(
SeedType,
PacketCount,
GerminationRate
)
DonationAccepted β DonationIsValid(
PacketCount,
GerminationRate
)
// selection pseudocode belongs here
MoreDonations β CheckForMoreDonations()
ENDWHILE
The flag is checked before the first cycle, so a WHILE loop is suitable.
A REPEAT loop would be more suitable if the repeated group had to run
at least once before the condition was tested.
Translate an Updated Parameter
A double-headed arrow means that a value is supplied to a module and comes back with a possible update.
Structure-chart meaning
AddAcceptedPackets receives:
PacketCount
TotalPackets
AddAcceptedPackets updates:
TotalPackets
One valid implementation uses a reference parameter:
CALL AddAcceptedPackets(
PacketCount,
TotalPackets
)
PROCEDURE AddAcceptedPackets(
BYVALUE PacketCount : INTEGER,
BYREF TotalPackets : INTEGER
)
TotalPackets β TotalPackets + PacketCount
ENDPROCEDURE
Another valid implementation could use a function:
TotalPackets β CalculateNewTotal(
PacketCount,
TotalPackets
)
FUNCTION CalculateNewTotal(
BYVALUE PacketCount : INTEGER,
BYVALUE CurrentTotal : INTEGER
) RETURNS INTEGER
RETURN CurrentTotal + PacketCount
ENDFUNCTION
BYREF merely because an
arrow is drawn. Use it when the procedure must alter the caller's variable.
Worked Example: Seed-Library Donation Batch
A community seed library processes donation forms. Each form contains:
- a seed type;
- the number of packets;
- a germination-rate percentage.
A donation is accepted when the number of packets is greater than zero and the germination rate is at least 70%. Accepted packet quantities are added to a batch total. Processing continues while more donation forms remain.
Text representation of the structure chart
ProcessDonationBatch
βββ InitialiseBatch
β βββ returns TotalPackets
βββ CheckForMoreDonations
β βββ returns MoreDonations
βββ WHILE MoreDonations = TRUE
β βββ ReadDonation
β β βββ returns SeedType, PacketCount,
β β GerminationRate
β βββ DonationIsValid
β β βββ receives PacketCount, GerminationRate
β β βββ returns DonationAccepted
β βββ IF DonationAccepted = TRUE
β β βββ AddAcceptedPackets
β β β βββ receives PacketCount
β β β βββ updates TotalPackets
β β βββ DisplayAcceptance
β β βββ receives SeedType, PacketCount
β βββ ELSE
β βββ DisplayRejection
β βββ receives SeedType
βββ DisplayBatchTotal
βββ receives TotalPackets
Step 1: Create the controlling procedure
PROCEDURE ProcessDonationBatch
DECLARE SeedType : STRING
DECLARE PacketCount : INTEGER
DECLARE GerminationRate : INTEGER
DECLARE TotalPackets : INTEGER
DECLARE DonationAccepted : BOOLEAN
DECLARE MoreDonations : BOOLEAN
ENDPROCEDURE
Step 2: Add initialisation
CALL InitialiseBatch(TotalPackets)
MoreDonations β CheckForMoreDonations()
Step 3: Add the repeated module calls
WHILE MoreDonations = TRUE DO
CALL ReadDonation(
SeedType,
PacketCount,
GerminationRate
)
DonationAccepted β DonationIsValid(
PacketCount,
GerminationRate
)
MoreDonations β CheckForMoreDonations()
ENDWHILE
Step 4: Place the selected calls inside the loop
IF DonationAccepted = TRUE THEN
CALL AddAcceptedPackets(
PacketCount,
TotalPackets
)
CALL DisplayAcceptance(
SeedType,
PacketCount
)
ELSE
CALL DisplayRejection(SeedType)
ENDIF
Step 5: Add the final output
CALL DisplayBatchTotal(TotalPackets)
Complete Derived Pseudocode
Controlling procedure
PROCEDURE ProcessDonationBatch
DECLARE SeedType : STRING
DECLARE PacketCount : INTEGER
DECLARE GerminationRate : INTEGER
DECLARE TotalPackets : INTEGER
DECLARE DonationAccepted : BOOLEAN
DECLARE MoreDonations : BOOLEAN
CALL InitialiseBatch(TotalPackets)
MoreDonations β CheckForMoreDonations()
WHILE MoreDonations = TRUE DO
CALL ReadDonation(
SeedType,
PacketCount,
GerminationRate
)
DonationAccepted β DonationIsValid(
PacketCount,
GerminationRate
)
IF DonationAccepted = TRUE THEN
CALL AddAcceptedPackets(
PacketCount,
TotalPackets
)
CALL DisplayAcceptance(
SeedType,
PacketCount
)
ELSE
CALL DisplayRejection(SeedType)
ENDIF
MoreDonations β CheckForMoreDonations()
ENDWHILE
CALL DisplayBatchTotal(TotalPackets)
ENDPROCEDURE
Initialisation procedure
PROCEDURE InitialiseBatch(
BYREF TotalPackets : INTEGER
)
TotalPackets β 0
ENDPROCEDURE
Input procedure
PROCEDURE ReadDonation(
BYREF SeedType : STRING,
BYREF PacketCount : INTEGER,
BYREF GerminationRate : INTEGER
)
OUTPUT "Seed type:"
INPUT SeedType
OUTPUT "Number of packets:"
INPUT PacketCount
OUTPUT "Germination rate:"
INPUT GerminationRate
ENDPROCEDURE
Validation function
FUNCTION DonationIsValid(
BYVALUE PacketCount : INTEGER,
BYVALUE GerminationRate : INTEGER
) RETURNS BOOLEAN
RETURN PacketCount > 0
AND GerminationRate >= 70
ENDFUNCTION
Updated-total procedure
PROCEDURE AddAcceptedPackets(
BYVALUE PacketCount : INTEGER,
BYREF TotalPackets : INTEGER
)
TotalPackets β TotalPackets + PacketCount
ENDPROCEDURE
Output procedures
PROCEDURE DisplayAcceptance(
BYVALUE SeedType : STRING,
BYVALUE PacketCount : INTEGER
)
OUTPUT PacketCount,
" packet(s) of ",
SeedType,
" accepted"
ENDPROCEDURE
PROCEDURE DisplayRejection(
BYVALUE SeedType : STRING
)
OUTPUT "Donation of ",
SeedType,
" rejected"
ENDPROCEDURE
PROCEDURE DisplayBatchTotal(
BYVALUE TotalPackets : INTEGER
)
OUTPUT "Total accepted packets: ",
TotalPackets
ENDPROCEDURE
More-donations function
FUNCTION CheckForMoreDonations()
RETURNS BOOLEAN
DECLARE Response : CHAR
OUTPUT "Another donation? Y/N"
INPUT Response
RETURN Response = 'Y'
ENDFUNCTION
Check That the Pseudocode Is Equivalent
After writing the pseudocode, compare it systematically with the chart.
| Check | Question | Seed-library example |
|---|---|---|
| Module coverage | Has every chart module been defined and called? |
ReadDonation,
DonationIsValid and all output modules appear.
|
| Interface accuracy | Do calls and headers contain matching values? |
DonationIsValid receives two integer arguments.
|
| Return values | Are returned results captured? |
The result of DonationIsValid is assigned to
DonationAccepted.
|
| Selection | Are true and false modules inside the correct branches? | Accepted packets are added only when the Boolean flag is true. |
| Repetition | Are exactly the intended modules inside the loop? | Reading, checking and responding repeat for each donation. |
| Loop update | Can the repetition condition change? |
MoreDonations is recalculated at the end of each cycle.
|
| Updated values | Does the new value reach the parent module? |
TotalPackets is passed by reference and updated.
|
| Variable scope | Are temporary values kept local where possible? | Each procedure uses only its parameters and local variables. |
Interactive: Chart-to-Pseudocode Translator
Build the pseudocode in layers. Use the tabs to move from module boxes to interfaces, control structures and the complete modular solution.
Common Mistakes and Misconceptions
- Writing calls but no module definitions. Each procedure or function used by the design should also be defined.
- Defining modules but never calling them. Child modules need to be connected to their parent in the pseudocode.
- Ignoring parameter arrows. Calls and headers must represent values passed across interfaces.
- Confusing arguments and parameters. Arguments appear in calls; parameters appear in headers.
- Ignoring a function's return value. The result should be assigned or used in an expression.
-
Calling both selection branches.
Alternative module calls belong inside the correct
THENandELSEblocks. - Placing final output inside the loop. Only modules below the chart's repetition marker should repeat.
- Failing to update the loop flag. This can produce an infinite loop.
- Assuming chart position gives complete execution order. Use parameter dependencies and control labels to determine the algorithm.
- Inventing internal logic not supported by the problem. The chart provides the design structure; module bodies must follow the stated requirements.
Practice
Core questions
- Explain what is meant by deriving equivalent pseudocode from a structure chart.
- State how a child module is represented in its parent's pseudocode.
- Explain how a returned function value should be used.
- Explain how a selection marker is translated.
- Explain how a repetition marker is translated.
- Give two possible ways of representing an updated value.
- Explain why a structure chart may not provide every statement needed inside a module.
Scenario A: Community Fridge Deliveries
A program processes food deliveries while more delivery records remain. It reads
a food category, quantity and use-by date. A function checks whether the delivery
can be accepted. Accepted quantities update TotalItems. Rejected
deliveries display a reason.
- Write a suitable top-level procedure header.
- Write the call to the input procedure.
- Write a Boolean validation function header.
- Write the assignment that captures the function result.
- Write the selection structure.
- Write the repetition structure.
- Define a procedure that updates
TotalItems.
Scenario B: Observatory Image Review
A program repeatedly reads an image identifier and confidence score. Images with a confidence score of at least 85 are stored for expert review. Other images are recorded as uncertain. After all images have been processed, the program outputs the number stored for review.
- Design a structure chart for the problem.
- Identify every parameter arrow.
- Identify the selection flag and repetition flag.
- Derive the controlling pseudocode.
- Define the procedures and functions.
- Check the pseudocode against the chart.
BYREF, then using a function return. Explain why both versions have the
same external effect.
Review
| Structure-chart feature | Pseudocode equivalent |
|---|---|
| Top-level module | Controlling procedure |
| Child module | Procedure or function definition and call |
| Downward parameter | Argument supplied to the called module |
| Upward function result | Return value assigned or used in an expression |
| Updated parameter | Reference parameter or value returned and reassigned |
| Selection marker | IF ... THEN ... ELSE ... ENDIF |
| Repetition marker | A suitable WHILE, REPEAT or FOR loop |
| Control flag | Boolean variable used by a condition |