0% found this document useful (0 votes)
14 views18 pages

Chapter-1 Algorithm

The document provides an overview of algorithms, including their representation through pseudocode and flowcharts. It explains key concepts such as variables, constants, control structures (sequence, selection, iteration), and provides examples of pseudocode and flowcharts for various scenarios. Additionally, it includes exercises for developing algorithms related to BMI calculation and thermostat control.

Uploaded by

Alia Khin
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views18 pages

Chapter-1 Algorithm

The document provides an overview of algorithms, including their representation through pseudocode and flowcharts. It explains key concepts such as variables, constants, control structures (sequence, selection, iteration), and provides examples of pseudocode and flowcharts for various scenarios. Additionally, it includes exercises for developing algorithms related to BMI calculation and thermostat control.

Uploaded by

Alia Khin
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

IGCSE, Unit – 1 Algorithms

Notes, Exercises

1
1. Notes
There are two ways of representing algorithms:

• Pseudocode
• Flowchart

2. Functions of each of the seven Arithmetic operators

2
2. Difference between variable and constant
Variable
Descriptive of the data it will hold. (E.g. distance or length)

Constant
Container that holds a value that never changes.
Like variables, constants have unique identifiers.

Control Structures
There are basically 3 types of control structures such as:
1. Sequence- In this the statements are executed one after another in proper
order.
2. Selection (Conditional)– It allows a program to take up alternative paths of
execution. It consists of If-Then or If-Then-Else control structure.
3. Iteration (Looping)– It is a control structure that repeats a sequence of
instructions until a particular condition is met.
Examples of types of loops are the “While” loop and “For” loop.

1. Sequence Example

3
Pseudo Code

Ex. Write an algorithm expressed in pseudocode that receive three numbers


from the keyboard, then calculates and displays the average.
Pseudo Code

2. Selection Example

SEND 'How old is your dog?' TO DISPLAY


RECEIVE human_years FROM KEYBOARD

SEND 'How old are you?' TO DISPLAY


RECEIVE user_age FROM KEYBOARD

SET dog_years TO human_years * 7


SEND 'In dog years, your dog is aged ' & dog_years TO DISPLAY

4
IF dog_years > user_age THEN
SEND 'Your dog is older than you!' TO DISPLAY
ELSE
SEND 'Your dog is not older than you.' TO DISPLAY

Flowchart

5
Example. A student is creating a guessing game. A player has to enter a number
no greater than 10. If it is too high, they are informed that they have made an
error. But if it is within the range 1 to 10, they are told whether or not they have
guessed the correct number. (Assume that the correct number is 3.)

Pseudo Code

6
Example.

7
3. Iteration Example

A For loop is used when code needs to be repeated a specified number of times.

FOR <Identifier> = Value1 TO (Value2)


<Statements>
NEXT
SET count TO 0
SET sum TO 0
FOR count=0 TO 10
SEND 'Enter Number' TO DISPLAY
RECEIVE num FROM KEYBOARD
SET sum TO sum+number
NEXT count
SEND 'Sum=' & sum TO DISPLAY

FOR EACH value in Values list


<Statements>
END FOR

SET sum TO 0
SET numbers TO [10,20,30,40,50]
FOR EACH number FROM numbers DO
SET sum TO sum+number
END FOREACH
SEND 'Sum=' & sum TO DISPLAY

8
2. While
While loops are also known as pre-condition loops. This is because a while loop will repeat forever
WHILE a set condition is true.

WHILE <Condition> DO

<Statements>

END WHILE

SET count TO 0
SET sum TO 0
WHILE count<=10 DO
SEND 'Enter Number' TO DISPLAY
RECEIVE num FROM KEYBOARD
SET sum TO sum+number
SET count TO count+1
END WHILE
SEND 'Sum=' & sum TO DISPLAY

3. REPEAT…UNTIL
A REPEAT UNTIL loop is also known as a Post-condition loop. This type of loop will repeat forever
UNTIL a condition becomes true.

REPEAT
<Statements>
UNIL <Condition>
SET count TO 0
SET sum TO 0
REPEAT
SEND 'Enter Number' TO DISPLAY
RECEIVE num FROM KEYBOARD

9
SET sum TO sum+number
SET count TO count+1
UNTIL count<10
SEND 'Sum=' & sum TO DISPLAY

Eg.
REPEAT
SEND "Please enter the password"
RECEIVE password FROM KEYBOARD
UNTIL password="secret"
Exercises:
1. Develop an algorithm using a flowchart that asks the user to enter their height
(in metres) and weight (in kilograms) and displays their body mass index (BMI).
The formula for calculating BMI is weight/height2.

2. Develop a pseudo code (expressed as a flowchart) to control the heating in a


house. A thermostat monitors the temperature within the house. During the
week the temperature should be 20°C between 06.00 and 08.30 in the morning
and between 17.30 and 22.00 at night. At weekends it should be 22°C between
08.00 and 23.00. If the temperature in the house falls below 10°C at any time
the boiler is switched on.

10
3. Develop a pseudo code for the following flowchart.

11
Ref:
[Link]
[Link]

Old Question

12
13
Question

14
15
2.

16
Python code
-----------------
from random import randint
counter=1
answer=randint(1,10)
guess=0
guess=int(input("Enter a number from 1 to 10"))
while guess!=answer:
counter +=1
if(guess > answer): print (guess, " was too hight")
else: print(guess, "was too low. try again")
guess=int(input("Guess gain"))
print ("you guesses ", guess, "in", counter, "Guesses")

3.

17
18

You might also like