Unit 1 –
Problem
Solving
ENG. SAMAA AWAD
IGCSE
Computer
Science
Understanding
Algorithms
Pseudocode
• Pseudocode is a structured code-like language that can be used to
describe an algorithm.
• It allows developers to focus on the logic of an algorithm rather than a
specific programming language.
Pseudocode
An algorithm that asks the user to input two numbers, and then outputs the result of adding them
together.
Pseudocode Variables
SEND “Please enter your first number” TO DISPLAY
RECEIVE n1 FROM KEYBOARD
SEND “Please enter your second number” TO DISPLAY
RECEIVE n2 FROM KEYBOARD
SET total = n1 + n2
SEND total TO DISPLAY
Pseudocode Elements
• Store inputs / outputs / calculated values in variables
• Text that needs to be displayed has to be placed in quotation marks ‘ or “
• Arithmetic operators are used for calculations
Variables
• Variables are used in programming to store values used by the program.
• The value stored by a variable can change as the program is running.
• Each variable needs to have a unique descriptive name to make the code easier to read.
• Uppercase, lowercase, numbers or underscore can be used in naming variables.
Ex:
firstName
FIRST_NAME
student_Grade
totalScore
n1
Constants
• A constant is the opposite of a variable.
• It is a place that stores a value that always stays the same.
• Constants are useful for storing fixed information, like the value of pi, the
number of weeks in a year, …
• Like variables, constants should have unique names to identify them.
Ex:
piValue = 3.14
weeksInAYear = 52
Activity
SEND “Please enter your first number” TO DISPLAY
RECEIVE num1 FROM KEYBOARD
SEND “Please enter your second number” TO DISPLAY
RECEIVE num2 FROM KEYBOARD
SET num3 = num1 * num2
SEND num3 TO DISPLAY
Activity
Write a pseudocode for an algorithm that converts temperatures from Celsius to Fahrenheit. The user should enter
a Celsius value, and the algorithm should display the corresponding Fahrenheit value.
Written Description Pseudocode
Input the Celsius temperature value from the user SEND “Please enter the Celsius temperature” TO DISPLAY
Calculate the Fahrenheit temperature value F = (C * 9/5) + 32 RECEIVE cTemp FROM KEYBOARD
SET fTemp = (cTemp * 9 / 5) + 32
Display the Fahrenheit result
SEND “The Fehrenheit temperature is” TO DISPLAY
SEND fTemp TO DISPLAY
Activity
Write a pseudocode for an algorithm that asks the user for scores in 3 subjects, calculates the
average score, and then assigns a grade (A, B, C, D) based on the average, where A (85% and
above), B (75% to 85%), C (65% to 75%) & D (below 65%).
Written Description Pseudocode
Input first grade
SEND “Please enter the three grades” TO DISPLAY
Input second grade RECEIVE g1 FROM KEYBOARD
Input third grade RECEIVE g2 FROM KEYBOARD
RECEIVE g3 FROM KEYBOARD
Calculate average = (first grade + second grade + third grade)/3
SET avg = (g1 + g2 + g3) / 3
If average >= 85
IF avg >= 85
SEND “Your final grade is A” TO DISPLAY
display “A”
ELSE IF avg >= 75 AND avg < 85
If 75 <= average < 85 SEND “Your final grade is B” TO DISPLAY
display “B” ELSE IF avg >= 65 AND avg < 75
If 65 <= average < 75 SEND “Your final grade is C” TO DISPLAY
ELSE
display “C”
SEND “Your final grade is D” TO DISPLAY
Otherwise
display “D”
Activity
Write a pseudocode for an algorithm that asks the user to enter their height (in meters), and weight (in kilograms),
and then calculates the body mass index and display it to the user. BMI = weight / height 2
Written Description Pseudocode
Input height SEND “Please enter the height” TO DISPLAY
Input weight RECEIVE h FROM KEYBOARD
SEND “Please enter the weight” TO DISPLAY
RECEIVE w FROM KEYBOARD
Calculate BMI = weight / (height * height)
SET bmi = w / (h * h)
SEND “Your body mass index is” TO DISPLAY
Display BMI SEND bmi TO DISPLAY
Creating Algorithms
Creating Algorithms
• Algorithms are made up of constructs
• Constructs are small parts that build the algorithm
• Sequence – step by step instructions
• Selection (also called condition or decision) – a choice is made between two
alternatives
• Iteration (also called loop) – a repetition of one or more steps
Creating Algorithms
Give me an algorithm to make a cup of tea
Fill the kettle with water What should the computer do to “wait
for the water to boil??
Turn on the kettle
When creating algorithms, you need to
Place tea in a cup tell the computer exactly what to do.
In this example, the algorithm needs to
Add sugar wait for the water temperature to reach
100.
Wait for water to boil
Pour water into the cup
Stir
Creating Algorithms
Selection and Iteration can be used to avoid ambiguous or unclear steps
Selection and Iteration are two constructs of an algorithm, in addition to Sequence
Activity
Written Flowchart
Description
Input a number
If the number is greater than 10
Display an error
Go to step 1
If the number is equal to 3
Display correct guess
Otherwise
Display incorrect guess
THANK
YOU