0% found this document useful (0 votes)
6 views

Lecture 2 Problem Solving Techniques Algorithims

Uploaded by

azharalikhan1810
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

Lecture 2 Problem Solving Techniques Algorithims

Uploaded by

azharalikhan1810
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 47

Programming Fundamental

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.

• Identify the requirements


1. Inputs or given data-items
2. Required output(s) or desired results
3. Indirect inputs (may not be given directly, you
have to calculate or assume)
1. Understanding the Problem
– Example: Calculate the area of a circle having
the radius of 3 cm
• Inputs:
Radius=3
• Output:
Area
• Indirect Inputs:
Pi=3.14

Area = 3.14 * (3*3) = 28.27


2. Plan the Logic
• Identify/Outline small steps in sequence, to
achieve the goal (or desired results)

• Tools such as flowcharts and pseudocode can be


used:
1. Flowchart: a pictorial representation of the
logic steps
2. Pseudocode: English-like representation of
the logic

Advice: Walk through the logic before coding


3. Code the Program
• Code the program:
– Select the programming language
– Write the program instructions in the selected
programming language
– Use the compiler software to translate the
program into machine understandable code
– Syntax errors (Error in program instructions) are
identified by the compiler during compilation and
can be corrected.
4. Test the Program
• Testing the program
– Execute using sample data and check the results

– Identify logic errors if any (undesired results or


output) and correct them
5. Deploy the Program
• Putting the program into production
– Do this after testing is complete and all known
errors have been corrected
Program Logic: Flowcharts
• “A graphic representation of a sequence of operations
to represent a computer program”
– Shows steps of the solution
– Shows individual steps and their interconnections

https://d3n817fwly711g.cloudfront.net/uploads/2017/07/flowchart-feature-image-01-1280x720.jpg
Basic Flowchart Symbols
Name Symbol Description

Oval Beginning or End of the Program

Parallelogram Input / Output Operations

Rectangle Processing for example, Addition,


Multiplication, Division, etc.

Denotes a Decision (or branching)


Diamond
for example IF-Then-Else

Denotes the Direction of logic


Arrow
flow
Example 1
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”
else
GRADE(M1+M2+M3+M4)/4
Print “PASS”
endif
No IS Yes
GRADE<50

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

• Step 1: Read Lft START


• Step 2: Lcm = Lft x 30 Read Lft
• Step 3: Print Lcm
Lcm = Lft x 30

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

• It describes a condition, we want to test

• if A>B is true (if A is greater than B) we take a action on


left

• Print the value of A

• if A>B is false (if A is not greater than B) we take a action


on right

• Print the value of B


IF–THEN–ELSE STRUCTURE
• The algorithm for the flowchart is as follows:
If A>B then
print A
else
print B
Yes is No
endif
A>B

Print A Print B
CASE Structure
• Multiple branching based on a single data item

Data Item

Do Step A Do Step B Do Step C Do Step D


Relational / Logical Operators
Relational Operators
Operator Symbol Description
(Pseudocode)
> Greater than
< Less than
= Equal to
 Greater than or equal to
 Less than or equal to
 Not equal to
Example 4
• Write an algorithm that reads two values, finds largest
value and then prints the largest value.

ALGORITHM
Step 1: Read VALUE1, VALUE2
Step 2: if (VALUE1 > VALUE2) then
MAX  VALUE1
else
MAX  VALUE2
endif

Step 3: Print “The largest value is”, MAX

-- DRAW the Flow Chart for the Program


Selection Structure
• A Selection structure can be based on
1. Dual-Alternative (two code paths)
2. Single Alternative (one code path)
Dual Alternative Example:

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

 While writing a program:


 Know what “building blocks” are available
 Use good programming principles
38
PROBLEM SOLVING
 Problem solving is a creative process. The
programmer must understand the problem
completely and then should prepare a plan
for its solution.
 Prepared plan must be implemented by
developing the computer program to solve
the problem.
 The process of developing a computer
program or software is in fact a series of
phases.
39
Computer program
development process
 Planning
 Analysis
 Design
 Coding
 Testing & debugging
 Implementation
 Maintenance
40
ALGORITHM
An algorithm is a well-defined sequence of
acts or procedures to perform for the
solution of a problem in a finite number of
steps.
Or
Step by step procedure for the solution of a
problem. It shows the logic of program.

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

This technique results in series of


numbered steps executed in
sequence.
These steps are written in natural
language (English).

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!!!!!!!!!!!!!!!!!!

You might also like