Unit 1 –
Problem
Solving
ENG. SAMAA AWAD
IGCSE
Computer
Science
Unit 1 Topics
1. Understanding algorithms
2. Creating algorithms
3. Sorting & searching algorithms
4. Decomposition & abstraction
What is an algorithm?
• An algorithm is like a recipe. It's a list of steps that you follow to complete a
task or solve a problem.
• Each step tells you exactly what you need to do, and if you follow the
steps in order, you'll get the result you want.
• Just like following a recipe to bake a cake, using an algorithm helps a
computer know what actions to take to achieve a specific goal.
What are algorithms used for?
• Algorithms are everywhere, helping with everything from simple tasks to
complex decisions
• Algorithms are used for a wide range of purposes across different fields; like
software development, problem solving, data processing, internet searches,
artificial intelligence and machine learning, digital security, and graphics.
If we look at the map as an
example of an algorithm to
reach from a certain location to
a certain destination, here’s
what we notice:
• Steps are clear and
unambiguous
• Steps are in sequence
• Steps can be used again and
again and will provide the same
result
• Steps provide a solution to the
problem
Different paths can exist to the same destination
Successful Algorithms
There are 3 points to consider when deciding if an algorithm is successful or
not
1. Accuracy – it must lead to the expected outcome or result
2. Consistency – it must lead to the same result each time
3. Efficiency – it must solve the problem in the shortest possible time
Algorithms & Programs
• Algorithms and programs are closely related but are not the
same.
• An algorithm is a detailed design of a solution.
• A program is when this design is implemented.
This unit is all about algorithms. Unit 2 is about programs.
Elements of an Algorithm
Each algorithm includes a combination of the following elements:
- Start / End – the algorithm should have a starting point and an end point.
- Steps – the algorithm should have actions or processes that need to be done
in order.
- Decisions – the algorithm might contain decisions or conditions at some point
where a certain path is followed if this condition is true, and another path is
followed if this condition is false. This is also called “conditions”.
- Iterations – the algorithm might contain some steps that need to be repeated
for a certain number of times. This is also called “loops”.
- Input / output data – the algorithm might contain data that needs to be
input from the user in order to make certain calculations or decisions, or
output to the user after obtaining the result of a certain calculation or process.
Displaying an Algorithm
• We carry out many everyday tasks using algorithms because we follow a
set of instructions to achieve a certain result.
• Algorithms can be displayed in many ways
Written Description – English sentences describing each step
Flowcharts – Graphical symbols for a more visual display
Pseudocode – Structured code-like language
Written Description
Give me an algorithm to make a cup of tea
Fill the kettle with water This algorithm has the following
elements:
Turn on the kettle - Start / end
Place tea in a cup - Steps
Add sugar
Wait for water to boil
Pour water into the cup
Stir
Written Description
Write an algorithm for getting to school by bus. It should start with leaving home and end
with arriving at school.
Leave home
This algorithm has the following
elements:
Walk to bus station
- Start / end
Wait for the bus to arrive
- Steps
Get on the bus
Choose and empty seat to sit
Wait for the bus to reach your destination
Get off the bus
Enter your school
Written Description
Write an algorithm for taking 2 numbers from the user and adding them then displaying the
result to the user.
This algorithm has the following
elements:
Input the first number from the user
- Start / end
Input the second number from the user
- Steps
Add the two numbers
- Input / output
Display the result to the user
Written Description
Write an algorithm for taking 2 numbers from the user and adding them then displaying the
result to the user. Also display if this result is odd or even.
This algorithm has the following
Input the first number from the user elements:
Input the second number from the user - Start / end
Add the two numbers - Steps
Display the result to the user - Input / output
If the result is divisible by 2 - Condition
Display “even” Note that in order to know if the result
Otherwise is even or odd, it is checked if it’s
divisible by 2. This is important because
Display “odd”
you need to tell the computer what do
to exactly in every step.
Written Description
Write an algorithm for taking 2 numbers from the user and adding them then displaying the
result to the user. Also display if this result is odd or even. After that, ask the user if he wants
to do another operation.
Input the first number from the user This algorithm has the following
Input the second number from the user
elements:
Add the two numbers - Start / end
Display the result to the user - Steps
If the result is divisible by 2
- Input / output
Display “even”
Otherwise - Condition
Display “odd”
- Loop
Ask the user if he wants to do another operation
Input the user’s choice
If the choice is “yes”
Go to step 1
Flowcharts
• Flowcharts show the algorithm as a diagram.
• It uses graphical symbols to provide a more visual display.
• Special symbols have to be used for every element of the algorithm.
Flowchart Symbols
The start and end of an A step that needs A decision that
algorithm
to be carried out needs to be made
The flow from one
An input or output
step to the other
Flowcharts
Draw a flowchart for an algorithm to make a cup of tea
Fill the kettle Turn on the Place the tea in
START
with water kettle the cup
Pour water into Wait for the
Add sugar
the cup water to boil
Stir END
START
Flowcharts Input the first
number from
the user
Draw a flowchart for
an algorithm for taking
Input the
2 numbers from the second number
from the user
user and adding them
then displaying the
result to the user. Add the two
numbers
Display the
result to the
user
END
START
Flowcharts Input the first Input the
number from second number
the user from the user
Write an algorithm for
taking 2 numbers from
Display the Add the two
the user and adding result to the numbers
user
them then displaying
the result to the user.
Also display if this Is the YES
result Display
result is odd or even. divisible “Even”
by 2?
NO
Display
END
“Odd”
STAR
T
Flowcharts Input the first Input the
second number
number from
the user from the user
Write an algorithm for
Display the Add the two
taking 2 numbers result to the numbers
user
from the user and
adding them then YES
Is the result
Display
displaying the result divisible by
“Even”
2?
to the user. Also
NO
display if this result is
YES
odd or even. After Display Do another
“Odd” operation?
that, ask the user if he
wants to do another NO
operation. END
HOMEWORK
Write a written description 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.
Write a written description 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%).
Write a written description 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
DRAW FLOWCHARTS FOR THE ABOVE 3 ALGORITHMS
HOMEWORK
What does the below algorithm do?
Ask the user to enter a number
Read the number
If the number is greater than 0
Display "The number is
positive“
Else if the number is less than 0
Display "The number is
Modify the above algorithm to display “The number is zero.” if the user entered 0.
negative"
Draw the corresponding flowchart for the modified algorithm.
THANK
YOU