Although pseudo code is fake code and can be written in a non-specific syntax the WJEC use their own standard and it is useful to understand how this work.
Self-documenting identifiers: using variable names that describe what the variable contains is important. Poor variable names can make it difficult to work out what is going on in a program. Sub-program/ function names should describe what the function does. ‘Function2’ is not a suitable name for a function used to calculate an average score, ‘calcAverage’ would be more appropriate.
Subroutines is a program unit/module that performs a particular task. These subroutines are merged to form larger programs and can be called by the main program or other subroutines. This method of programming is in modular design. Subroutines can be either functions or procedures.
Inputs and Outputs: when writing algorithms there is often a need to receive inputs at the beginning to assign value to variables. This can be done through terminology such as input(“Enter name”) name. Outputs will often be found throughout the algorithm when calculations have been completed and need to display to the user. Output will often appear towards the end of the algorithm. This can be done through terminology such as output(“Average: ”) average or output(average(“average”).
Sequence is a set of instructions that are processed one after another. For example, asking for two pieces of data to be inputted.
Selection is the process of decision making. So, the next instruction will be executed based on a certain condition. This process is normally completed using if statements or switch statements such as case select.
Iteration is the process of repeating instructions based on a condition. This can be either a set number of loops or until a condition is met. The most common iteration processes are for and while loops. For the loop to be exited there may be either a count or a rogue value to terminate the loop.
Counts are variables used to count values to satisfy certain conditions. An incremental count is one that usually counts upwards by one and can be shown as count = count + 1.
Rogue Values is the value that is entered into a loop for it to terminate. This value must be a value which would not normally arise. Example: mark = -1 or 999 would be a rogue value, where normally mark would be between 0 and 100.
String Handling is the dealing with the string data type in programming. A string can be manipulated to either extract information or to alter it based on a range of operations. There are many different types of operations that can be used within a programming language. Here are some examples and their python equivalent.
Data Structures An arrays store multiple items within a single variable. A two-dimensional array functions like a table, organising data into rows and columns.
1D arrays are linear data structures that can be defined as such myArray[6]. This would mean that the array has 6 elements within it. The first element will be located in position myArray[0].
2D arrays are data structures that are made up of rows and tables, that can be defined as such myArray[6, 5]. This would mean that the array has 6 elements in rows and 5 in columns. The first element will be located in position myArray[0, 0].
Inequality operators are used in logical conditions. DIV and MOD are used for integer division and finding the remainder.
Logical Operators are use in to make more complex logical conditions, XOR also use to show encryption using Binary.
String Handling is the dealing with the string data type in programming. A string can be manipulated to either extract information or to alter it based on a range of operations.
To find out how many characters are in a string we used the ‘len’ command. Any keyboard stroke is counted as a character so spaces and special characters like an exclamation mark are counted as well as letters and numbers.
greeting is String
greeting = “Hello from me!”
length = len(greeting)
print length
13
To discover the contents of all or part of a string we have to use the ‘mid’ command. Normally you would use the ‘len’ command to find out how long the string is before you would use the ‘mid’ command.
txt is String
partMessage is String
txt = “Have a happy birthday”
partMessage = mid(txt, 8, 5)
print partMessage
happy
To replace part of a string you need to use the ‘replace’ command.
txt is String
message is String
txt = “Have a happy birthday”
message = replace(txt, “happy”, “fantastic”)
print message
Have a fantastic birthday
Example 2:
txt is String
message is String
txt = “Have a happy birthday”
message = replace(txt, “a”, “xx”)
print message
fxxntxxstic birthdxxy
Data Validation is an automatic computer check to ensure that the data entered is sensible and reasonable. It does not check the accuracy of data (this is Verification).
Type check: checks something of the correct data type is entered.
E.g. a date in a date field or a number in a number field.
x is int
output(“Enter a number”)
input x
if type(x) is int
print("x is an integer")
else
print("x is not an integer")
end if
Range check: Checks if something is within a specific range. E.g. an order quantity is between 1 and 10, > 18years old, or <21 etc
age is int
input(“Enter your age”) age
if age >= 18 then
print(“You are old enough to enter the site”)
else
print(“You must be 18 to enter the site”)
end if
Presence check: checks that something is there. E.g. Mandatory field. Makes sure REQUIRED fields are entered often marked with *
name as string
output("Please enter your name")
input name
if name = “” then
print(“This cannot be left blank”)
else
print(“Welcome” & name)
end if
Format check: checks what is entered is of the correct structure. E.g. Postcode (LLNN NLL) E.g. TS23 3DX.
######## You will not be asked to write this in the exam #########
Def Sub is_valid_postcode(postcode)
postcode_pattern = "^[A-Z]{1,2}[0-9][0-9]?[A-Z]?[ ]?[0-9][A-Z]{2}$"
postcode is string
output(“Enter postcode”)
input postcode
if postcode matches postcode_pattern then
print(“Valid postcode”)
else
print(“Valid postcode”)
end if
Length check: Ensures that information is of a specified length. E.g. a mobile number field may be more than 11 characters. If it is less or more, it would not be accepted.
telephone is string
output(“Enter telephone number”)
input telephone
telephone_Length = len(telephone) or telephone.length
if telephone.length < 11 OR >14 then
print(“This is not the correct number of characters for telephone number”)
end if
This the the blended learning for the New GCSE. You can use this to work through to learn some more about writing algorithms. For the Sorts you only need to know Merge and Bubble, and for Search you only need to know Linear and Binary.
A food manufacturer produces ‘pots’ of yoghurt. It is important that the amount of yoghurt in each pot is at least the amount stated on the pot. The weight of yoghurt in a sample of pots is measured.
Design a program using pseudo-code with the following inputs:
● the required weight
● the number of pots to be sampled
● the weight of yoghurt in each of the pots in the sample
The outputs for the program should be for each pot found to be underweight:
● the pot number (1,2,3,etc.) and the weight of yoghurt in that pot
The program should also output the total number of underweight pots in the sample.
All outputs should contain suitable messages where helpful.
For instance, if the inputs are:
151
8
151 152 150 154 149 152 151 153
The outputs should be similar to:
Underweight pot: 3 150
Underweight pot: 5 149
Total number of underweight pots: 2 [6]
Try answering the question with this level of support (PURPLE least support) . If you get stuck Try the next level.
Use the Big 6 to help you:
Declare & Initialise variables
Input(s)
Loop structure & increment
Output(s)
Selection / Errors
Calculations / Processing
Also mark for
Algorithm works correctly
Use the Big 6 and the following example instructions to help you (REMEMBER you will have to structure your code):
Declare & Initialise variables
declare number as integer (you can omit declare e.g. number is integer)
inserted is Boolean
set number = 0
set inserted = False
Input(s)
output "Enter in a number"
input number
Loop structure & increment
You could use your own variable name potNumber insteed of i, we tend to use i and then j as counters within loops
Counter Controlled Loops
for i = 1 to ???
.....
next i
Condition Controlled loops - Exit controlled loop (check the condition after entering the loop)
--------------------------------------- OR -----------------------------------------------------
do repeat
.... OR ...
i = i + 1 i = i + 1
while (i <= ???) until (i > ???)
--------------------------------------- OR -----------------------------------------------------
Condition Controlled loops - Entry controlled loop (check the condition before entering the loop)
i = ??
while (i <= ???)
....
i = i + 1
end while
Output(s)
output "This is an output text"
output "The total is " , total
Selection / Errors
The condition can be made up of multiple elements using. OR AND NOT logical expressions between each condition
For single selection use one if statement
ONE Output
if (mark < passMark) then
....
end if
For double selection use one if else statement
if (mark < passMark) then
....
else
....
end if
For multiple selection use more than one if statement (else if)
if (mark < passMark) then
....
else if ( mark = passMark) then
....
else
....
end if
Calculations / Processing
total = total + number
total = total + (i * 3)
average = total / numberOfItems
sort Numbers
Use the Big 6 to help you, some examples of the instructions have been given but you will have to add some more of your own. (REMEMBER these might not be in order)
Declare & Initialise variables
Use all of these declaration statements
declare requiredWeight as integer
declare numberOfPots as integer
declare weight as integer
declare totalUnderWeight as integer
Input(s)
Use similar output then an input statement
output "Please enter in a number of ..."
input number...
Loop structure & increment
Chose which style of iteration you will use you can alter the condition controlled loops to act as a counter controlled loop.
You could use potNumber insteed of i, we tent to use i and then j as counters
Counter Controlled Loop (these loops work a set number of times)
for i = 1 to ???
...
next i
Condition Controlled loops - Exit controlled loop (check the condition after entering the loop)
--------------------------------------- OR -----------------------------------------------------
do repeat
... OR ...
i = i + 1 i = i + 1
while (i <= ???) until (i > ???)
--------------------------------------- OR -----------------------------------------------------
Condition Controlled loops - Entry controlled loop (check the condition before entering the loop)
i = ??
while (i <= ???)
...
i = i + 1
end while
Output(s)
Use this output but another is missing
output "Total number of underweight pots: " + totalUnderWeight
Selection / Errors
Use this selection statement but populate the condition
if (????? < ????) then
...
end if
Calculations / Processing
Use this instruction
totalUnderWeight = totalUnderWeight + 1
----------------------------------- EXAMPLE ---------------------------------
Use this Example to help you understand how to layout your pseudo code
Write an algorithm to input two numbers from a user, add them together and calculate the average.
If the user typed in the numbers 7 and 3, then your output should look something like:
Please enter first number
Please enter second number
The average of your two numbers is 5
Procedure CalculateAverage
Begin
Declare firstNum as Integer
Declare secondNum as Integer
Declare Total as Integer
Declare Average as Real
firstNum = 0
secondNum = 0
Total = 0
Average = 0
Output ‘Please enter first number’
Input firstNum
Output ‘Please enter second number’
Input secondNum
Total = firstNum + secondNum
Average = Total / 2
Output ‘The average of your two numbers is ‘, Average
End
Use the Big 6 to help you and the code listing below (REMEMBER these are not in order)
Declare & Initialise variables
Use all of these declaration statements
declare requiredWeight as integer
declare numberOfPots as integer
declare weight as integer
declare totalUnderWeight as Integer
Input(s)
Use all of these instructions
output "Please enter the weight of the current pot"
input weight
output "Please enter the required weight"
input requiredWeight
output "Please enter the number of pots"
input numberOfPots
Loop structure & increment
Use one style of loop
Counter Controlled Loop (these loops work a set number of times)
for potNumber = 1 to numberOfPots
.....
next potNumber
Condition Controlled loops - Exit controlled loop (check the condition after entering the loop)
------------------------------------ OR -------------------------------------------------
potNumber = 0
do repeat
.... ....
potNumber = potNumber + 1 OR potNumber = potNumber + 1
while (potNumber <= numberOfPots) until (potNumber > numberOfPots)
----------------------------------- OR ------------------------------------------------
Condition Controlled loops - Entry controlled loop (check the condition before entering the loop)
potNumber = 1
while (potNumber < numberOfPots)
....
potNumber = PotNumber + 1
end while
Output(s)
Use all of these instructions
output "Underweight Pot: " , potNumber , potWeight (+ or , can be used to concatinate)
output "Total number of underweight pots: " , totalUnderWeight
Selection / Errors
Use this selection statement but populate it
if (potWeight < requiredWeight) then
...
end if
Calculations / Processing
Use all of this instruction
totalUnderWeight = totalUnderWeight + 1