Writing Algorithm
Writing Algorithm
Eleanor B. De la Vega
Subject Teacher
Designing Solutions to
Computer Based Problems
■Analyze the problem
■Design a solution/program
■Code/enter the program
■Test the program
■Evaluate the program,
■Analyze the problem – when you analyze the
problem. You look at the problem in front of
you and decide what you need to do to solve
the problem.
■Design a solution/program – program
design is the most important in producing a
computer program. This is the stage where
you decide how your program will work to
meet the decisions you made during
analysis.
Creating an Algorithm
■Stepping
■Looping
■choosing
Stepping Out
Problem: To find the sum of two numbers
Algorithm: 1. Add the two numbers
2. Write down the answer
Problem: To change the temperature Fahrenheit
to centigrade
Algorithm: 1. Subtract 32
2. Multiply the result in (1) by 5
3. Divide the result in (2) by 9
4. Write down the answer
Exercise
■Find the product of two numbers (this
means to multiply the two numbers)
■Find the difference between the two
numbers
Problem
❑ Find the volume of a cone given its
diameter and height.
Formula:
Radius = half a diameter
Algorithm
1. Start
2. Divide the diameter by 2 to give the radius
3. Multiply 3.14 by the radius
4. Multiply the result in (2) by the radius
5. Multiply the result in (3) by the height
6. Divide the result in (4) by 3 to give the volume
7. Write down the answer
8. End
Exercise
■Create an algorithm that will read
the two sides of a rectangle and
calculate its area.
Exercise
Algorithm
1. Start
2. Define the width of the rectangle.
3. Define the Height of the rectangle.
4. Define Area of the rectangle.
5. Calculate the area of the rectangle by multiplying the width
and height of the rectangle.
6. Assign the area of the rectangle to the area variable.
7. print the area of the rectangle.
8. End
Exercise
Going loopy! (looping)
Three different types of loops:
1)The REPEAT UNTIL loop
2)The WHILE loop
3)And the FOR loop
1. Repeat Until Loop
■This type of loop keeps on
carrying out a command or
commands UNTIL a given
condition is satisfied, the
condition is given with the UNTIL
command.
Example
Problem: To wash a car
Algorithm: 1. REPEAT
2. wash with warm soapy
water
3. UNTIL the whole car is
clean
Exercise
1. To keep delivering the news
paper until your bag is empty.
2. To paint a wall.
3. To complete a set of multiple
choice questions.
2. While Loop
■ In this type of loop the condition
is given a long with the WHILE
command and the loop keeps on
carrying out a command or
commands until an END WHILE
command is given.
Example
Problem: To ask for number more than 10, then
stop when such a number is given.
Algorithm: 1. WHILE number given is less than 10
2. ask for a number more than 10
3. END WHILE
Example
Example
Exercise
1. Keep asking for a number less than 5 and stop
when a number less than 5 is given.
2. Keep asking for password, and to give a
message “accepted” when the correct
password is given.
3. To ask for the length of one side of a square,
then keep asking for guesses for the area of
the square until the correct area is given (think
about stepping and looping).
3. For Loop
■ In this type of loop the number of
times it is repeated must be
known. The commands to be
repeated are sandwiched between
the FOR and END FOR commands.
Example
Problem: To print the numbers from 10 to 20
Algorithm: 1. FOR number = 10 to 20
2. Print number
3. END FOR
Example
Problem: To print the 13 times table from 1 x 13 to
10 x 13
Algorithm: 1. FOR z = 1 to 10
2. Print 13 x z
3. END FOR
Example
Example
Choosing
Choosing
Looping
4. IF number is biggest than largest
5. THEN largest = number
6. END WHILE
7. Print largest
Exercise
Problem: To count the number
of red boxes in a set of red,
white and blue boxes.
Algorithm:
1. number = 0
2. WHILE still boxes to check
3. Read color
4. IF color is red
5. THEN number = number + 1
6. ELSE number = number
7. END WHILE
Thank
you!