Upload
ali-asghar-manjotho
View
122
Download
7
Tags:
Embed Size (px)
Citation preview
Lecture – 05 to 11Computer Programming
14 Computer Systems Engineering – Second Semester
By: Mr. Ali Asghar Manjotho, Lecturer, CSE-MUET
Contents
• Problem analysis tools
• IPO (Input, Process, Output) chart (LL 04)
• Algorithm (LL 04)
• Flow Chart (LL 04)
• Problem Examples (LL 04)
LL 04 = Learning Level 04 – Analysis
Ali Asghar Manjotho, Lecturer CSE-MUET 2
Problem analysis tools
• Problem analysis tools help you analyze the problem to have a clear picture of the problem.
• They help you identify the problems and their causes at the beginning.
• They help you better understand the problem by conducting its postmortem.
Ali Asghar Manjotho, Lecturer CSE-MUET 4
Problem analysis tools
• The problem analysis tools that we will be using to solve problem are:
Ali Asghar Manjotho, Lecturer CSE-MUET 5
IPO Chart
• IPO stands for Input Process Output.
• It performs the analysis by identifying the input, process and output of a given problem.
Ali Asghar Manjotho, Lecturer CSE-MUET 6
IPO Chart
• It can be represented by the following chart:
Ali Asghar Manjotho, Lecturer CSE-MUET 7
Input Processing Output
Processing Items:
Algorithm:
IPO Chart
• The input section specifies all the inputs to the problem.
• The input may be given by the user from input devices like, keyboard, mouse, scanner etc.
• The input may also come as the contents of a file.
• The input may come from the network or Internet.
Ali Asghar Manjotho, Lecturer CSE-MUET 8
IPO Chart
• The output section specifies all the outcomes of the problem.
• The output may be in the form of the presenting the results on the monitor screen, printer or any other output device.
• The output may also be stored in a file.
• The output may be redirected to network or Internet.
Ali Asghar Manjotho, Lecturer CSE-MUET 9
IPO Chart
• The processing section specifies two things: processing items and algorithm.
• The processing items involve any formula or equation to be used while processing.
• The algorithm is the step by step processing procedure to solve that task.
Ali Asghar Manjotho, Lecturer CSE-MUET 10
Algorithm
• Algorithm is the representation of a solution to a problem.
• It is step by step procedure to solve a given problem.
• An algorithm contains finite number of instructions in order to solve a problem.
Ali Asghar Manjotho, Lecturer CSE-MUET 11
Algorithm
• For example, calculating the average of three numbers.
Step 01: Start
Step 02: Input number1, number2 and number3 from the user
Step 03: Calculate average as: average = (number1 + number2 + number3)/3
Step 04: Print average
Step 05: End
Ali Asghar Manjotho, Lecturer CSE-MUET 12
Flow Chart
• Flow chart is the pictorial representation of a process or an algorithm.
• It uses symbols (boxes of different shapes) to represent each step of an algorithm.
• All the symbols are then connected with arrows to show the flow of the process.
Ali Asghar Manjotho, Lecturer CSE-MUET 13
Flow Chart
Ali Asghar Manjotho, Lecturer CSE-MUET 14
Symbol Name Meaning
Terminal(Oval)
Indicates the beginning and end points of an algorithm.
Process(Rectangle)
Shows an instruction other than input, output or selection.
Flow Chart
Ali Asghar Manjotho, Lecturer CSE-MUET 15
Symbol Name Meaning
Input-Output(Parallelogram)
Shows an input or output instruction.
Decision(Diamond)
Shows any point in the process where decision is to be made.
Flow Chart
Ali Asghar Manjotho, Lecturer CSE-MUET 16
Symbol Name Meaning
On-PageConnector
Continues and connects the flowchart on the same page.
Off-PageConnector
Continues and connects the flowchart on another page.
Flow Chart
Ali Asghar Manjotho, Lecturer CSE-MUET 17
Symbol Name Meaning
Sub-ProcessDenotes movement to another process and hence a new flowchart
ArrowsConnects symbols and shows flow of the process or algorithm.
Problem 01: Area of rectangle
Problem Statement: Write a program that accepts the width andthe height of a rectangle from the user and prints the area of therectangle.
Ali Asghar Manjotho, Lecturer CSE-MUET 19
Problem 01 - IPO Chart and Algorithm
Ali Asghar Manjotho, Lecturer CSE-MUET 20
Input Processing Output
• Width of rectangle• Height of rectangle
Processing Items:area = width * height
Algorithm:Step 01: StartStep 02: Input width and height from the userStep 03: Calculate area as: area = width * heightStep 04: Print areaStep 05: End
• Area of rectangle
Problem 02 : Average of three numbers
Problem Statement: Write a program that accepts three numbersfrom the user and displays the average of the numbers.
Ali Asghar Manjotho, Lecturer CSE-MUET 22
Problem 02 - IPO Chart and Algorithm
Ali Asghar Manjotho, Lecturer CSE-MUET 23
Input Processing Output
• First number• Second number• Third number
Processing Items:average = (number1 + number2 + number3)/3
Algorithm:Step 01: StartStep 02: Input number1, number2 and number3
from the userStep 03: Calculate average as: average =
(number1 + number2 + number3)/3Step 04: Print averageStep 05: End
• Average of numbers
Problem 03 : Temperature scale conversion
Problem Statement: Write a program that receives thetemperature in Celsius from the user and displays the temperaturein Fahrenheit and Kelvin.
Ali Asghar Manjotho, Lecturer CSE-MUET 25
Problem 03 - IPO Chart and Algorithm
Ali Asghar Manjotho, Lecturer CSE-MUET 26
Input Processing Output
• Temperature inCelsius
Processing Items:F = C * 1.8 + 32K = C + 273.15
Algorithm:Step 01: StartStep 02: Input Celsius c from the userStep 03: Calculate Fahrenheit temperature as:
f = (c * 1.8) + 32Step 04: Calculate Kevin temperature as:
k = c + 273.15Step 05: Print f and kStep 06: End
• Temperature in Fahrenheit
• Temperature in Kelvin
Problem 04 : Area and circumference of a circle
Problem Statement: Write a program that receives the radius ofthe circle from the user and displays the area and circumference ofthe circle.
Ali Asghar Manjotho, Lecturer CSE-MUET 28
Problem 04 - IPO Chart and Algorithm
Ali Asghar Manjotho, Lecturer CSE-MUET 29
Input Processing Output
• Radius of circle Processing Items:area = π * radius^2circumference = 2 * π * radius
Algorithm:Step 01: StartStep 02: Input radius from the userStep 03: set PI = 3.1415Step 04: Calculate area as: area = PI * radius^2Step 05: Calculate circumference as:
circumference = 2 * PI * radiusStep 06: Print area and circumferenceStep 07: End
• Area of circle• Circumference of
circle
Problem 05 : Buntty, Bubbly and Building
Problem Statement: There are two friends Buntty and Bubbly.Buntty is standing at the top of a vertical building and Bubbly isstanding on the ground away from the building. Write a programthat displays the distance of Bubbly from the building after askingthe user about the length of the building and the distance betweenBuntty and Bubbly.
Ali Asghar Manjotho, Lecturer CSE-MUET 31
Problem 05 - IPO Chart and Algorithm
Ali Asghar Manjotho, Lecturer CSE-MUET 32
Input Processing Output
• Distance between Buntty and Bubbly
• Height of the Building
Processing Items:Hyp2 = Base2 + Perp2
Hyp = Distance b/w Buntty and BubblyBase = Distance b/w Bubbly and buildingPerp = Height of building
Base = 𝐻𝑦𝑝2 + 𝑃𝑒𝑟𝑝2
Algorithm:Step 01: StartStep 02: Input Dis_Bun_Bub, Height_Bui from the userStep 03: Calculate Distance b/w Bubbly and building as:
Dis_Bub_Bui = 𝐷𝑖𝑠_𝐵𝑢𝑛_𝐵𝑢𝑏2 + 𝐻𝑒𝑖𝑔ℎ𝑡_𝐵𝑢𝑖2
• Distance of Bubbly fromthe building
Problem 05 - IPO Chart and Algorithm
Ali Asghar Manjotho, Lecturer CSE-MUET 33
Input Processing Output
Step 04: Print Dis_Bub_BuiStep 05: End
Problem 06 : Notes and Coins
Problem Statement: A person enters the bank and stands in thequeue to get his salary. When his turn comes, he requests thecashier that I need my salary with minimum notes and coins. Writea program for the cashier that first asks the cashier to enter thesalary amount and then displays the number of notes and coins of(Rs. 5000, Rs. 1000, Rs. 100, Rs. 50, Rs. 20, Rs. 10, Rs. 5, Rs. 2 andRe. 1).
Ali Asghar Manjotho, Lecturer CSE-MUET 35
Problem 06 - IPO Chart and Algorithm
Ali Asghar Manjotho, Lecturer CSE-MUET 36
Input Processing Output
• Salary amount
Processing Items:No of notes = 𝑠𝑎𝑙𝑎𝑟𝑦/𝑛𝑜𝑡𝑒_𝑎𝑚𝑜𝑢𝑛𝑡No of coins = 𝑠𝑎𝑙𝑎𝑟𝑦/𝑐𝑜𝑖𝑛_𝑎𝑚𝑜𝑢𝑛𝑡
Algorithm:Step 01: StartStep 02: Input salary from the userStep 03: Calculate notes of 5000 as:
notes_5000 = 𝑠𝑎𝑙𝑎𝑟𝑦/5000
Step 04: Recalculate salary as: salary = salary – (notes_5000 * 5000)
Step 05: Calculate notes of 5000 as:notes_1000 = 𝑠𝑎𝑙𝑎𝑟𝑦/1000
Step 06: Recalculate salary as: salary = salary – (notes_1000 * 1000)
• Number of notes of 5000• Number of notes of 1000• Number of notes of 500• Number of notes of 100• Number of notes of 50• Number of notes of 20• Number of notes of 10• Number of coins of 5• Number of coins of 2• Number of coins of 1
Problem 06 - IPO Chart and Algorithm
Ali Asghar Manjotho, Lecturer CSE-MUET 37
Input Processing Output
Step 07: Calculate notes of 500 as:notes_500 = 𝑠𝑎𝑙𝑎𝑟𝑦/500
Step 08: Recalculate salary as: salary = salary – (notes_500 * 500)
Step 09: Calculate notes of 100 as:notes_100 = 𝑠𝑎𝑙𝑎𝑟𝑦/100
Step 10: Recalculate salary as: salary = salary – (notes_100 * 100)
Step 11: Calculate notes of 50 as:notes_50 = 𝑠𝑎𝑙𝑎𝑟𝑦/50
Step 12: Recalculate salary as: salary = salary – (notes_50 * 50)
Step 13: Calculate notes of 20 as:notes_20 = 𝑠𝑎𝑙𝑎𝑟𝑦/20
Problem 06 - IPO Chart and Algorithm
Ali Asghar Manjotho, Lecturer CSE-MUET 38
Input Processing Output
Step 14: Recalculate salary as: salary = salary – (notes_20 * 20)
Step 15: Calculate notes of 10 as:notes_10 = 𝑠𝑎𝑙𝑎𝑟𝑦/10
Step 16: Recalculate salary as: salary = salary – (notes_10 * 10)
Step 17: Calculate coins of 5 as:coins_5 = 𝑠𝑎𝑙𝑎𝑟𝑦/5
Step 18: Recalculate salary as: salary = salary – (coins_5 * 5)
Step 19: Calculate coins of 2 as:coins_2 = 𝑠𝑎𝑙𝑎𝑟𝑦/2
Step 20: Recalculate salary as: salary = salary – (coins_2 * 2)
Problem 06 - IPO Chart and Algorithm
Ali Asghar Manjotho, Lecturer CSE-MUET 39
Input Processing Output
Step 21: Calculate coins of 1 as:coins_1 = salary
Step 22: Print notes_5000, notes_1000, notes_500, notes_100, notes_50, notes_20, notes_10, coins_5, coins_2, coins_1
Step 23: End
Problem 07 : Even or odd number
Problem Statement: Write a program that receives an integernumber from the user and displays whether it is an even number oran odd number.
Ali Asghar Manjotho, Lecturer CSE-MUET 42
Problem 07 - IPO Chart and Algorithm
Ali Asghar Manjotho, Lecturer CSE-MUET 43
Input Processing Output
• Integer number Processing Items:If (number MOD 2) = 0 then it is evenIf (number MOD 2) ≠ 0 then it is odd
Algorithm:Step 01: StartStep 02: Input number from the userStep 03: if (number MOD 2) == 0 then GOTO Step 04
else GOTO Step 05Step 04: Print “Number is even” GOTO Step 06Step 05: Print “Number is odd”Step 06: End
• Number is odd or even
Problem 08 : Positive numbers
Problem Statement: Write a program that receives an integernumber from the user. If the number is positive then display thatnumber.
Ali Asghar Manjotho, Lecturer CSE-MUET 45
Problem 08 - IPO Chart and Algorithm
Ali Asghar Manjotho, Lecturer CSE-MUET 46
Input Processing Output
• Integer number Processing Items:If (number > 0) then it is positive
Algorithm:Step 01: StartStep 02: Input number from the userStep 03: if (number > 0) then GOTO Step 04 else
GOTO Step 05Step 04: Print number Step 05: End
• Number if it is positive
Problem 09 : Largest number
Problem Statement: Write a program that receives three uniqueinteger numbers from the user and displays the largest number.
Ali Asghar Manjotho, Lecturer CSE-MUET 48
Problem 09 - IPO Chart and Algorithm
Ali Asghar Manjotho, Lecturer CSE-MUET 49
Input Processing Output
• First number• Second number• Third number
Processing Items:If (num1> num2 and num1 > num3) then num1 is largestIf (num2> num1 and num2 > num3) then num2 is largestIf (num3> num1 and num3 > num2) then num3 is largest
Algorithm:Step 01: StartStep 02: Input num1, num2 and num3 from the userStep 03: if (num1> num2 and num1 > num3) then GOTO
Step 05Step 04: if (num2> num1 and num2 > num3) then GOTO
Step 06 else GOTO Step 07
• Largest number
Problem 09 - IPO Chart and Algorithm
Ali Asghar Manjotho, Lecturer CSE-MUET 50
Input Processing Output
Step 05: Print num1 GOTO Step 08Step 06: Print num2 GOTO Step 08Step 07: Print num3Step 08: End
Problem 10 : Marks sheet
Problem Statement: Write a program that receives the marks offive different subjects from the user and displays the percentageand grade obtained.
Ali Asghar Manjotho, Lecturer CSE-MUET 52
Problem 10 - IPO Chart and Algorithm
Ali Asghar Manjotho, Lecturer CSE-MUET 53
Input Processing Output
• Marks of firstsubject
• Marks of second subject
• Marks of third subject
• Marks of fourth subject
• Marks of fifth subject
Processing Items:Percentage = (Obtained marks/Total marks)*100If(Percentage >= 85) grade = A+ If(Percentage >= 80 and Percentage <= 84) grade = AIf(Percentage >= 75 and Percentage <= 79) grade = B+If(Percentage >= 70 and Percentage <= 74) grade = BIf(Percentage >= 65 and Percentage <= 69) grade = C+If(Percentage >= 60 and Percentage <= 64) grade = CIf(Percentage >= 55 and Percentage <= 59) grade = D+If(Percentage >= 50 and Percentage <= 54) grade = DIf(Percentage <= 49) grade = F
• Percentageof marks
• Grade obtained
Problem 10 - IPO Chart and Algorithm
Ali Asghar Manjotho, Lecturer CSE-MUET 54
Input Processing Output
Algorithm:Step 01: StartStep 02: Input marks of subjects sub1, sub2, sub3, sub4 and sub5
from the userStep 03: Calculate percentage as :
per = ((sub1 + sub2 + sub3 + sub4 + sub5)/500)*100Step 04: If (per>=85) then grade=“A+” GOTO Step 10Step 05: If (per>=80 and per<=84) then grade=“A” GOTO Step 13Step 06: If (per>=75 and per<=79) then grade=“B+” GOTO Step 13Step 07: If (per>=70 and per<=74) then grade=“B” GOTO Step 13Step 08: If (per>=65 and per<=69) then grade=“C+” GOTO Step 13Step 09: If (per>=60 and per<=64) then grade=“C” GOTO Step 13Step 10: If (per>=55 and per<=59) then grade=“D+” GOTO Step 13Step 11: If (per>=50 and per<=54) then grade=“D” GOTO Step 13
Problem 10 - IPO Chart and Algorithm
Ali Asghar Manjotho, Lecturer CSE-MUET 55
Input Processing Output
Step 12: If (per<=49) then grade=“F” Step 13: Print per, gradeStep 14: End
Problem 11 : First 10 integer numbers
Problem Statement: Write a program that generates and displaysfirst 10 integer numbers.
Ali Asghar Manjotho, Lecturer CSE-MUET 58
Problem 11 - IPO Chart and Algorithm
Ali Asghar Manjotho, Lecturer CSE-MUET 59
Input Processing Output
Processing Items:1, 2, 3, . . . , 10
Algorithm:Step 01: StartStep 02: Set i = 1Step 03: Repeat Step 04 to Step 05 while i <=10Step 04: Print iStep 05: Set i = i + 1Step 06: End
• First 10 integer numbers
Problem 12 : Sum of first N integer numbers
Problem Statement: Write a program that displays sum of first Ninteger numbers. Where as the number N is provided by the user.
Ali Asghar Manjotho, Lecturer CSE-MUET 61
Problem 12 - IPO Chart and Algorithm
Ali Asghar Manjotho, Lecturer CSE-MUET 62
Input Processing Output
• Number N Processing Items:Sum = 1 + 2 + 3 + , . . . , + N
Algorithm:Step 01: StartStep 02: Input N from the userStep 03: Set i = 1, sum = 0Step 04: Repeat Step 05 to Step 06 while i <=NStep 05: Set sum = sum + iStep 06: Set i = i + 1Step 07: Print sumStep 08: End
• Sum of first N integers
Problem 13 : First N even numbers
Problem Statement: Write a program that generates and displaysfirst N even numbers. Where as the number N is provided by theuser.
Ali Asghar Manjotho, Lecturer CSE-MUET 64
Problem 13 - IPO Chart and Algorithm
Ali Asghar Manjotho, Lecturer CSE-MUET 65
Input Processing Output
• Number N Processing Items:2N
Algorithm:Step 01: StartStep 02: Input N from the userStep 03: Set i = 1Step 04: Repeat Step 05 to Step 06 while i <=NStep 05: Print 2*iStep 06: Set i = i + 1Step 07: End
• First N even number
Problem 14 : First N multiples of 5
Problem Statement: Write a program that generates and displaysfirst N multiples of 5. Where as the number N is provided by theuser.
Ali Asghar Manjotho, Lecturer CSE-MUET 67
Problem 14 - IPO Chart and Algorithm
Ali Asghar Manjotho, Lecturer CSE-MUET 68
Input Processing Output
• Number N Processing Items:5, 10, 15, 20, 25, . . . , N*5
Algorithm:Step 01: StartStep 02: Input N from the userStep 03: Set i = 1Step 04: Repeat Step 05 to Step 06 while i <=NStep 05: Print i * 5Step 06: Set i = i + 1Step 07: End
• First N multiples of 5
Problem 14 - IPO Chart and Algorithm
Ali Asghar Manjotho, Lecturer CSE-MUET 69
Input Processing Output
• Number N Processing Items:5, 10, 15, 20, 25, . . . , N*5
Algorithm:Step 01: StartStep 02: Input N from the userStep 03: Set i = 5Step 04: Repeat Step 05 to Step 06 while i <=N*5Step 05: Print iStep 06: Set i = i + 5Step 07: End
• First N multiples of 5
Problem 15 : Factorial of a number
Problem Statement: Write a program that receives a positiveinteger number from the user and displays its factorial.
Ali Asghar Manjotho, Lecturer CSE-MUET 72
Problem 15 - IPO Chart and Algorithm
Ali Asghar Manjotho, Lecturer CSE-MUET 73
Input Processing Output
• Number N Processing Items:N! = N * (N-1) * (N-2) * , . . . , * 1
Algorithm:Step 01: StartStep 02: Input N from the userStep 03: Set i = N , factorial = 1Step 04: Repeat Step 05 to Step 06 while i >1Step 05: Set factorial = factorial * iStep 06: Set i = i - 1Step 07: Print factorialStep 08: End
• Factorial of N
Problem 16 : Prime or composite number
Problem Statement: Write a program that receives a positiveinteger number from the user and displays whether it is a prime orcomposite number.
Ali Asghar Manjotho, Lecturer CSE-MUET 75
Problem 16 - IPO Chart and Algorithm
Ali Asghar Manjotho, Lecturer CSE-MUET 76
Input Processing Output
• Number N Processing Items:Prime number = (N MOD n) ≠ 0, where n= 2 to (N-1)Composite number = Otherwise
Algorithm:Step 01: StartStep 02: Input N from the userStep 03: Set i = 2 , isPrime = trueStep 04: Repeat Step 05 to Step 06 while i < NStep 05: if (N MOD i) = 0 then isPrime = false GOTO Step 07Step 06: Set i = i + 1Step 07: if isPrime = true GOTO Step 08 else GOTO Step 09
• N is prime or composite
Problem 16 - IPO Chart and Algorithm
Ali Asghar Manjotho, Lecturer CSE-MUET 77
Input Processing Output
Step 08: Print “Number is prime” GOTO Step 10Step 09: Print “Number is composite”Step 10: End