Lecture 2 Problem Solving Techniques Algorithims
Lecture 2 Problem Solving Techniques Algorithims
Level: BS
Semester: 1st
1
Problem Solving Steps
1. Understand the problem
2. Plan the logic
3. Code the program
4. Test the program
5. Deploy the program into production
1. Understanding the Problem
• Problems are often described in natural language
like English.
https://d3n817fwly711g.cloudfront.net/uploads/2017/07/flowchart-feature-image-01-1280x720.jpg
Basic Flowchart Symbols
Name Symbol Description
Print Print
“PASS” “FAIL”
STOP
Example 2
• Write an algorithm and draw a flowchart to convert
the length in feet to centimeter.
Example 2
Algorithm Flowchart
Print LCM
STOP
Example 3
• Write an algorithm and draw a flowchart that will read
the Length and Width of a rectangle and calculate its
area.
Example 3
START
Algorithm
Read
• Step 1: Read W,L W, L
• Step 2: Area = L x W
Area L x W
• Step 3: Print A
Print
Area
STOP
Decision Structures
• The expression A > B is a logical expression
Print A Print B
CASE Structure
• Multiple branching based on a single data item
Data Item
ALGORITHM
Step 1: Read VALUE1, VALUE2
Step 2: if (VALUE1 > VALUE2) then
MAX VALUE1
else
MAX VALUE2
endif
Yes No
is
A>B
Print A Print B
Selection Structure
Single Alternative Example
Pseudocode: IF GPA is greater than 2.0 Then
Print “Promoted ”
End IF
No GPA Yes
> 2.0
Print
“Promoted”
Loop Structure
• Repetition (WHILE structure)
– Repeats a set of actions based on the answer to a
question/condition
pseudocode: DoWHILE <Some-True-Condition>
Do Something
ENDDO
Loop Structure
• REPEAT-UNTIL structure
– Repeats a set of actions until a condition remains True
– pseudocode: REPEAT
Do-Something
UNTIL <Some True Condition>
https://www.google.com.pk/url?sa=i&url=https%3A%2F%2Fsummer-heart-0930.chufeiyun1688.workers.dev%3A443%2Fhttps%2Fimgflip.com%2Fi%2Fnp6r7&psig=AOvVaw0hSaZQ4QperYmr4Q-0159s&ust=1630516336101000&source=images&cd=vfe&ved=2ahUKEwijzbDp4NvyAhX3AmMBHQ7ZB7QQjRx6BAgAEAk
Lecture # 2
Problem Solving Approach
1) Natural Language Approach
2) Pseudo code
3) Flow Chart
37
Introduction
Before writing a program:
Have a thorough understanding of the
problem
Carefully plan an approach for solving it
41
Algorithm: A Formal Definition
An algorithm is a
finite sequence of instructions,
a clear, step-by-step procedure
for solving a problem,
often used for calculation and data processing.
We must know that
What is the data
What is the operation to be performed
What is the order of the operations
42
Characteristics of ALGORITHM
Steps must be in proper order
Algorithm must terminate and should
not repeat any step infinite time.
Correct result must be obtained on
termination.
43
Algorithm Representation
Alogrithm can be represented using:
1. Natural Language
2. Pesudocode
3. Flow chart
44
Natural Language Algorithm
45
Natural Language Algorithm
Advantages:
Easy to understand and use.
Disadvantages:
Can be wordy results in lengthy algorithm.
Can be difficult to convert in any
programming language.
46
Natural Language Algorithm
Problem Statement
• What
• Why
Algorithm Description
• How
• When
• Where
47
Natural Language Algorithm
Problem Statement
Calculate Input
Describe Outout
Show etc
Display
Take
Process
48
Examples # 1:
Problem Statement (PS):
Calculate the sum of first five numbers.
Algorithm Description (AD):
This algorithm will calculate the sum of
first five numbers and add them and
display result.
49
Examples # 2:
Problem Statement (PS):
Take five numbers from user and calculate
the sum of numbers.
Algorithm Description (AD):
This algorithm will calculate the sum of
first five numbers given by user and
display result.
50
Examples # 3:
Problem Statement (PS):
Measure the speed of a given car.
Algorithm Description (AD):
This algorithm will show that how to
measure the speed of a given car.
51
Examples # 4:
Problem Statement (PS):
Arrival of student in university on time.
Algorithm Description (AD):
This algorithm will describe the procedure
to reach the university on time.
52
Examples # 5:
PS: Calculate sum of five numbers from user.
AD: This algorithm will take five numbers from
user, calculate the sum and display the result.
Steps:
1. Start/ Begin.
2. Take five numbers from user.
3. Add five numbers.
4. Store the result.
5. Display the result.
6. Stop/ Finish/ End
53
Examples # 6:
PS: Measure the speed of a car.
AD: This algorithm shows that how to measure
speed of a given car.
Steps:
1. Start/ Begin.
2. Take average distance and time.
3. Divide distance by time. OR Calculate the speed by
dividing distance over time.
4. Store the result.
5. Display the result. OR Display the speed.
6. Stop/ Finish/ End 54
Examples # 7:
PS: Calculate the average of five even numbers
given by user.
AD: This algorithm will display the average of five
even numbers taken by user.
Steps:
1. Start/ Begin.
2. Input five even numbers.
3. Calculate the sum.
4. Store the sum.
5. Divide the sum by five.
6. Store the average.
7. Display the average.
8. Stop/ Finish/ End 55
Examples # 8:
PS: Find the area of square.
AD: This algorithm will display the calculated area of
given square.
Steps:
1. Start.
2. Input length of one side.
3. Calculate the area by multiplying with itself.
4. Store the area.
5. Display the area.
6. Finish
56
Examples # 9:
PS: Obtain a number from user and display back the
product of the number.
AD:
Steps:
57
Thanks!!!!!!!!!!!!!!!!!!