ALGORITHMS
Software Engineering 4 Business
ALGORITHMS
A typical programming task can be divided into
two phases:
Problem solving phase
produce an ordered sequence of steps that describe
solution of problem
this sequence of steps is called an algorithm
Implementation phase
implement the program in some programming language
Program = Algorithm + Data structures
What is an Algorithm?
Steps used to solve a problem
Problem must be • Steps must be
Well defined – Ordered
Fully understood – Unambiguous
by the programmer – Complete
3
Program Development
1. Understand the problem
2. Represent your solution (your algorithm)
Pseudocode
Flowchart
3. Implement the algorithm in a program
4. Test and debug your program
4
Step 1: Understanding the Problem
Input
What information or data are you given?
Process
What must you do with the information/data?
This is your algorithm!
Output
What are your deliverables?
5
Computational problems
A computational problem specifies an
input-output relationship
What does the input look like?
What should the output be for each input?
Example:
Input:
an integer number n
Output: Is the number prime?
Example:
Input:
A list of names of people
Output: The same list sorted alphabetically
6
Algorithms
A tool for solving a well-specified
computational problem
Input Algorithm Output
Algorithms must be:
Correct: For each input produce an appropriate output
Efficient: run as quickly as possible, and use as little
memory as possible
7
Algorithms
Algorithm
A set of unambiguous instructions for solving a
problem or subproblem in a finite amount of time
using a finite amount of data
Why must instructions be unambiguous?
Why must time and data be finite?
8
Properties of an algorithm
INPUT Zero or more quantities are externally supplied.
OUTPUT At least one quantity is produced.
DEFINITENESS each instruction is clear and
unambiguous.
FINITENESS if we trace out the instructions of an
algorithm, then for all cases, the algorithm terminates after
a finite number of steps.
EFFECTIVENESS every instruction must very basic so
that it can be carried out, in principle, by a person using
only pencil & paper.
2. Algorithm Representation
Algorithms can be described in three ways:
Natural language like English: When this way is
chosen care should be taken, we should ensure that
each & every statement is definite.
Graphic representation called flowchart: This method
will work well when the algorithm is small & simple.
Pseudo-code Method: In this method, we should
typically describe algorithms as program, which
resembles programming language constructs
Pseudocode
Pseudocode
A way of expressing algorithms that uses a
mixture of English phrases and indention
to make the steps in the solution explicit
There are no grammar rules in pseudocode
Pseudocode is not case sensitive
11
• Pseudo-code:
<Algorithm name>
// input ? The comment lines “//”
// function?
// Output?
Begin
<data definition>
<actions>
End
Programming Fundamentals --> 12
Ch1. Problem solving
Pseudocode & Algorithm
Example 1: Write an algorithm to
determine a student’s final grade and
indicate whether it is passing or failing.
The final grade is calculated as the
average of four marks.
Pseudocode & Algorithm
Pseudocode:
Input a set of 4 marks
Calculate their average by summing and dividing
by 4
if average is below 50
Print “FAIL”
else
Print “PASS”
Pseudocode & Algorithm
Detailed Algorithm
Step 1: Input M1,M2,M3,M4
Step 2: GRADE (M1+M2+M3+M4)/4
Step 3: if (GRADE < 50) then
Print “FAIL”
else
Print “PASS”
endif
The Flowchart
(Dictionary) A schematic representation of a sequence of
operations, as in a manufacturing process or computer
program.
(Technical) A graphical representation of the sequence
of operations in an information system or program.
Information system flowcharts show how data flows from source
documents through the computer to final distribution to users.
Program flowcharts show the sequence of instructions in a
single program or subroutine. Different symbols are used to
draw each type of flowchart.
The Flowchart
A Flowchart
shows logic of an algorithm
emphasizes individual steps and their
interconnections
e.g. control flow from one action to the next
Flowchart Symbols
Basic
Name Symbol Use in Flowchart
Oval Denotes the beginning or end of the program
Parallelogram Denotes an input operation
Rectangle Denotes a process to be carried out
e.g. addition, subtraction, division etc.
Diamond Denotes a decision (or branch) to be made.
The program should continue along one of
two routes. (e.g. IF/THEN/ELSE)
Hybrid Denotes an output operation
Flow line Denotes the direction of logic flow in the program
Example
START
Step 1: Input M1,M2,M3,M4
Step 2: GRADE (M1+M2+M3+M4)/4
Input
M1,M2,M3,M4
Step 3: if (GRADE <50) then
Print “FAIL”
else
GRADE(M1+M2+M3+M4)/4 Print “PASS”
endif
N Y
IS
GRADE<50
PRINT PRINT
“PASS” “FAIL”
STOP
Control Structures
Structures that control how the program
“flows” or operates, and in what order
Sequence
Decision Making
Looping
20
Sequence
One step after another, with no branches
Already wrote one for “Weekly Pay”
problem
What are some real life examples?
Dialing
a phone number
Purchasing and paying for groceries
21
Decision Making
Selecting one choice from many based
on a specific reason or condition
If something is true, do A … if it’s not, do B
What are some real life examples?
Walkingaround campus (construction!)
Choosing where to eat lunch
22
Decision Making: Pseudocode
Answer the question “Is a number positive?”
Start with a plain English description
1. Display "Enter the number: "
2. Get the number (call it num)
3. If num < 0
4. Display "It is negative"
5. Else
6. Display "It is positive"
23
Decision Making: Flowchart
Display “Enter Get the
Start the number: ” number
TRUE FALSE
num < 0
Display Display
“It is negative” “It is positive”
End
24
Looping: Pseudocode
Write an algorithm that counts from 1 to 20
Start with a plain English description
1. Set num = 1
2. While num <= 20
3. Display num
4. num = num + 1
5. (End loop)
25
Looping: Flowchart
Start num = 1
There’s an error in this
flowchart… do you see
it?
TRUE Display num = num +
num >= 20 num 1
FALSE
End
26
Looping: Flowchart
Start num = 1
This type of error is called a
“bug,” and finding and fixing
bugs is called “debugging”
TRUE Display num = num +
num <= 20 num 1
FALSE
End
27
Flowchart – example 2
Begin
Read age
YES Age > 55? NO
print “Pencen” print “Kerja lagi”
End
Principles of Programming - NI July
2005 28
Implement the algorithm
Verify that the algorithm works source of
errors
• Many errors made in:
- analyzing the problem,
- developing an algorithm, and/or
- coding the algorithm
Programming Fundamentals --> 29
Ch1. Problem solving
Implement the algorithm
• Errors are of three types:
. syntax errors
. run-time errors
. logic errors
• Syntax errors: detected by the program compiler
. source code does not conform to one or more of
program’s grammar rules
. examples of syntax errors: undeclared variable, …
• Often one mistake leads to multiple error messages –
can be confusing
Programming Fundamentals --> 30
Ch1. Problem solving
Syntax Errors
“Syntax” is the set of rules followed by a
computer programming language
Similar to grammar and spelling in English
Examples of Python’s syntax rules:
Keywords must be spelled correctly
True and False, not Ture or Flase or Truu
Quotes and parentheses must be closed:
("Open and close")
31
Syntax Error Examples
Find the syntax errors in each line of code
below:
1 prnit("Hello")
2 print("What"s up?")
3 print("Aloha!)
4 print("Good Monring")
32
Syntax Error Examples
Find the syntax errors in each line of code
below:
1 prnit("Hello")
2 print("What"s up?")
3 print("Aloha!)
4 not actually
print("Good Monring")
a syntax
error
33
Implement the algorithm
• Run-time errors. detected and displayed by computer during
execution.
- Occur when program directs computer to perform illegal
operation. Example: int x=y/0;
- will stop program execution and display message
• Logic errors.
- caused by faulty algorithm
- sign of error: incorrect program output
- cure: thorough testing and comparison with expected
results
A logic error is referred to as a bug, so finding logic errors is
called debugging.
Programming Fundamentals --> 34
Ch1. Problem solving
Exercise
Write an algorithm that asks a user for their
grade, and tells them their letter grade.
A: 100 - 90 C: <80 - 70
F: <60 - 0
B: <90 - 80 D: <70 – 60
Write an algorithm that asks a user for their
name, then responds with “Hello NAME”
You can use a flowchart or pseudocode
35
Examples/Application of
algorithms in software projects
E-commerce Platforms: Use recommendation algorithms to suggest
products. Example: Jumia, Ali express
Navigation Apps: Use pathfinding algorithms to calculate
routes.Example: Waze, Google maps
Social Media: Use graph algorithms to suggest friends or detect
communities.
Databases: Use indexing and search algorithms to retrieve data
quickly.
Gaming: Use AI algorithms for enemy behavior or procedural content
generation
Algorithmic decision making (ADM) – Decision making using
algorithms
Best Practices
Start Simple: Begin with a straightforward solution and
optimize only if necessary.
Use Libraries: Leverage existing libraries (e.g., NumPy,
TensorFlow) for common algorithms.
Document: Clearly document the algorithm's purpose,
logic, and implementation.
Test Thoroughly: Validate the algorithm with various
inputs and edge cases.
Stay Updated: Keep up with advancements in algorithms