1/4/2022
Lecture Outline
SWE1301: Introduction to Problem
Solving and Software Development • Sequential Algorithms
• Structure
• Examples
Lecture 06 : Sequential Structure
Venue : CIT Theatre
Presented by: M. I. Mukhtar
1/4/2022 SWE1301: Problem Solving and Software Development MIM 2
Recall-Structures of Algorithms Sequential Logic Structure
• The sequential structure executes instructions • The most commonly used and the simplest logic
one after another in a sequence. structure is the sequential structure.
• All problems use the sequential structure, and most
problems use it in conjunction with one or more of
• The decision structure branches to execute one the other logic structures.
of two possible sets of instructions.
• The sequential structure executes instructions one
• The loop structure executes a set of after another in a sequence.
instructions many times.
• A programmer who uses the sequential logic
structure is asking the computer to process a set of
instructions in sequence from the top to the bottom of
an algorithm.
1/4/2022 SWE1301: Problem Solving and Software Development MIM 3 1/4/2022 SWE1301: Problem Solving and Software Development MIM 4
1
1/4/2022
Sequential Logic Structure Format Example 1
• Using appropriate solution tools, solve the
problem of calculating the area of a circle.
1/4/2022 SWE1301: Problem Solving and Software Development MIM 5 1/4/2022 SWE1301: Problem Solving and Software Development MIM 6
Recall Solution: Problem Analysis Chart..
Step 1, 2,3,4: • Calculate the Area of a circle given the radius (r)
PAC, • The Area is calculated by multiplying the ∏ with
• Identify problem.
Pseudocode, radius.
• Understand problem.
Flowchart Given Data Required Results
• Identify alternatives ways.
• Choose best way.
r Area
Step 5: ∏
Algorithm, Processing Required Solution alternative
• List the instruction
Flowchart
Area = ∏r2 1. Define r2 as r * r or pow(r,2)
2. Define r as a constant (assign
Step 6: Coding value to r) or input value.
• Evaluate the solution & Programming languages us PI instead of ∏
Testing symbol
1/4/2022 SWE1301-Problem Solving and Software Development- MIM 7 1/4/2022 SWE1301: Problem Solving and Software Development MIM 8
2
1/4/2022
Solution: Pseudocode Solution: Algorithms…
From Example 1; From Example 1:
• Enter the radius and PI • Step 1: Enter radius
• Calculate Area • Step 2: Enter PI
• Display Area • Step 3: Area = PI * radius * radius
• Step 4: Print Area
• Step 5 : End
Or
• Step 1: Enter radius
• Step 2: Enter PI
• Step 3: Area = PI * pow (radius,2)
• Step 4: Print Area
• Step 5 : End
1/4/2022 SWE1301: Problem Solving and Software Development MIM 9 1/4/2022 SWE1301: Problem Solving and Software Development MIM 10
Solution: Flowchart Solution: Testing solution for Bugs
• Desk Check with the following
• Radius of 10
START
• Radius of -2
Read radius • Desk check is a manual technique for
checking the logic and correctness of an
Area = PI * radius * radius algorithm.
Print Area
STOP
1/4/2022 SWE1301: Problem Solving and Software Development MIM 11 1/4/2022 SWE1301: Problem Solving and Software Development MIM 12
3
1/4/2022
Solution: Coding in Python Solution: Testing code for Bugs
From Example 1 • Test with the following
1. Alternative 1 • Radius of 10
import math • Radius of -2
radius = float (input("Enter the radius"))
Area = math.pi * radius * radius
print(Area)
2. Alternative 2
radius = 12
Area = math.pi * pow(radius,2)
print(“The area of a circle with radius”, r,
“is”,Area)
1/4/2022 SWE1303 13 1/4/2022 SWE1301: Problem Solving and Software Development MIM 14
Example 2 Solution- PAC
• Using appropriate solution tools, solve the Given Data Required result
problem of reading two numbers (n1 and n2 )
and finding their sum. n1 , n2 Sum
• Desk Check with the following
• 10 and 18 Required Processing Solution alternative
• -2 and 32
Define n1 and n2 as a
Sum = n1 + n2 constant or input value.
1/4/2022 SWE1303 15 1/4/2022 SWE1303 16
4
1/4/2022
Solution- Algorithm & Flowcharts Solution: Coding in Python
Algorithm Flowchart Desk Checking n1= float(input("enter the first number"))
n2= float(input("enter the second number"))
START
Sum = n1 + n2
Step 1: Read n1. Read n1 n1=10, n2= 18 print (Sum)
n1=-2, n2 = 32
Step 2: Read n2. Read n2
Step 3: Sum =n1+n2 Sum = n1 + n2 Sum = 10 + 18
Sum = -2 + 32
Step 4: Print Sum Print Sum
Step 5 : End 28
STOP 30
1/4/2022 SWE1303 17 1/4/2022 SWE1303 18
Example 3 Solution- PAC
• Using appropriate solution tools, solve the Given Data Required result
problem of converting the length in feet(LFT) to
length in centimeter (LCM) . Given that LCM= LFT LCM
LFT* 30
Required Processing Solution alternative
• Desk Check with the following
•2 Define LCM as a
• 10 LCM= LFT * 30 constant or input value.
1/4/2022 SWE1303 19 1/4/2022 SWE1303 20
5
1/4/2022
Solution- Algorithm & Flowchart Solution: Coding in Python
Algorithm Flowchart Test LFT= float(input("enter the length in feet"))
LCM = LFT * 30
START LFT = 2 print (LCM)
Step 1: Input LFT. Input LFT
LFT = 10
Step 2: LCM = LFT * LCM = 2 * 30
30 LCM =LFT * 30
LCM= 10 * 30
Step 3: Print LCM
Print LCM 60
Step 4 : End 300
STOP
1/4/2022 SWE1303 21 1/4/2022 SWE1303 22
Example 4 Solution- Algorithm & Flowchart
• Using appropriate solution tools, solve the Algorithm Flowchart
problem of finding the average of five numbers.
START
• Test with 2,8,5,9 Step 1: Read Read
n1,n2,n3,n4,n5 n1,n2,n3,n4,n5
Step 2: Average= Average =
(n1+n2+n3+n4+n5)/5 (n1+n2+n3+n4+n5)/5
Step 3: Print Average Print Average
Step 4 : End
STOP
1/4/2022 SWE1303 23 1/4/2022 SWE1303 24
6
1/4/2022
Solution: Coding in Python Example 5
n1= float(input("enter the first number")) • Using appropriate solution tools, solve the
n2= float(input("enter the second number")) problem of calculating the volume and Area of a
n3= float(input("enter the third number")) sphere.
n4= float(input("enter the fourth number")) • Given that :
n5= float(input("enter the fifth number"))
Average = (n1 + n2 + n3 + n4 + n5)/5
print(Average)
1/4/2022 SWE1303 25 1/4/2022 SWE1303 26
Solution- Algorithm & Flowchart Solution- Algorithm & Flowchart
Algorithm Flowchart Algorithm Flowchart
START START
Enter r Enter r
Step 1: Enter r Step 1: Enter r
Step 2: Volume= Volume = Step 2: Area= 4*PI*pow Area =
(4*PI*pow (r,3))/3 (4*PI*pow (r,3))/3 (r,2) 4*PI*pow (r,2)
Print Volume Print Area
Step 3: Print Volume Step 3: Print Area
STOP STOP
Step 4 : End Step 4 : End
1/4/2022 SWE1303 27 1/4/2022 SWE1303 28
7
1/4/2022
Solution: Coding in Python Example 6
import math • Using appropriate solution tools, solve the
r = float (input("enter radius")) problem of asking a user to enter his/her name
Volume = (4 * math.pi * pow(r,3))/3 and then outputs a message in the format (the
name of the person plus “Welcome to problem
Area = ( 4 * math.pi * pow(r,2))
solving class”).
print (Volume , "and" , Area)
1/4/2022 SWE1303 29 1/4/2022 SWE1301: Problem Solving and Software Development MIM 30
Solution- PAC Solution- Algorithm & Flowchart
Given Data Required result Algorithm Flowchart
START
name message
Read name
Step 1: Read name.
Required Processing Solution alternative Message = name + “welcome to
Step 2: Message = name +
problem solving class”
“welcome to problem
solving class”
Message = name +
welcome to problem Print Message
solving class Step 3: Print Message
STOP
Step 4 : End
1/4/2022 SWE1303 31 1/4/2022 SWE1303 32
8
1/4/2022
Solution: Coding in Python Exercises
name = str(input("enter your name")) • Using appropriate solution tools, solve the
Message = name + " welcome to problem solving problem of reading two numbers (n1 and n2 )
class" and finding their product and difference.
print(Message)
• Using appropriate solution tools, solve the
problem of calculating area of a trapezium.
1/4/2022 SWE1303 33 1/4/2022 SWE1303 34
Questions??
1/4/2022 SWE1301: Problem Solving and Software Development MIM 35