Writing an algorithm in pseudocode from a problem

Tip: Choose Save as PDF in the print dialog to download. Use A4 paper size.

Writing an algorithm in pseudocode from a problem

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. A problem says: 'Input two numbers and output their total.' Which line should come first in your algorithm?
A OUTPUT Total
B DECLARE Num1, Num2, Total : INTEGER
2. Which is the correct order for writing an algorithm from a problem?
A DECLARE, INPUT, process, OUTPUT
B OUTPUT, process, INPUT, DECLARE
3. A problem says: 'Output 'Pass' if the mark is 50 or more, otherwise output 'Fail'.' Which structure fits?
A FOR ... TO ... NEXT
B IF ... THEN ... ELSE ... ENDIF
4. A problem says: 'Input 30 test marks and output the total.' Which structure should you choose for the process?
A A FOR loop that repeats 30 times
B A CASE statement with 30 branches
5. A problem says: 'Input a menu choice of 1, 2 or 3 and output a different message for each.' Which structure is the neatest choice?
A CASE Choice OF ... ENDCASE
B REPEAT ... UNTIL Choice = 3

Match the Pairs

Draw a line connecting each item on the left to its match on the right.

Left columnRight column
1DECLARE Total : INTEGER AFOR Count ← 1 TO 5 INPUT Score NEXT Count
2INPUT Mark IF Mark >= 50 THEN OUTPUT "Pass" ENDIF BThe statements are in the wrong order and the IF is incomplete. The IF must come before the OUTPUT, wrap it, and end with ENDIF.
3The problem has several fixed choices for one value, so a CASE structure suits the process step CDeclare the variable and its data type at the start, before it is used in the algorithm
4The problem says to repeat an action a set number of times, so a FOR loop is used for the process step DInput a value, process it with a selection test from the problem, then output the result
5Total ← 0 INPUT Mark Total ← Total + Mark EThe variables Total and Mark are used but never declared. DECLARE Total : INTEGER and DECLARE Mark : INTEGER must come first.
6INPUT Age OUTPUT "Adult" IF Age >= 18 THEN FCASE OF Choice 1 : OUTPUT "Start" 2 : OUTPUT "Quit" ENDCASE

Definitions

DECLARE, INPUT, process, OUTPUT

Fill in the Blanks

Complete each line of code by filling in the missing parts.

1.
_______ Mark : INTEGER INPUT Mark OUTPUT Mark
2.
DECLARE Age : INTEGER _____ Age OUTPUT Age
3.
IF Mark >= 50 THEN OUTPUT "Pass" _____
4.
FOR Count ← 1 TO 5 INPUT Score ____ Count
5.
DECLARE Total : INTEGER Total ← 0 ______ Total

Spot the Error

Shade in the letter of the line that contains an error.

Question 1
A DECLARE Num1 : INTEGER
B DECLARE Num2 INTEGER
C INPUT Num1
D INPUT Num2
E OUTPUT Num1 + Num2
Question 2
A DECLARE Mark : INTEGER
B INPUT Mark
C IF Mark > 50 THEN
D OUTPUT "Pass"
E ENDIF
Question 3
A DECLARE Total : INTEGER
B DECLARE Num : INTEGER
C Total ← 1
D INPUT Num
E Total ← Total + Num
F OUTPUT Total
Question 4
A DECLARE Choice : INTEGER
B INPUT Choice
C CASE OF Choice
D 1 OUTPUT "Start"
E OTHERWISE : OUTPUT "Stop"
F ENDCASE
Question 5
A DECLARE Price : STRING
B DECLARE Total : REAL
C INPUT Price
D Total ← Price * 3
E OUTPUT Total

Answer Key

Writing an algorithm in pseudocode from a problem
Section A: Quick Questions
Q1
B — DECLARE Num1, Num2, Total : INTEGER
Q2
A — DECLARE, INPUT, process, OUTPUT
Q3
B — IF ... THEN ... ELSE ... ENDIF
Q4
A — A FOR loop that repeats 30 times
Q5
A — CASE Choice OF ... ENDCASE
Section B: Match the Pairs
Q1
DECLARE Total : INTEGER → Declare the variable and its data type at the start, before it is used in the algorithm
Q2
INPUT Mark IF Mark >= 50 THEN OUTPUT "Pass" ENDIF → Input a value, process it with a selection test from the problem, then output the result
Q3
The problem has several fixed choices for one value, so a CASE structure suits the process step → CASE OF Choice 1 : OUTPUT "Start" 2 : OUTPUT "Quit" ENDCASE
Q4
The problem says to repeat an action a set number of times, so a FOR loop is used for the process step → FOR Count ← 1 TO 5 INPUT Score NEXT Count
Q5
Total ← 0 INPUT Mark Total ← Total + Mark → The variables Total and Mark are used but never declared. DECLARE Total : INTEGER and DECLARE Mark : INTEGER must come first.
Q6
INPUT Age OUTPUT "Adult" IF Age >= 18 THEN → The statements are in the wrong order and the IF is incomplete. The IF must come before the OUTPUT, wrap it, and end with ENDIF.
Section C: Definitions
Q1
DECLARE, INPUT, process, OUTPUT — To write an algorithm from a problem, DECLARE variables first, then INPUT, process with IF, CASE or a loop, then OUTPUT. Check each part against the problem and use the exam format.
Section D: Fill in the Blanks
Q1
DECLARE Mark : INTEGER INPUT Mark OUTPUT Mark
Q2
DECLARE Age : INTEGER INPUT Age OUTPUT Age
Q3
IF Mark >= 50 THEN OUTPUT "Pass" ENDIF
Q4
FOR Count ← 1 TO 5 INPUT Score NEXT Count
Q5
DECLARE Total : INTEGER Total ← 0 OUTPUT Total
Section E: Spot the Error
Q1
B — A DECLARE statement needs a colon between the identifier and the data type. It should be DECLARE Num2 : INTEGER.
Q2
C — The problem says a mark of 50 or more passes, but > leaves out a mark of exactly 50. The condition should be Mark >= 50.
Q3
C — A running total must be set to 0 before anything is added, otherwise the answer is wrong by 1. It should be Total ← 0.
Q4
D — Each CASE branch needs a colon between the value and the statement. It should be 1 : OUTPUT "Start".
Q5
A — Price is used in a calculation and can have decimals, so it must be REAL. A STRING cannot be multiplied. It should be DECLARE Price : REAL.