11.1.4 Input, Output and Built-in Routines
A useful program needs a way to receive data, process it and communicate
results. Pseudocode represents these interactions using
INPUT and OUTPUT.
Programs can also call ready-made routines instead of recreating every operation. These routines may measure a string, extract characters, change letter case, truncate a real value or generate a pseudo-random number.
By the end of this section, you should be able to:
- Write pseudocode that receives a value from the keyboard.
- Produce clear console output containing text, variables and results.
- Trace how data moves from input through processing to output.
- Explain the purpose of built-in functions and library routines.
- Interpret a supplied function signature.
- Use common string and numeric routines in expressions.
- Distinguish a returned value from output displayed on the console.
- Apply supplied routines without assuming undocumented behaviour.
Input, Processing and Output
Many short algorithms can be understood as a flow of data through three stages.
| Stage | Purpose | Possible pseudocode |
|---|---|---|
| Input | Receive data that the algorithm needs | INPUT Quantity |
| Processing | Calculate, compare, convert or manipulate the data | Total ← Quantity * UnitPrice |
| Output | Communicate a result or message | OUTPUT "Total: $", Total |
Receiving Keyboard Input
The INPUT command waits for a value and stores it in a variable.
The variable must already have been declared with an appropriate data type.
Pseudocode pattern
INPUT <Identifier>
Example
DECLARE CrateCount : INTEGER
OUTPUT "Enter the number of crates: "
INPUT CrateCount
The prompt is produced using OUTPUT. The
INPUT statement then stores the entered value in
CrateCount.
Input must have a destination
| Statement | Valid? | Reason |
|---|---|---|
INPUT CrateCount |
Yes | The value has a variable in which to be stored |
INPUT 12 |
No | A literal value is not a storage location |
INPUT MAX_CRATES |
No, if it is a constant | A constant must not receive a new value |
Producing Console Output
The OUTPUT command displays information. It can display literal
text, stored values, expression results or values returned by functions.
Pseudocode pattern
OUTPUT <Value or values>
| Purpose | Example |
|---|---|
| Display a message | OUTPUT "Processing complete" |
| Display one variable | OUTPUT CrateCount |
| Display text and a variable | OUTPUT "Crates loaded: ", CrateCount |
| Display several values |
OUTPUT "Section ", Zone, ", shelf ", ShelfNumber
|
| Display an expression result | OUTPUT "Spaces remaining: ", Capacity - Occupied |
| Display a returned value | OUTPUT "Characters: ", LENGTH(ItemName) |
OUTPUT "CrateCount" displays the word, not the variable’s value.
Building a Complete Input–Output Sequence
A short interaction normally introduces the required variables, displays a prompt, receives data, performs processing and then labels the result clearly.
Original example: equipment labels
CONSTANT LABEL_PRICE = 1.75
DECLARE LabelCount : INTEGER
DECLARE TotalCost : REAL
OUTPUT "Enter the number of labels required: "
INPUT LabelCount
TotalCost ← LabelCount * LABEL_PRICE
OUTPUT "Labels ordered: ", LabelCount
OUTPUT "Total cost: $", TotalCost
| Statement group | Role |
|---|---|
| Declarations | Prepare storage for the entered and calculated data |
| Prompt and input | Tell the user what to enter and store the response |
| Assignment | Calculate the required result |
| Output | Display labelled information rather than an unexplained number |
OUTPUT TotalCost may be technically valid, but
OUTPUT "Total cost: $", TotalCost communicates its meaning more
clearly.
Using Ready-Made Routines
A programming environment provides routines for operations that programmers frequently need. Calling a ready-made routine avoids writing its internal algorithm each time.
A function call is normally used wherever its returned value is required.
NameLength ← LENGTH(DeviceName)
OUTPUT "Final four characters: ", RIGHT(DeviceName, 4)
In the first statement, the returned integer is assigned to a variable. In the second, the returned string is sent directly to output.
Reading a Function Signature
A supplied signature explains how a function must be called. It identifies the expected arguments and the type of value returned.
RIGHT(Text : STRING, Count : INTEGER) RETURNS STRING
| Part | Meaning |
|---|---|
RIGHT |
The function name |
Text : STRING |
The first argument must be a string |
Count : INTEGER |
The second argument must be an integer |
RETURNS STRING |
The function call is replaced by a string result |
Valid use
Suffix ← RIGHT("SENSORGRID", 4)
The function receives a string and an integer. It returns
"GRID".
Calls that do not match the signature
RIGHT(42, 4)
RIGHT("SENSORGRID", "four")
RIGHT("SENSORGRID")
These calls use the wrong argument type or the wrong number of arguments.
String Functions and Operations
String routines can count, extract or change characters. The following pseudocode functions illustrate the operations students may need to apply.
| Routine or operation | Purpose | Original example | Result |
|---|---|---|---|
LENGTH(Text) |
Return the number of characters | LENGTH("Greenhouse") |
10 |
RIGHT(Text, Count) |
Return characters from the right-hand end | RIGHT("CYBERLAB", 3) |
"LAB" |
MID(Text, Start, Count) |
Return a specified number of characters from a position | MID("ALGORITHM", 3, 4) |
"GORI" |
UCASE(Character) |
Return the uppercase equivalent of one character | UCASE('m') |
'M' |
LCASE(Character) |
Return the lowercase equivalent of one character | LCASE('Q') |
'q' |
& |
Concatenate strings | "Lab " & "Access" |
"Lab Access" |
Positions used by MID
Consider the string "ALGORITHM".
| Position | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 |
|---|---|---|---|---|---|---|---|---|---|
| Character | A | L | G | O | R | I | T | H | M |
MID("ALGORITHM", 3, 4) starts at position 3 and returns four
characters: "GORI".
Numeric Routines
Taking the integer part
The INT function returns the integer part of a real value.
WholeUnits ← INT(14.87)
WholeUnits receives 14. The fractional part is
removed; the value is not rounded to 15.
Generating a pseudo-random real value
RAND(Limit) returns a real value from zero up to, but not
including, the supplied limit.
RandomReading ← RAND(20)
The result may be a value such as 7.43, satisfying:
0 <= RandomReading AND RandomReading < 20
Generating an integer from 1 to 8
RandomNumber ← INT(RAND(8)) + 1
RAND(8)produces a real value from 0 up to, but not including, 8.INTproduces an integer from 0 to 7.- Adding 1 shifts the possible results to 1 through 8.
INT truncates. It does not
perform conventional rounding.
Programming-Language Extension
Real programming languages may use different function names and indexing rules. These language-specific forms are useful for implementation, but they should not replace the required pseudocode notation in a pseudocode answer.
Python slicing
Text[start:end]
Python indexes begin at zero. The start index is included and the end index is excluded.
| Expression | Result for Text = "ALGORITHM" |
|---|---|
Text[2:6] |
"GORI" |
Text[:4] |
"ALGO" |
Text[-3:] |
"THM" |
Converting input in a programming language
Some languages initially receive console input as text. A conversion routine may therefore be needed before arithmetic can be performed.
QuantityText = input("Enter quantity: ")
Quantity = int(QuantityText)
Worked Example: Asset-Label Generator
A laboratory system receives an asset name and a zone letter. It creates a short label using the final four characters of the asset name and an uppercase version of the zone.
Step 1: declarations
DECLARE AssetName : STRING
DECLARE ZoneLetter : CHAR
DECLARE ShortCode : STRING
DECLARE NormalisedZone : CHAR
DECLARE CharacterCount : INTEGER
Step 2: input
OUTPUT "Enter the asset name: "
INPUT AssetName
OUTPUT "Enter the zone letter: "
INPUT ZoneLetter
Step 3: use the routines
ShortCode ← RIGHT(AssetName, 4)
NormalisedZone ← UCASE(ZoneLetter)
CharacterCount ← LENGTH(AssetName)
Step 4: display labelled results
OUTPUT "Generated label: ", NormalisedZone, "-", ShortCode
OUTPUT "Characters in asset name: ", CharacterCount
Trace using sample input
| Input or expression | Value or result |
|---|---|
AssetName |
"ROBOTARM" |
ZoneLetter |
'c' |
RIGHT("ROBOTARM", 4) |
"TARM" |
UCASE('c') |
'C' |
LENGTH("ROBOTARM") |
8 |
| Final output | Generated label: C-TARM |
Interactive: Input–Process–Output Tracer
Use the existing tracer to follow data from user input through calculation to console output. Change the input values and inspect the program state after each statement.
Interactive: Built-in Routine Explorer
The string and numeric modes reinforce the general idea of supplying arguments and receiving a return value. The slicing and conversion modes are programming-language extensions.
Implementation Note: Readable Code
Comments can explain why a less-obvious operation is being performed. They are intended for human readers and do not form part of the program’s output.
// Produce an integer from 1 to 8
RandomNumber ← INT(RAND(8)) + 1
Common Mistakes and Misconceptions
-
Putting the prompt inside INPUT: use
OUTPUTfor the prompt andINPUT Identifierto receive the value. - Inputting into a literal or constant: input needs a variable destination.
-
Quoting identifiers:
OUTPUT "Total"displays text rather than the value ofTotal. - Unlabelled results: displaying a number without indicating what it represents.
- Confusing return and output: a returned value is available to the calling expression but is not automatically displayed.
- Using the wrong argument type: passing text where a routine requires an integer.
- Ignoring parameter order: swapping the start position and character count in a string function.
- Assuming all positions start at zero: follow the supplied routine definition rather than applying Python rules.
-
Confusing truncation with rounding:
INT(9.96)returns9. - Assuming random bounds: check whether each limit is included or excluded.
Practice
Question 1: keyboard input
Write pseudocode that displays a prompt and receives an integer into
RobotCount.
Question 2: labelled output
Write one output statement that displays the text
"Available spaces: " followed by the value of
SpacesAvailable.
Question 3: several output values
Write one statement that displays a zone letter, shelf number and item count with suitable labels.
Question 4: string routines
Evaluate:
LENGTH("Microchip")RIGHT("AUTOMATION", 4)MID("SECURITY", 2, 3)UCASE('r')
Question 5: read the signature
TAIL(Text : STRING, Count : INTEGER) RETURNS STRING
- State the type required for each argument.
- State the return type.
- Explain why
TAIL(27, "four")is invalid.
Question 6: numeric routines
- State the result of
INT(18.99). - State the smallest possible value of
INT(RAND(5)). - State the largest possible value of
INT(RAND(5)). - Write an expression that generates an integer from 1 to 5.
Question 7: complete the algorithm
An algorithm receives a workshop name and displays the name together with its length.
DECLARE WorkshopName : STRING
DECLARE NameLength : INTEGER
// Add the missing prompt, input, function call and output.
Question 8: correct the errors
DECLARE Age : INTEGER
INPUT "Enter age: " Age
OUTPUT "Age"
LastPart ← RIGHT(Age, 3)
Identify three problems and suggest corrections.
Show suggested answers
Question 1
OUTPUT "Enter the number of robots: "
INPUT RobotCount
Question 2
OUTPUT "Available spaces: ", SpacesAvailable
Question 3
OUTPUT "Zone: ", ZoneLetter,
", shelf: ", ShelfNumber,
", items: ", ItemCount
Question 4
9"TION""ECU"'R'
Question 5
- The first argument must be a
STRING. - The second argument must be an
INTEGER. - The function returns a
STRING. - The supplied call uses an integer for the first argument and a string for the second.
Question 6
1804INT(RAND(5)) + 1
Question 7
DECLARE WorkshopName : STRING
DECLARE NameLength : INTEGER
OUTPUT "Enter the workshop name: "
INPUT WorkshopName
NameLength ← LENGTH(WorkshopName)
OUTPUT "Workshop: ", WorkshopName
OUTPUT "Number of characters: ", NameLength
Question 8
-
The prompt should be a separate statement:
OUTPUT "Enter age: ". -
Input should then be written as
INPUT Age. -
OUTPUT "Age"displays literal text; useOUTPUT Ageor a labelled form. -
RIGHTrequires a string, whileAgeis an integer.
Review
| Concept | Key idea | Example |
|---|---|---|
| Input | Receive a value into a variable | INPUT Quantity |
| Output | Display one or more values | OUTPUT "Quantity: ", Quantity |
| Built-in function | A ready-made operation that returns a value | LENGTH(ItemName) |
| Argument | A value supplied to a routine | 4 in RIGHT(Name, 4) |
| Return value | The result that replaces the function call | A string returned by RIGHT |
INT |
Return the integer part of a real value | INT(12.75) = 12 |
RAND |
Return a pseudo-random real value below a limit | RAND(10) |