What is pseudocode?
Pseudocode is a type of design notation. It literally means 'phoney code'. It is a textual method of design a rough draft of a program that allows program designers to detail the processes and steps (algorithms) to solve a problem. Pseudocode is language independent; this means that the developers avoid using language specific keywords.
Pseudocode is written in a very readable way and as close to English as possible. Whilst it is the case that pseudocode avoids using programming keywords, constructs such as loops, and selection (if statements) must still be made very clear.
SQA Pseudocode
Within both Higher and National 5 Computing Science, you will need to know how to read and interpret pseudocode. The SQA uses its own reference language called the SQA Reference Language (SQARL) which clearly defines how pseudocode questions are formed. SQARL uses keywords such as DECLARE and SEND:
Line 1 SEND "What is the capital of France?" TO DISPLAY Line 2 DECLARE answer AS STRING INITIALLY FROM KEYBOARD Line 3 IF answer == "Paris" THEN Line 4 SEND βCorrectβ TO DISPLAY Line 5 END IF
The above code could be easily translated from pseudocode to Python since it reads like English:
1. print("What is the capital of France?") 2. answer = input() 3. if answer == "Paris": 4. print("Correct")
Of course, since pseudocode does not need to follow a specific syntax, we can write it much closer to English. The following design is for a program that will read the name, prelim mark and coursework mark for a class of 20 pupils from a file. It will calculate a percentage from each of their prelim marks and coursework marks added together. It will then display the name of the pupil with the highest percentage and their percentage:
1. Get results 2. Calculate percentages 3. Find position of pupil with top mark 4. Display pupil with top mark
Refinement
The process of refinement is all about breaking a problem down into smaller chunks to make it easier to solve. For the previous program, the problem can be refined as follows:
1. 1.1. Open marks file 1.2. Start fixed loop for each pupil 1.3. Get pupil name() 1.4. Get prelim mark() 1.5. Get course mark() 1.6. End fixed loop 1.7. Close marks file 2. 2.1. Start fixed loop for each pupil 2.2. percentage() equals (prelim mark() + course mark()) divided by 1.5 2.3. End fixed loop 3. 3.1. top position equals first position 3.2. Start fixed loop from second pupil 3.3. If percentage() is greater than current top percentage Then 3.4. set position as new top position 3.5. End If 3.6. End fixed loop 4. Display "Top pupil is", pupil name(top position), "with", percentage(top position), "percent"
Activity 1
For each of the following Python programs create a pseudocode program of the program.
-
name = "Frank" print(name)
-
username = input("Please insert your name") if username == "John": password = input("Please insert your password") if password = "1234": print("Welcome.") else: print("Your password is incorrect") else: print("No such user")
-
number = generateRandomNumber(1, 100) user_number = input("Please guess a number") while user_number != number: print("Wrong number. Try again!") user_number = input("Please guess again") print("Well done. You have given me the right number!")
Activity 2
For each of the following problems, write pseudocode to solve the problem:
- Print every even number from 1 to 50.
- Ask the user for three sets of marks and find the average of the marks. Display the average on the screen after.
- Ask the user for 10 marks and then figure out which marks is the highest and display it on the screen.