CSC102Lecture4_2024
CSC102Lecture4_2024
LECTURE 4
Sakpere A.B.
Department of Computer Science,
University of Ibadan.
Today’s Lecture:
Problem Solving and Algorithm...
Outline
• Problem
• Problem Solving
• Algorithms
• Pseudocode
Brainstorm Session
• What do you understand by the word
“problem”?
Tidy your
room:
Pick up all the stuff
on your floor and
put it away.
Make your bed.
Vacuum the floor.
An algorithm for Snakes and Ladders
Snakes and Ladders:
• This game is for 2 - 4 players.
• The first player to hit the last square wins and the game
ends.
• To start, each player takes his/her turn to roll the dice
and the one with the highest throw begins.
• Players then take turns to roll the die. They each move
the number of squares indicated by the die.
• If a player lands on a square depicting the bottom of the
ladder, she moves up to the square containing the top of
the ladder.
• If a player lands on the square containing the head of a
snake, he slides down to the square containing the tail of
the snake.
Is this an algorithm?
To find the sum of three numbers, a, b, c:
d=a+b+c
Ponder…
• If humans need an algorithm for virtually
every task, can a computer require less or do
without it?
• It must be finite
• It must generate one or more output after
processing
Elements of Algorithm
Elements of Algorithm
• Sequence
o Each step is followed by another step
• Selection
o A choice may be made among alternatives
• Iteration
o A set of steps may be repeated
Print Print
A B
Selection Structure
• Let's see an example from Scratch
Selection Structure
• Let's see an example from Scratch
Example on Selection
©
Cop
yrig
ht
2000
Example of Iteration
An Example from Scratch
Example of Iteration
1. Fill kettle with water
2. Plug Kettle
3. While water has not boiled,
Wait 60 seconds
4. Turn kettle off
5. Remove plug from power outlet
Algorithm to Boil Water in Kettle
(Comprehensive)
1. take the lid off kettle
2. If there is enough water in the kettle , go to step 7
3. Put kettle under tap
4. Open tap
5. While kettle is not full,
Wait
6. Close tap
7. Replace lid on kettle
8. Plug kettle into power outlet
9. Turn kettle on
10. While water has not boiled,
Wait 5 seconds
11. Turn kettle off
12. Remove plug from power outlet
Algorithm to Boil Water in Kettle
1. take the lid off kettle
2. If there is enough water in the
kettle , go to step 7
3. Put kettle under tap
4. Open tap
5. While kettle is not full,
Wait
6. Close tap
7. Replace lid on kettle
8. Plug kettle into power outlet
9. Turn kettle on
10. While water has not boiled,
Wait 5 seconds
11. Turn kettle off
12. Remove plug from power outlet
Will this algorithm work for this kettle? Why or why not?
Representing Algorithms
• Algorithm can be technically expressed as:
o Pseudocode
o Flowchart
Pseudocode
• Pseudocode is an informal high-level
description
• Uses same structure as programming
language
• Usually written before programming and
intended for humans
Vocabulary used in Pseudocode
Input/output
• Read – reads a value into a variable. Usually used to
prompt user for a value
• Display – outputs a variable or constant usually to a
terminal
• Variable is a placeholder
Initialization & Computation
A simple Example
• Write an algorithm that will calculate the area
of a rectangle
o Step 1: Enter the length of the rectangle
o Step 2: Enter the breadth of the rectangle
o Step 3: Multiply length by breadth to get the area
o Step 4: Output the area of the rectangle