Tutorial 0
❓️
Common Pseudocode Tasks & FAQs
0478 IGCSE
There are many common tasks you will have to complete in exams - this page is essentially a repository of many of these key tasks you are expected to be able to perform
If there is something else you'd like me to add to this page, please contact me
More details for specific aspects of pseudocode you have to know can be found on the tutorials pages
1
Input into an Array
Often, we are required to ask the user to input data into an array
This is fairly simple to do - simply loop over all elements in the array and input at that particular index
In the example below, the user is asked to select their football team - both positions and players - this team sheet is then output to the user at the end
2
Find Min, Max, Sum & Average of Inputs
Many IGCSE/O-Level 15 markers and some A-Level questions require us to calculate the minimum, maximum, sum and mean of all numbers in an array
Looking at the code below, note:
- We assign min & max as being the first element - this is since, e.g. if giving min a huge default value, then if all values happened to be larger than this, we would think the min was this default value we initially assigned it - same for max, if we chose a small/negative initial value, but the real values all happened to be less than it
- We then loop through all elements in the array
- Min: if the current element is smaller than the current minimum, then set this current element as the new minimum
- Max: if the current element is bigger than the current maximum, then set this current element as the new maximum
- Sum: add each element to the sum
- Mean: divide the sum by the number of elements
- Note: in this example, we could have also assigned sum to be the first element, then started our loop from element 2
3
Find Min, Max, Sum & Average of Inputs
The code is effectively the same as that above, but here we should ideally assign the first input to be the min/max/sum - we could do this by having the first input before the loop, then only looping from 2 to n, or we could have a Boolean value (flag) representing whether we have got the first input yet
4
Linear Search
Often we want to search to see whether a specific value exists in an array - we can use linear search to do that:
- Initialise an index variable to point to first element in array
- Loop through array element-by-element
- If current element is the value we are searching for, then update flag/return TRUE if in a function etc
- If item hasn't been found after looping through all elements in the array, then we know the item doesn't exist
Note: we can also write this as a function, returning TRUE if we find the item, while returning FALSE is we have looped through the entire array and not found the item
5
1D Bubble Sort
In order to sort an array in either ascending or descending order, we can use bubble sort:
- Assume array is unsorted and loop to the n-1 position (e.g. 1 to 4, if 5 elements) in the array on the first iteration
- Compare elements pair by pair - if in the wrong order (i.e. if current element bigger than next element for ascending order...or current element smaller than next for descending order), then swap them by making use of a temporary variable
- If elements are not sorted after looping over all pairs, then go back to the start
More detailed comments can be seen in the code
Note: when we swap, we need the temporary variable - this is since if we do a <-- b, both variables now have the same value at this point - b <-- a will be redundant - hence why we need to move one value into a temporary variable before we update it, then assign this temporary variable value to the other variable. Drawing a picture with arrows representing the assignments between 3 variables can help with understanding.
Note: to sort in descending order, simply change the > in the IF statement to a <
2D Bubble Sort
The logic for 2D bubble sort is the same as for 1D bubble sort - the only difference is that we need to swap each column individually inside the IF statement
6
Round to n Decimal Places
We can use the ROUND function to round to a given number of decimal places, where the 2nd parameter is the number of decimal places to round to
7
Force Round Up or Down
We can use the ROUND function then add an offset of +/- 0.5 to round up or down to the nearest integer respectively
Let's look at an example - take a number like 7.0 - if we use ROUND(7.0, 0), that will be rounded to 7 - however, if we add 0.5, we effectively get ROUND(7.50, 0) which hence rounds up to 8
8
Generate Random Number in a Range
Returns a real number between 0-1 inclusive
Note: we can multiply and add an offset to generate a random number within a range as shown below
The offset should be the lower bound of our range - then we should multiply our random value by the range. For example, if we want a random number between -10 and 10, the smallest number in the range is -10 and the range is 20
If we want to make this an integer, we will have to use ROUND
9
For Loop - Custom & Negative Step
By default, when we create a for loop, there is an implied step of 1 - i.e. the for loop variable increments by 1 each iteration of the loop
We can change this via the STEP keyword as shown below:
10
Loop Through Characters in a String
We can create a simple for loop from 1 to the length of the string, then get the current character with SUBSTRING
11
Check if Character is Uppercase, Lowercase, Digit or Other
To check if a character is in a given range, we can use either IF or CASE statements
This works because it's simply the ASCII code of the characters that are compared - '0' = 48, 'A' = 65 and 'a' = 97
12
Length Check
Suppose customers should enter their credit card number - for this simple example, let's assume all should be 16 digits
Note: for credit card/telephone numbers, we often want to store them as strings, not integers - this is so leading zeros aren't remove, so that additional characters can be allowed in telephone numbers such as +(123) 456-789 etc
13
Range Check
Assume you have a pizza delivery business - customers should be able to order between 1 and 50 pizzas
14
Format Check
The specific code for a format check will depend on the format you are checking - let's say for our example, a password has to:
- Start with an uppercase letter
- Be at least 8 characters
- Contain at least 1 lowercase character, number and special character
- Can't start and end with the same character
15
Odd or Even
We can simply MOD by 2 - if the answer is 0, the number is even, if the answer is 1, it's odd
This same logic can be used to check if a number is a multiple of anything - for example, if MOD(n, 5) = 0, then n would be a multiple of 5
16
Check if Number is Integer
There are at least 3 ways of checking if a number is an integer or not
17
Declare, Populate & Output 2D Array
Note: we usually loop through a 2D array with rows being the outer loop, the columns being the inner loop - this effectively loops left to right, top to bottom - the same way books in many languages like English are read
18
Display Menu
To display a menu, we can simply output a list of options along with a "rogue value" to stop the program - we loop until that rogue value is entered
19
Procedures
A reminder - a procedure is a group of code we can use (call) via the CALL keyword - we can optionally have parameters which will have different arguments on different calls of the procedure, hence modifying the behaviour of that particular procedure call - like below, where we call the same procedure with 3 different syllabus codes
20
Functions
Like procedures, functions are groups of code that can be used at different points throughout the program
There are two differences, however:
- Functions have to return a value
- We don't need the CALL keyword when calling a function - since we can use the function directly in another statement or expression like output, an if statement, calculation etc
21
Reading from Files
For IGCSE/O-Level, you should only have to read a single line of a file - if they ask you to read multiple lines of a file, they will tell you the number of lines to read/that the file contains
For example, Jokes.txt contains 36 lines, hence the following code will read and output all 36 lines
Note how we are first required to open the file in read mode and close it after use too
22
Writing to Files
Suppose we want to keep writing names of students in your class to a file, until the user enters nothing