Lesson Objectives Aims To be able to write an algorithm in Pseudo Code Students may need a copy of the pseudo code guide from the exam spec (appendix 5)
Pseudocode This means ‘fake code’ or ‘almost code’ It’s part way between English sentences, and programming code. It is language neutral (it does not follow the syntax of any particular language – anyone should be able to follow it)
Programs Programs, like Lego models, are made up of basic building blocks When you combine these blocks in the right way you can make almost anything
Programs You have already been using these building blocks when you made flow charts I’ll now show you how the basic building blocks and how they relate to flow chart symbols
Flow Chart equivalent: Purpose: Pseudocode Guide Output Pseudo Code Example: print(“Hello”) Flow Chart equivalent: Purpose: To write a message on the screen, ask a question, show a value Output Final Score
num = input(“Enter a number”) Flow Chart equivalent: Purpose: Pseudocode Guide Input Pseudo Code Example: num = input(“Enter a number”) Flow Chart equivalent: Purpose: To get a piece of data from the user or another source Num = “Enter a number”
Pseudocode Guide Selection Pseudo Code Example: if num = 2 then Is Num = 2? Selection Pseudo Code Example: if num = 2 then output (“you guessed it!”) elseif num < 4 then output (“Almost right!”) endif Flow Chart equivalent: Purpose: To make a decision in a program, to have a range of outputs or things happening depending on an input YES NO Is num < 4? YES NO
Things to do after the loop… Pseudocode Guide Iteration Pseudo Code Example: for i = 1 to 10 … next i Second example: while (i != 11) ... i = i + 1 endwhile Purpose: To do something repeatedly i = 0 Is i < 11 YES Do loop actions… NO i = i + 1 Things to do after the loop…
Pseudocode Algorithm Example Write a pseudo code algorithm which: Asks for a test score/mark Outputs: “Pass” if they scored on, or over 50 “Merit if they scored on, or over 70 “Distinction” if they scored on, or over 90 or “Fail” if below 50 mark = input(“Input mark”) If mark < 50 then print(“Fail”) elseif mark < 70 then print (“Pass”) elseif mark < 90 then print (“Merit”) else print(“Distinction”) endif
Task Using the pseudo code guide and your own knowledge: See if you can write an a pseudo code program for each of the challenges They are graded by difficulty – challenge yourself If you find it too hard, try drawing a flow chart first
Task – Example Answer, Low a) The program asks the user to enter the number of hours they have been working for. The system must output: the number of minutes they have been working the number of seconds they have been working. hoursWorked = input(“Enter the number of hours worked”) numberMinutes = hoursWorked * 60 numberSeconds = numberMinutes * 60 print “number of minutes = “ & numberMinutes Print “number of seconds = “ & numberSeconds
Task – Example Answer, Low b) The program asks for a user's age, and their favourite number. The system must output: the two numbers added together the two numbers multiplied together. age = input(“Enter age”) favNum = input(“Enter favourite number”) print (age + favNum) print (age * favNum)
Activity 2 – Example Answer, Low c) The program asks the user to enter two numbers. Output the largest of these two numbers. num1 = input(“Enter the first number”) num2 = input(“Enter the second number”) if num1 > num2 then print(num1 & “ is larger”) elseif num2 > num1 then print(num2 & “ is larger”) else print(“The numbers are the same”) endif
Activity 2 – Example Answer, Medium a) The program that asks the user to enter the number of letters in the alphabet. Output whether they got it correct, or incorrect. answer = input(“How many letters are in the alphabet?”) if answer == 26 then print(“Correct”) else print(“Incorrect”) endif
Activity 2 – Example Answer, Medium b) The program asks the user to enter a number. The program then outputs the 12 times table for that number, e.g. if they enter 5 it displays 5, 10, 15 etc. number = input(“Enter a number”) for x = 1 to 12 print(x * number) next x
Activity 2 – Example Answer, Medium c) The program asks the user for 2 numbers and a symbol (+, -, /, * or ^). The program then outputs the calculation based on the symbol, e.g. if 2, 3 and + were entered it would output 2 + 3 = 5. num1 = input(“Enter a number”) num2 = input(“Enter a number”) symbol = input(“Enter a symbol”) if symbol == “+” then print(num1 + num2) elseif symbol == “-” then print(num1 – num2)
Activity 2 – Example Answer, Medium elseif symbol == “/” then print(num1 / num2) elseif symbol == “*” then print(num1 * num2) elseif symbol == “^” then print(num1 ^ num2) Else print(“Invalid symbol”) endif
Activity 2 – Example Answer, High a) The amount of water in a fish tank, in litres, is calculated using the formulae: (height x width x depth) / 1000. The program must take the height, width and depth as input and output a suitable message e.g. The tank holds 221.6 litres of water. height = input(“Enter the tank height”) width = input(“Enter the tank width”) depth = input(“Enter the tank depth”) water = (height * width * depth) / 1000 print “The tank holds “ & water & “ litres of water”
Activity 2 – Example Answer, High b) The program takes a number as input, and then outputs this many # symbols. number = input(“Enter a number”) for x = 0 to number print ”#” next x
Activity 2 – Example Answer, High c) A program asks the user to input a whole number. If the user enters an invalid input (e.g. a decimal number) then it tells them their input is invalid and asks for the input again. do number = input(“Enter a whole number”) check = number MOD 2 if check == 1 then print(“This is a whole number”) wholeNumber = true elseif check == 0 then else print(“This is NOT a whole number, try again!”) endif until wholeNumber == true
Plenary What are the symbols used in flowcharts? What are the differences between pseudo code and program code?