0% found this document useful (0 votes)
3 views

Day 2 Algorithm Design

python programming tutorial series Day 2 Algorithm

Uploaded by

Chit Su
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Day 2 Algorithm Design

python programming tutorial series Day 2 Algorithm

Uploaded by

Chit Su
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 27

Algorithm Design

• Pseudocodes
• Flowcharts
What is Algorithm?

• An algorithm is a step-by-step procedure for solving a specific problem or


accomplishing a specific, finite goal.
• An algorithm is a plan in which logical thinking are structured to process the
formatted user inputs to form a successful program for desired output.
• An algorithm can be described in not only pseudocode but also flowchart
style.
Logic Planning Tools for Programmer

• When programmers plan the logic for a solution to a programming


problem, they often use one of two tools:
• pseudocode (pronounced “sue-doe-code”) or
• flowcharts.
• Pseudocode is an English-like representation of the logical steps it takes to solve a
problem and it does not necessarily follow all the syntax rules of any specific
language
• A flowchart is a graphic representation.
Pseudocode for Algorithm
Flowchart for Algorithm
What is pseudocode in
programming language?
Pseudocode: Decomposing A Problem

• 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

• The following five statements constitute a pseudocode representation of a


number-doubling problem:
• start
input myNumber
set myAnswer = myNumber * 2
output myAnswer
stop
Writing Pseudocode (Cont’d)

• 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

• Arithmetic Operator is the mathematical calculation for two operands (two


inputs).
Problem :Doubling 2 numbers

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

for loop ControlVariable = initialValue to finalValue step stepValue


do something
endfor
For Statement

• . In a for statement, a loop control variable is:


• Initialized
• Evaluated
• Incremented
Problems: Hello 3 times

for count = 0 to 3 step 1


output "Hello"
endfor
Problems: Hello 10000 times

for count = 0 to 10000 step 1


output "Hello"
endfor
Problems: double 10000 number

for count = 0 to 10000 step 1


input myNumber
set myAnswer = myNumber * 2
output myAnswer
endfor
Problems: Adding 3 numbers

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

You might also like