Download presentation

Presentation is loading. Please wait.

Published byMarquise Longbottom Modified about 1 year ago

1
Program Planning & Design Chapter 3

2
Steps in Program Planning & Design oUnderstand the problem oChoose and/or design appropriate algorithm oDevelop flow-chart or pseudo-code oWrite computer program oTest & verify

3
Problem description oProblem should be well defined Design a computer program that will train 3 rd grade school children in multiplication 1 through 16. Every time the student enters a correct answer, the program will generate and ask a new problem. The program will allow the student three chances to give correct answer. The program should terminate when a student enters ‘END’ as the answer.

4
Pseudo Code oMore human-language type oMultiplication question 1.Generate a pair of random numbers ‘a’ and ‘b’ between 1 and 16. Ensure that this pair has not been used already. 2.Save a & b and ask the user to enter the answer to a x b 3.Give 3 chances to the user to answer correctly. Give the correct answer if the user failed in all attempts and record this pair as a repeated error. 4.Repeat 1 – 3 till the user types ‘END’. 5.Print all repeated error pairs with correct answers. Can you find a flaw?

5
Hierarchy Charts Salary Calculator Annual RaiseBonusLeaveDeductions One timeDistributed PaidUnpaidSick FixedOne time

6
Flow Charts Processing Step Terminator Start & End Connector Data In or Out Off page Connector Decision

7
Problem Understanding oGet a pair of numbers from the user, add them and display the result on monitor. Integer, Real or Complex numbers Representation of Complex numbers Representation of Complex numbers Rectangular or polar representation?Rectangular or polar representation? oDivide a number by another number and write the result on monitor.

8
User Interface oUser of a computer is not necessarily a computer scientist or engineer Make the users learn CS/CE? Who will hire CS/CE majors? Who will hire CS/CE majors? Design programs that are user friendly More jobs for CS/CEMore jobs for CS/CE

9
Documentation oTry to understand the following code T = A A = B B = T

10
Documentation oTry to understand the following code ‘Select the bigger of the two numbers If (numberA < numberB) then biggernumber = numberB Else biggernumber = numberA End If Comment line

11
Example oA 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 eligible for any incentive. For the amount above Rs. 5,000 but below Rs. 20,000, the incentive is fixed at 5% of the “eligible amount”. The eligible amount is found by subtracting Rs. 5,000 from the monthly sale figure. Incentive rate steps up by 2% for every additional Rs 10,000 sale, beyond the Rs. 20,000 sale. The total incentive received by a dealer is capped at a yearly maximum of Rs. 19,900. Write a computer program that computes the monthly incentive for a dealer, given his/her sale amount for a month.

12
Solution: Incentive Calculations First Method Second Method If the total sale is above Rs. 20,000 in a month then use the highest rate to the total eligible amount. For example, a sale of Rs.39,000 has Rs34,000 as eligible amount. The maximum rate is 7%. Thus the incentive is Rs.2,380 for that month. Use 5% incentive rate on the eligible amount for sales up to Rs. 20,000. For a total sale beyond this use incremental incentive rate. For a sale of Rs. 39,000, we use 5% rate for the first Rs. 20,000 sale and 7% for the additional Rs. 19,000. Thus the total incentive is 5% of 15,000 + 7% of 19,000 = 750 + 1,330 = Rs.2,080

13
Incentive: Pseudo Code Get the name or identity of the dealer. Get YearIncentive given to that dealer so far and the MonthSale. Set MonthIncentive=0 If ( YearIncentive is less than 19,900 AND MonthSale is more than 5000) then Calculate Incentive for current month TotalIncentive = YearIncentive + Incentive MonthIncentive=Incentive If ( TotalIncentive > 19,900) MonthIncentive = 19,900- YearIncentive TotalIncentive = 19,900 End If YearIncentive=TotalIncentive Save YearIncentive End If Display MonthIncentive Stop

14
Flow Chart Start Read MonthSale & YearIncentive YearIncentive < 19,900 And MonthSale > 5000 MonthIncentive=0 Calculate Incentive and save results No Yes A B 1

15
Flow Chart Display MonthIncentive STOP 1

16
A GCF algorithm oLet A & B be the two positive numbers 1.Assign M & N the values of larger & smaller of the two numbers respectively 2.Divide M by N. Let the remainder be R. 3.If R = 0 then N is the GCF, otherwise let M = N, and N = R and return to step 2. Ref: Computer Science An Overview, 6 th edition, by J. Glenn Brooksheer, Addison-Wesley, 2000

17
Working of the GCF Algo oAssume that A = 15 & B = 24 oM = 24 & N = 15 oM÷N = 24 ÷ 15 = 1 with R = 9 oM = 15 & N = 9 oM ÷ N = 15 ÷ 9 = 1 with R = 6 oM = 9 & N = 6 oM ÷ N = 9 ÷ 6 = 1 with R = 3 oM = 6 & N = 3 oM ÷ N = 6 ÷ 3 = 2 with R = 0 oGCF = 3 oCheck o15 = 3 x 5 o24 = 2 x 2 x 2 x 3 oGCF = 3

18
GCF Flowchart Start M=A, & N=B Is A>B? M=B, & N=A R=Remainder(M,N) Is R=0? GCF=N M=N, N=R End Yes No No Yes Print GCF of A & B Read A & B

19
Sorting Lists oProblem An instructor found that student scores in the midterm were in descending order when he arranged student names alphabetically. Later on, the student names and scores got mixed up. Help the instructor recover correct score of each student. An instructor found that student scores in the midterm were in descending order when he arranged student names alphabetically. Later on, the student names and scores got mixed up. Help the instructor recover correct score of each student. oSolution Sort names & scores Sort names & scores Limitation You can view only two data elements simultaneously You can view only two data elements simultaneously

20
Sorting lists Sultan Qadeer Aamir Zubair Khan 90 45 98 54 87 Your Task: Come up with a method to sort the two lists

21
Sorting Lists QadeerAamir Sultan Zubair Khan QadeerAamir Sultan Khan Zubair Question: When should we stop? How do we know that the list is sorted? Remember; we can see only two data points simultaneously. Khan Zubair Aamir QadeerSultan Khan Zubair Aamir SultanQadeer Khan Zubair Sultan AamirQadeer

22
Bubble 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 2 nd 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 3 rd 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

Similar presentations

© 2017 SlidePlayer.com Inc.

All rights reserved.

Ads by Google