Upload
marquise-longbottom
View
217
Download
1
Embed Size (px)
Citation preview
Program Planning & DesignProgram Planning & Design
Chapter 3Chapter 3
Steps in Program Planning & Steps in Program Planning & DesignDesign
o Understand the problemUnderstand the problem
o Choose and/or design appropriate algorithmChoose and/or design appropriate algorithm
o Develop flow-chart or pseudo-codeDevelop flow-chart or pseudo-code
o Write computer programWrite computer program
o Test & verifyTest & verify
Problem descriptionProblem description
o Problem should be well definedProblem should be well definedDesign a computer program that will train 3Design a computer program that will train 3 rdrd
grade school children in multiplication 1 through grade school children in multiplication 1 through 16. Every time the student enters a correct 16. Every time the student enters a correct answer, the program will generate and ask a answer, the program will generate and ask a new problem. The program will allow the new problem. The program will allow the student three chances to give correct answer. student three chances to give correct answer. The program should terminate when a student The program should terminate when a student enters ‘END’ as the answer.enters ‘END’ as the answer.
Pseudo CodePseudo Codeo More human-language typeMore human-language type
o Multiplication questionMultiplication question1.1. Generate a pair of random numbers ‘a’ and ‘b’ between 1 Generate a pair of random numbers ‘a’ and ‘b’ between 1
and 16. Ensure that this pair has not been used already.and 16. Ensure that this pair has not been used already.
2.2. Save a & b and ask the user to enter the answer to a x bSave a & b and ask the user to enter the answer to a x b
3.3. Give 3 chances to the user to answer correctly. Give the Give 3 chances to the user to answer correctly. Give the correct answer if the user failed in all attempts and record correct answer if the user failed in all attempts and record this pair as a repeated error.this pair as a repeated error.
4.4. Repeat 1 – 3 till the user types ‘END’.Repeat 1 – 3 till the user types ‘END’.
5.5. Print all repeated error pairs with correct answers.Print all repeated error pairs with correct answers.
Can you find a flaw?
Hierarchy ChartsHierarchy Charts
Salary Calculator
Annual Raise Bonus Leave Deductions
One time Distributed
Paid Unpaid Sick
Fixed One time
Flow ChartsFlow Charts
Processing StepTerminatorStart & End
ConnectorData In or Out
Off page Connector
Decision
Problem UnderstandingProblem Understanding
o Get a pair of numbers from the user, add Get a pair of numbers from the user, add them and display the result on monitor.them and display the result on monitor.
Integer, Real or Complex numbersInteger, Real or Complex numbersRepresentation of Complex numbersRepresentation of Complex numbers
• Rectangular or polar representation?Rectangular or polar representation?
o Divide a number by another number and Divide a number by another number and write the result on monitor.write the result on monitor.
User InterfaceUser Interface
o User of a computer is not necessarily a User of a computer is not necessarily a computer scientist or engineercomputer scientist or engineer
Make the users learn CS/CE?Make the users learn CS/CE?• Who will hire CS/CE majors? Who will hire CS/CE majors?
Design programs that are user friendlyDesign programs that are user friendly• More jobs for CS/CE More jobs for CS/CE
DocumentationDocumentation
o Try to understand the following codeTry to understand the following code
T = AA = BB = T
DocumentationDocumentation
o Try to understand the following codeTry to understand the following code
‘Select the bigger of the two numbersIf (numberA < numberB) then
biggernumber = numberBElse
biggernumber = numberAEnd If
Comment line
ExampleExample
o A factory gives a sale incentive to its dealers as follows. A A factory gives a sale incentive to its dealers as follows. A monthly sale of Rs 5,000 is the break even point and is not monthly sale of Rs 5,000 is the break even point and is not eligible for any incentive. For the amount above Rs. 5,000 eligible for any incentive. For the amount above Rs. 5,000 but below Rs. 20,000, the incentive is fixed at 5% of the but below Rs. 20,000, the incentive is fixed at 5% of the “eligible amount”. The eligible amount is found by “eligible amount”. The eligible amount is found by subtracting Rs. 5,000 from the monthly sale figure. subtracting Rs. 5,000 from the monthly sale figure. Incentive rate steps up by 2% for every additional Rs Incentive rate steps up by 2% for every additional Rs 10,000 sale, beyond the Rs. 20,000 sale. The total 10,000 sale, beyond the Rs. 20,000 sale. The total incentive received by a dealer is capped at a yearly incentive received by a dealer is capped at a yearly maximum of Rs. 19,900. Write a computer program that maximum of Rs. 19,900. Write a computer program that computes the monthly incentive for a dealer, given his/her computes the monthly incentive for a dealer, given his/her sale amount for a month.sale amount for a month.
Solution: Incentive CalculationsSolution: Incentive Calculations
First MethodFirst Method Second MethodSecond Method
If the total sale is above Rs. If the total sale is above Rs. 20,000 in a month then use the 20,000 in a month then use the highest rate to the total eligible highest rate to the total eligible amount. For example, a sale of amount. For example, a sale of Rs.39,000 has Rs34,000 as eligible Rs.39,000 has Rs34,000 as eligible amount. The maximum rate is amount. The maximum rate is 7%. Thus the incentive is 7%. Thus the incentive is Rs.2,380 for that month.Rs.2,380 for that month.
Use 5% incentive rate on the Use 5% incentive rate on the eligible amount for sales up to Rs. eligible amount for sales up to Rs. 20,000. For a total sale beyond 20,000. For a total sale beyond this use incremental incentive this use incremental incentive rate. For a sale of Rs. 39,000, we rate. For a sale of Rs. 39,000, we use 5% rate for the first Rs. use 5% rate for the first Rs. 20,000 sale and 7% for the 20,000 sale and 7% for the additional Rs. 19,000. Thus the additional Rs. 19,000. Thus the total incentive is 5% of 15,000 + total incentive is 5% of 15,000 + 7% of 19,000 = 750 + 1,330 = 7% of 19,000 = 750 + 1,330 = Rs.2,080Rs.2,080
Incentive: Pseudo CodeIncentive: Pseudo CodeGet the name or identity of the dealer.Get YearIncentive given to that dealer so far and the MonthSale.Set MonthIncentive=0If (YearIncentive is less than 19,900 AND MonthSale is more than 5000) then
Calculate Incentive for current monthTotalIncentive = YearIncentive + IncentiveMonthIncentive=IncentiveIf (TotalIncentive > 19,900)
MonthIncentive = 19,900- YearIncentiveTotalIncentive = 19,900
End If YearIncentive=TotalIncentiveSave YearIncentive
End IfDisplay MonthIncentiveStop
Flow ChartFlow ChartStart
Read MonthSale & YearIncentive
YearIncentive < 19,900And MonthSale > 5000
MonthIncentive=0 Calculate Incentive and save results
No YesA
B
1
Flow ChartFlow Chart
DisplayMonthIncentive
STOP
1
A GCF algorithmA GCF algorithm
o Let A & B be the two positive numbersLet A & B be the two positive numbers
1.1. Assign M & N the values of larger & Assign M & N the values of larger & smaller of the two numbers respectivelysmaller of the two numbers respectively
2.2. Divide M by N. Let the remainder be R.Divide M by N. Let the remainder be R.
3.3. If R = 0 then N is the GCF, otherwise let If R = 0 then N is the GCF, otherwise let M = N, and N = R and return to step 2.M = N, and N = R and return to step 2.
Ref: Computer Science An Overview, 6Ref: Computer Science An Overview, 6thth edition, by J. Glenn Brooksheer, Addison-Wesley, 2000 edition, by J. Glenn Brooksheer, Addison-Wesley, 2000
Working of the GCF AlgoWorking of the GCF Algo
o Assume that A = 15 & B = 24Assume that A = 15 & B = 24o M = 24 & N = 15M = 24 & N = 15o MM÷N = 24 ÷ 15 = 1 with R = 9÷N = 24 ÷ 15 = 1 with R = 9o M = 15 & N = 9M = 15 & N = 9o M ÷ N = 15 ÷ 9 = 1 with R = 6M ÷ N = 15 ÷ 9 = 1 with R = 6o M = 9 & N = 6M = 9 & N = 6o M ÷ N = 9 ÷ 6 = 1 with R = 3M ÷ N = 9 ÷ 6 = 1 with R = 3o M = 6 & N = 3M = 6 & N = 3o M ÷ N = 6 ÷ 3 = 2 with R = 0M ÷ N = 6 ÷ 3 = 2 with R = 0o GCF = 3GCF = 3
o CheckCheck
o 15 = 3 x 515 = 3 x 5
o 24 = 2 x 2 x 2 x 324 = 2 x 2 x 2 x 3
o GCF = 3GCF = 3
GCF FlowchartGCF FlowchartStartStart
M=A, & N=BM=A, & N=B
Is A>B?Is A>B?
M=B, & N=AM=B, & N=A
R=Remainder(M,N)R=Remainder(M,N)
Is R=0?Is R=0?
GCF=NGCF=N
M=N, N=RM=N, N=R
EndEnd
YesYesNoNo
NoNoYesYes
Print GCF of A & BPrint GCF of A & B
Read A & BRead A & B
Sorting ListsSorting Lists
o ProblemProblemAn instructor found that student scores in the midterm An instructor found that student scores in the midterm
were in descending order when he arranged student were in descending order when he arranged student names alphabetically. Later on, the student names names alphabetically. Later on, the student names and scores got mixed up. Help the instructor recover and scores got mixed up. Help the instructor recover correct score of each student.correct score of each student.
o SolutionSolutionSort names & scoresSort names & scores
LimitationLimitation You can view only two data elements simultaneouslyYou can view only two data elements simultaneously
Sorting listsSorting lists
SultanSultanQadeerQadeerAamirAamirZubairZubairKhanKhan
90904545989854548787
Your TaskYour Task: Come up with a method to sort the two lists: Come up with a method to sort the two lists
Sorting ListsSorting ListsQadeerQadeer
AamirAamir
SultanSultan
ZubairZubair
KhanKhan
QadeerQadeer
AamirAamir
SultanSultan
KhanKhan
ZubairZubair
Question: When should we stop? Question: When should we stop?
How do we know that the list is sorted?How do we know that the list is sorted?
Remember; we can see only two data points simultaneously.Remember; we can see only two data points simultaneously.
KhanKhan
ZubairZubair
AamirAamir
QadeerQadeer
SultanSultan
KhanKhan
ZubairZubair
AamirAamir
SultanSultan
QadeerQadeer
KhanKhan
ZubairZubair
SultanSultan
AamirAamir
QadeerQadeer
Bubble SortBubble Sort
23
13
32
5
17
- Compare two elements at a time and interchange IF REQUIRED.- Repeat the process “n-1” times – why?
- Now repeat this process 3 times- Note that after one iteration the largest number (32) is at its rightful position.
First Iteration Complete
23
13
32
5
17
Second Iteration Complete
- Still need 2 more iterations.- Note that after 2nd iteration the second largest number (23) is at its rightful position.
23
13
32
5
17
Third Iteration Complete
- One more iteration required.- Note that after the 3rd iteration the third largest number (17) is at its rightful position.
23
13
32
5
17
- No need to swap.- All elements are at their rightful positions.
Fourth (n-1) Iteration Complete
Sorted List
5
13
17
23
32