Day 2 Algorithm Design
Day 2 Algorithm Design
• Pseudocodes
• Flowcharts
What is Algorithm?
• inputs – the data used by the system that needs to be entered while the
system is active
• processes – the tasks that need to be performed using the input data and
any other previously stored data
• outputs – information that needs to be displayed or printed for the users of
the system
• storage – data that needs to be stored in files on an appropriate medium for
use in the future (optional)
Writing Pseudocode
• Pseudocode is fairly flexible because it is a planning tool, and not the final product.
• Therefore, for example, you might prefer any of the following:
• Instead of start and stop, some pseudocode developers would use the terms begin
and end.
• Instead of writing input myNumber, some developers would write get myNumber or
read myNumber.
• Instead of writing set myAnswer = myNumber * 2, some developers would write
calculate myAnswer = myNumber times 2 or compute myAnswer as myNumber
doubled.
• Instead of writing output myAnswer, many pseudocode developers would write
display myAnswer, print myAnswer, or write myAnswer.
Pseudocode Representation of Adding Two
Numbers Problem
Pseudocode Representation of Adding Two
Numbers Problem
• start
input firstNumber and secondNumber
set myAnswer = firstNumber+secondNumber
output myAnswer
stop
Another Writing Style
Arithmetic Operator
start
input myNumber1
set myAnswer1 = myNumber1 * 2
input myNumber2
set myAnswer2 = myNumber2 * 2
output myAnswer1
output myAnswer2
stop
Repeating Instructions
Problem :Doubling 10, 000 numbers
Inefficient pseudocode for program
that doubles 10,000 numbers
Solution : For Loop Structure
set myAnswer=0
for count = 0 to 3 step 1
input myNumber
set myAnswer = myNumber+ myAnswer
endfor
output myAnswer
Assignment
An alarm app
The alarm app can be decomposed into:
• inputs – time to set the alarm, remove a previously set alarm time, switch
an alarm off, press snooze button
• processes – continuously check if the current time matches an alarm time
that has been set, storage and removal of alarm times, management of
snooze
• outputs – continuous sound/tune (at alarm time or after snooze time expired)
• storage – time(s) for alarms set.
Pseudocode for “Setting Up Alarm Program”
start
input new time
set the alarm time
continuously check if the current time matches an alarm
time that has been set
output sound
stop
Pseudocode for “Remove Previous Alarm”
start
select previous time
remove the alarm time
output successfully remove selected alarm time
stop
Pseudocode for “Remove Previous Alarm”
start
input select previous time
remove the alarm time
output successfully remove selected alarm time
stop