Algo 2 HWEEEEEEEEEEE
Algo 2 HWEEEEEEEEEEE
ALGORITHMS
Introduction
■ Problem
■ Problem Solving
Algorithms
■ Implementation phase
– implement the program in some programming language
Algorithms
■ Unambiguous
■ Executable
■ Ordered
Algorithms
■ Start - End
■ Goto
■ Set, Initialize
■ Read, Get
■ Print, Write, Display
Assignment
– C=A+B
– R=R+1
Mathematical Operators
■ Sequence
■ Branching (Selection)
■ Loop (Repetition)
Relational Operators
■ Determine wheter the the student passed or failed according the entered GPA. To
pass, GPA must be greater than or equal to 60.
var: gpa
1. Start
2. Get gpa
3. If gpa>=60
print "Passed"
else
print "Failed"
4. End
Algorithms
■ Find the greater of two numbers
var: number1,number2,max
1. Start
2. Get number1
3. Get number2
4. If number1>number2
max=number1
else
max=number2
5. Print max
6. End
Logical Operators
var: a,b,c,max
1. Start
2. Get a,b,c
3. If a>=b and a>=c then max=a
4. If b>=a and b>=c then max=b
5. If c>=a and c>=b then max=c
6. Print max
7. End
Structures
■ Sequence
■ Branching (Selection)
■ Loop (Repetition)
Algorithms
■ Find odd numbers between 1 to 10
var: counter
1. Start
2. counter=1
3. Print counter
4. counter=counter+2
5. If counter<=10 then go to step 3
6. End
Algorithms
■ Find odd numbers between 1 to 10
var: counter
1. Start
2. counter=1
3. If counter%2=1 then print counter
4. counter=counter+1
5. If counter<=10 then go to step 3
6. End
Flowchart
■ Flowchart is a diagram which visually presents the flow of data through processing
systems.
■ Once the flowchart is drawn, it becomes easy to write the program in any high level
language.
Flowchart Symbols
■ Terminal
■ Flow lines
■ Process
■ Input/output
■ Used for any I/O operation. Indicates that the computer is to obtain data or output
results.
Input Output
Flowchart Symbols
■ Decision
■ Connector
■ Allows the flowchart to be drawn without intersecting lines or without a reverse flow.
Flowchart Symbols
■ Predefined process
END
Exercise
■ Find the greater of two numbers
var: number1,number2,max
1. Start
2. Get number1
3. Get number2
4. If number1>number2
max=number1
else
max=number2
5. Print max
6. End
Homework
■ Using flowcharts, write an algorithm to read three numbers then display the
smallest.