lec2_308
lec2_308
Algorithms
CSE308
LECTURE 2: PROBLEM-SOLVING
Analysis
Problem
specification
Design
Algorithm
Implementation
Program
Compilation
Executable
(solution)
Analyze the Problem
Design
Developing the algorithm that solves the
problem
the
• Identify alternative ways to solve the problem
solution
• Select the best way to solve the problem from the list
of alternative solutions
• List instructions that enable you to solve the problem
using selected solution
Programming errors are called bugs and the process of tracking them down
and correcting them is called debugging.
Three kinds of errors can occur in a program:
1. Syntax errors
▪ A program can only be executed if it is syntactically correct; otherwise, the process
fails and returns an error message.
▪ syntax refers to the structure of a program and the rules about that structure.
2. Runtime errors
▪ So called because the error does not appear until you run the program.
▪ These errors are also called exceptions because they usually indicate that something
exceptional (and bad) has happened.
3. Semantic errors
▪ If there is a semantic error in the program, it will run successfully, in the sense that the
computer will not generate any error messages, but it will not do the right thing. It will
do something else. Specifically, it will do what the programmer told it to do.
▪ But the written program does not solve the original problem. The meaning of the
program (its semantics) is wrong.
Testing the Algorithm
• Important distinction
➢ Mathematics
We test the answer
➢ Programs
We test the process
11
Testing the Algorithm
• It is a Continuous process.
* Technical documentation
It is addressed to the programmer who may modify the program later.
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
15
Algorithms
❑ It must be correct
❑ It must be finite (in terms of time and size)
❑ It must terminate
❑ It must be unambiguous
▪ Which step is next?
❑ It must be space and time efficient
Problem
Algorithm: A sequence
of instructions describing
how to do a task (or
process)
C++ Program
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
Statement 1
Statement 2
Statement 3
:
Flowchart – selection control
structure
No Yes
Condition
else- then-
statement(s) statement(s)
Flowchart – repetition control
structure
yes Loop
Condition Statement(s)
no
Example
START
Step 1: Input M1,M2,M3,M4
Input Step 2: GRADE (M1+M2+M3+M4)/4
M1,M2,M3,M4 Step 3: if (GRADE <50) then
Print “FAIL”
GRADE(M1+M2+M3+M4)/4
else
Print “PASS”
endif
N IS Y
GRADE<50
PRINT PRINT
“PASS” “FAIL”
STOP
Example 2
Flowchart
Algorithm
START
Step 1: Input Lft
Step 2: Lcm Lft x 30 Input
Lft
Step 3: Print Lcm
Lcm Lft x 30
Print
Lcm
STOP
Example 3
Algorithm
START
Step 1: Input W,L
Step 2: AL x W Input
W, L
Step 3: Print A
ALxW
Print
A
STOP
Flowchart – example 4
Begin
Calculate
Age = current year – birth date
Display
age
End
Flowchart – example 5
Begin
Read age
End
Flowchart – example 6
Begin
sum = 0
current_number = 1
NO
current_number <= 10? print sum
YES
End
sum = sum + current_number
current_number = current_number + 1