02_Algorithms_I
02_Algorithms_I
• Problem Solving
Algorithms
• Problem solving phase
• produce an ordered sequence of steps that describe solution of
problem
• Implementation phase
• implement the program in some programming language
Algorithms
• The word is derived from the phonetic pronunciation of the last name
of Abu Ja'far Mohammed ibn Musa al-Khowarizmi.
• An algorithm is a sequence of steps to solve a particular problem
• An algorithm is an ordered set of unambiguous steps that produces a
result and terminates in a finite time
Algorithms
• It is a step-by-step representation of a solution to a given problem
• An algorithm uses a definite procedure.
• It is not dependent on any programming language.
• Every step in an algorithm has its own logical sequence.
Properties of Algorithms
• Finiteness: An algorithm must always terminate after a finite number of
steps.
• Definiteness: Each step of an algorithm must be precisely defined. Also the
actions are defined unambiguously for each activity in the algorithm.
• Input: Any operation you perform need some beginning value/quantities
associated with different activities in the operation.
• Output: One always expects output/result (expected value/quantities) in
terms of output from an algorithm.
• Effectiveness: Algorithms to be developed/written using basic operations.
Algorithms
• Define your algorithms input
• Define the variables
• Outline the algorithm's operations
• Output the results of your algorithm's operations
Algorithms
• Unambiguous
• Executable
• Ordered
Algorithms
• C=A+B
• R=R+1
Mathematical Operators
Operator Meaning Example
+ Addition A+B
- Substraction A-B
* Multiplication A*B
/ Divison A/B
^ Power A^B
% Reminder A%B
Algorithms
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 Goto 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 Goto Step 3
6. End
Flowchart
• Flowchart is a diagram which visually presents the flow of data
through processing systems.
• Used for any I/O operation. Indicates that the computer is to obtain
data or output results.
Input Output
Flowchart Symbols
• Decision
3. Get number2
4. sum = number1 + number2 PRINT sum
5. Print sum
6. End
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.