Like this presentation? Why not share!

2,943

-1

-1

Published on

Re-uploaded

No Downloads

Total Views

2,943

On Slideshare

0

From Embeds

0

Number of Embeds

0

Shares

0

Downloads

0

Comments

0

Likes

3

No embeds

No notes for slide

- 1. Computer Fundamentals: Pradeep K. Sinha & Priti SinhaComputer Fundamentals: Pradeep K. Sinha & Priti SinhaSlide 1/44Chapter 11: Planning the Computer ProgramRef Page
- 2. Computer Fundamentals: Pradeep K. Sinha & Priti SinhaComputer Fundamentals: Pradeep K. Sinha & Priti SinhaSlide 2/44Chapter 11: Planning the Computer ProgramRef PageIn this chapter you will learn about:§ Programs must be planned before they are written§ Algorithm§ Flowchart§ Pseudocode§ Plan the logic of a computer program§ Commonly used tools for program planning andtheir useLearning ObjectivesLearning Objectives183
- 3. Computer Fundamentals: Pradeep K. Sinha & Priti SinhaComputer Fundamentals: Pradeep K. Sinha & Priti SinhaSlide 3/44Chapter 11: Planning the Computer ProgramRef PagePurpose of Program PlanningPurpose of Program Planning§ To write a correct program, a programmer must writeeach and every instruction in the correct sequence§ Logic (instruction sequence) of a program can be verycomplex§ Hence, programs must be planned before they arewritten to ensure program instructions are:§ Appropriate for the problem§ In the correct sequence183
- 4. Computer Fundamentals: Pradeep K. Sinha & Priti SinhaComputer Fundamentals: Pradeep K. Sinha & Priti SinhaSlide 4/44Chapter 11: Planning the Computer ProgramRef PageAlgorithmAlgorithm§ Refers to the logic of a program and a step-by-stepdescription of how to arrive at the solution of a givenproblem§ In order to qualify as an algorithm, a sequence ofinstructions must have following characteristics:§ Each and every instruction should be precise andunambiguous§ Each instruction should be such that it can be performed ina finite time§ One or more instructions should not be repeated infinitely.This ensures that the algorithm will ultimately terminate§ After performing the instructions, that is after the algorithmterminates, the desired results must be obtained184
- 5. Computer Fundamentals: Pradeep K. Sinha & Priti SinhaComputer Fundamentals: Pradeep K. Sinha & Priti SinhaSlide 5/44Chapter 11: Planning the Computer ProgramRef PageSample Algorithm (Example 1)Sample Algorithm (Example 1)There are 50 students in a class who appeared in theirfinal examination. Their mark sheets have been given toyou.The division column of the mark sheet contains thedivision (FIRST, SECOND, THIRD or FAIL) obtained by thestudent.Write an algorithm to calculate and print the total numberof students who passed in FIRST division.184
- 6. Computer Fundamentals: Pradeep K. Sinha & Priti SinhaComputer Fundamentals: Pradeep K. Sinha & Priti SinhaSlide 6/44Chapter 11: Planning the Computer ProgramRef PageStep 1: Initialize Total_First_Division andTotal_Marksheets_Checked to zero.Step 2: Take the mark sheet of the next student.Step 3: Check the division column of the mark sheet to see if it isFIRST, if no, go to Step 5.Step 4: Add 1 to Total_First_Division.Step 5: Add 1 to Total_Marksheets_Checked.Step 6: Is Total_Marksheets_Checked = 50, if no, go to Step 2.Step 7: Print Total_First_Division.Step 8: Stop.Sample Algorithm (Example 1)Sample Algorithm (Example 1)(contd…)184
- 7. Computer Fundamentals: Pradeep K. Sinha & Priti SinhaComputer Fundamentals: Pradeep K. Sinha & Priti SinhaSlide 7/44Chapter 11: Planning the Computer ProgramRef PageThere are 100 employees in an organization. The organizationwants to distribute annual bonus to the employees based on theirperformance. The performance of the employees is recorded intheir annual appraisal forms.Every employee’s appraisal form contains his/her basic salary andthe grade for his/her performance during the year. The grade is ofthree categories – ‘A’ for outstanding performance, ‘B’ for goodperformance, and ‘C’ for average performance.It has been decided that the bonus of an employee will be 100% ofthe basic salary for outstanding performance, 70% of the basicsalary for good performance, 40% of the basic salary for averageperformance, and zero for all other cases.Write an algorithm to calculate and print the total bonus amount tobe distributed by the organization.Sample Algorithm (Example 2)Sample Algorithm (Example 2)185
- 8. Computer Fundamentals: Pradeep K. Sinha & Priti SinhaComputer Fundamentals: Pradeep K. Sinha & Priti SinhaSlide 8/44Chapter 11: Planning the Computer ProgramRef PageStep 1: Initialize Total_Bonus and Total_Employees_Checked to zero.Step 2: Initialize Bonus and Basic_Salary to zero.Step 3: Take the appraisal form of the next employee.Step 4: Read the employee’s Basic_Salary and Grade.Step 5: If Grade = A, then Bonus = Basic_Salary. Go to Step 8.Step 6: If Grade = B, then Bonus = Basic_Salary x 0.7. Go to Step 8.Step 7: If Grade = C, then Bonus = Basic_Salary x 0.4.Step 8: Add Bonus to Total_Bonus.Step 9: Add 1 to Total_Employees_Checked.Step 10: If Total_Employees_Checked < 100, then go to Step 2.Step 11: Print Total_Bonus.Step 12: Stop.Sample Algorithm (Example 2)Sample Algorithm (Example 2)(contd…)185
- 9. Computer Fundamentals: Pradeep K. Sinha & Priti SinhaComputer Fundamentals: Pradeep K. Sinha & Priti SinhaSlide 9/44Chapter 11: Planning the Computer ProgramRef PageRepresentation of AlgorithmsRepresentation of Algorithms§ As programs§ As flowcharts§ As pseudocodesWhen an algorithm is represented in the form of aprogramming language, it becomes a programThus, any program is an algorithm, although thereverse is not true185
- 10. Computer Fundamentals: Pradeep K. Sinha & Priti SinhaComputer Fundamentals: Pradeep K. Sinha & Priti SinhaSlide 10/44Chapter 11: Planning the Computer ProgramRef PageFlowchartFlowchart§ Flowchart is a pictorial representation of an algorithm§ Uses symbols (boxes of different shapes) that havestandardized meanings to denote different types ofinstructions§ Actual instructions are written within the boxes§ Boxes are connected by solid lines having arrow marks toindicate the exact sequence in which the instructions areto be executed§ Process of drawing a flowchart for an algorithm is calledflowcharting186
- 11. Computer Fundamentals: Pradeep K. Sinha & Priti SinhaComputer Fundamentals: Pradeep K. Sinha & Priti SinhaSlide 11/44Chapter 11: Planning the Computer ProgramRef PageBasic Flowchart SymbolsBasic Flowchart SymbolsTerminal ProcessingDecisionInput/OutputFlow lines Connectors187
- 12. Computer Fundamentals: Pradeep K. Sinha & Priti SinhaComputer Fundamentals: Pradeep K. Sinha & Priti SinhaSlide 12/44Chapter 11: Planning the Computer ProgramRef PageExamples of Decision SymbolExamples of Decision SymbolIs I = 10?NoYes(a) A two-way branch decision. (b) A three-way branch decision.A > BA = BA < B CompareA & B188
- 13. Computer Fundamentals: Pradeep K. Sinha & Priti SinhaComputer Fundamentals: Pradeep K. Sinha & Priti SinhaSlide 13/44Chapter 11: Planning the Computer ProgramRef PageI = ?(c) A multiple-way branch decision.= 0 = 1 = 2 = 3 = 4 = 5 = OtherExamples of Decision SymbolExamples of Decision Symbol(contd…)188
- 14. Computer Fundamentals: Pradeep K. Sinha & Priti SinhaComputer Fundamentals: Pradeep K. Sinha & Priti SinhaSlide 14/44Chapter 11: Planning the Computer ProgramRef PageSample Flowchart (Example 3)Sample Flowchart (Example 3)A student appears in an examination, which consists oftotal 10 subjects, each subject having maximum marksof 100.The roll number of the student, his/her name, and themarks obtained by him/her in various subjects aresupplied as input data.Such a collection of related data items, which is treatedas a unit is known as a record.Draw a flowchart for the algorithm to calculate thepercentage marks obtained by the student in thisexamination and then to print it along with his/her rollnumber and name.188
- 15. Computer Fundamentals: Pradeep K. Sinha & Priti SinhaComputer Fundamentals: Pradeep K. Sinha & Priti SinhaSlide 15/44Chapter 11: Planning the Computer ProgramRef PageStartRead input dataAdd marks of allsubjects giving TotalPercentage = Total / 10Write output dataStopSample Flowchart (Example 3)Sample Flowchart (Example 3)(contd…)189
- 16. Computer Fundamentals: Pradeep K. Sinha & Priti SinhaComputer Fundamentals: Pradeep K. Sinha & Priti SinhaSlide 16/44Chapter 11: Planning the Computer ProgramRef Page50 students of a class appear in the examination ofExample 3.Draw a flowchart for the algorithm to calculate and printthe percentage marks obtained by each student alongwith his/her roll number and name.Sample Flowchart (Example 4)Sample Flowchart (Example 4)189
- 17. Computer Fundamentals: Pradeep K. Sinha & Priti SinhaComputer Fundamentals: Pradeep K. Sinha & Priti SinhaSlide 17/44Chapter 11: Planning the Computer ProgramRef PageFlowchart for the solutionof Example 4 with aninfinite (endless) processloop.StartAdd marks of allsubjects giving TotalPercentage = Total / 10Write output dataRead input dataSample Flowchart (Example 4)Sample Flowchart (Example 4)(contd…)190
- 18. Computer Fundamentals: Pradeep K. Sinha & Priti SinhaComputer Fundamentals: Pradeep K. Sinha & Priti SinhaSlide 18/44Chapter 11: Planning the Computer ProgramRef PageFlowchart for the solutionof Example 4.StopStartRead input dataCount = 0Add marks of all subjects giving TotalPercentage = Total/10Write output dataAdd 1 to CountIs Count = 50?NoYesSample Flowchart (Example 4)Sample Flowchart (Example 4)(contd…)191
- 19. Computer Fundamentals: Pradeep K. Sinha & Priti SinhaComputer Fundamentals: Pradeep K. Sinha & Priti SinhaSlide 19/44Chapter 11: Planning the Computer ProgramRef PageSample Flowchart (Example 4)Sample Flowchart (Example 4)Generalized flowchartfor the solution ofExample 4 using theconcept of trailerrecord. Here theprocess loop isterminated by detectinga special non-datarecord.StopYesStartAdd marks of all subjectsgiving TotalPercentage = Total / 10NoIs Rollno = 0000000?Read input dataWrite output data(contd…)191
- 20. Computer Fundamentals: Pradeep K. Sinha & Priti SinhaComputer Fundamentals: Pradeep K. Sinha & Priti SinhaSlide 20/44Chapter 11: Planning the Computer ProgramRef PageFor the examination of Example 3, we want to make alist of only those students who have passed (obtained30% or more marks) in the examination.In the end, we also want to print out the total number ofstudents who have passed.Assuming that the input data of all the students isterminated by a trailer record, which has sentinel valueof 9999999 for Rollno, draw a flowchart for thealgorithm to do this.Sample Flowchart (Example 5)Sample Flowchart (Example 5)192
- 21. Computer Fundamentals: Pradeep K. Sinha & Priti SinhaComputer Fundamentals: Pradeep K. Sinha & Priti SinhaSlide 21/44Chapter 11: Planning the Computer ProgramRef PageIs Percentage = > 30?Percentage = Total/10StartCount = 0Add marks of all subjects giving TotalIs Rollno = 9999999?NoYesAdd 1 to CountRead input dataWrite output dataNoWrite CountStopYesSample Flowchart (Example 5)Sample Flowchart (Example 5)(contd…)193
- 22. Computer Fundamentals: Pradeep K. Sinha & Priti SinhaComputer Fundamentals: Pradeep K. Sinha & Priti SinhaSlide 22/44Chapter 11: Planning the Computer ProgramRef PageSuppose the input data of each student for the examination ofExample 3 also contains information regarding the sex of thecandidate in the field named Sexcode having values M (formale) or F (for female).We want to make a list of only those female students who havepassed in second division (obtained 45% or more but less than60% marks).In the end, we also want to print out the total number of suchstudents.Assuming that the input data of all the students is terminatedby a trailer record, which has a sentinel value of Z for Sexcode,draw a flowchart for the algorithm to do this.Sample Flowchart (Example 6)Sample Flowchart (Example 6)193
- 23. Computer Fundamentals: Pradeep K. Sinha & Priti SinhaComputer Fundamentals: Pradeep K. Sinha & Priti SinhaSlide 23/44Chapter 11: Planning the Computer ProgramRef PageAdd marks of all subjects giving TotalYesYesNoStartCount = 0No1Read input dataIs Sexcode = Z?Is Sexcode = F?12Percentage = Total / 103Sample Flowchart (Example 6)Sample Flowchart (Example 6)195
- 24. Computer Fundamentals: Pradeep K. Sinha & Priti SinhaComputer Fundamentals: Pradeep K. Sinha & Priti SinhaSlide 24/44Chapter 11: Planning the Computer ProgramRef PageNoNoYesYesAdd 1 to CountWrite output dataIs Percentage < 60?Is Percentage = > 45?StopWrite Count21113Sample Flowchart (Example 4)Sample Flowchart (Example 4)(contd…)195
- 25. Computer Fundamentals: Pradeep K. Sinha & Priti SinhaComputer Fundamentals: Pradeep K. Sinha & Priti SinhaSlide 25/44Chapter 11: Planning the Computer ProgramRef PageLevels of FlowchartLevels of Flowchart§ Flowchart that outlines the main segments of a programor that shows less details is a macro flowchart§ Flowchart with more details is a micro flowchart, ordetailed flowchart§ There are no set standards on the amount of details thatshould be provided in a flowchart196
- 26. Computer Fundamentals: Pradeep K. Sinha & Priti SinhaComputer Fundamentals: Pradeep K. Sinha & Priti SinhaSlide 26/44Chapter 11: Planning the Computer ProgramRef PageExample of Micro FlowchartExample of Micro FlowchartPart of a macroflowchartAdd marks of allsubjects giving TotalIs I > 10?1I = 1Total = 0Total = Total + Marks (I)I = I + 11YesNoA microFlowchart196
- 27. Computer Fundamentals: Pradeep K. Sinha & Priti SinhaComputer Fundamentals: Pradeep K. Sinha & Priti SinhaSlide 27/44Chapter 11: Planning the Computer ProgramRef Page§ First chart the main line of logic, then incorporate detail§ Maintain a consistent level of detail for a given flowchart§ Do not chart every detail of the program. A reader who isinterested in greater details can refer to the program itself§ Words in the flowchart symbols should be commonstatements and easy to understandFlowcharting RulesFlowcharting Rules196
- 28. Computer Fundamentals: Pradeep K. Sinha & Priti SinhaComputer Fundamentals: Pradeep K. Sinha & Priti SinhaSlide 28/44Chapter 11: Planning the Computer ProgramRef Page§ Be consistent in using names and variables in theflowchart§ Go from left to right and top to bottom inconstructing flowcharts§ Keep the flowchart as simple as possible. Crossing offlow lines should be avoided as far as practicable§ If a new flowcharting page is needed, it isrecommended that the flowchart be broken at aninput or output point.§ Properly labeled connectors should be used to linkthe portions of the flowchart on different pages(contd…)Flowcharting RulesFlowcharting Rules197
- 29. Computer Fundamentals: Pradeep K. Sinha & Priti SinhaComputer Fundamentals: Pradeep K. Sinha & Priti SinhaSlide 29/44Chapter 11: Planning the Computer ProgramRef PageAdvantages of FlowchartAdvantages of Flowchart§ Better Communication§ Proper program documentation§ Efficient coding§ Systematic debugging§ Systematic testing197
- 30. Computer Fundamentals: Pradeep K. Sinha & Priti SinhaComputer Fundamentals: Pradeep K. Sinha & Priti SinhaSlide 30/44Chapter 11: Planning the Computer ProgramRef PageLimitations of FlowchartLimitations of Flowchart§ Flowcharts are very time consuming and laborious todraw (especially for large complex programs)§ Redrawing a flowchart for incorporating changes/modifications is a tedious task§ There are no standards determining the amount of detailthat should be included in a flowchart198
- 31. Computer Fundamentals: Pradeep K. Sinha & Priti SinhaComputer Fundamentals: Pradeep K. Sinha & Priti SinhaSlide 31/44Chapter 11: Planning the Computer ProgramRef PagePseudocodePseudocode§ A program planning tool where program logic is written inan ordinary natural language using a structure thatresembles computer instructions§ “ Pseudo” means imitation or false and “ Code” refers tothe instructions written in a programming language.Hence, pseudocode is an imitation of actual computerinstructions§ Because it emphasizes the design of the program,pseudocode is also called Program Design Language(PDL)198
- 32. Computer Fundamentals: Pradeep K. Sinha & Priti SinhaComputer Fundamentals: Pradeep K. Sinha & Priti SinhaSlide 32/44Chapter 11: Planning the Computer ProgramRef PageBasic Logic (Control) StructuresBasic Logic (Control) StructuresAny program logic can be expressed by using onlyfollowing three simple logic structures:1. Sequence logic,2. Selection logic, and3. Iteration (or looping) logicPrograms structured by using only these three logicstructures are called structured programs, and thetechnique of writing such programs is known asstructured programming199
- 33. Computer Fundamentals: Pradeep K. Sinha & Priti SinhaComputer Fundamentals: Pradeep K. Sinha & Priti SinhaSlide 33/44Chapter 11: Planning the Computer ProgramRef PageIt is used for performing instructions one after anotherin sequence.Sequence LogicSequence LogicProcess 1(b) PseudocodeProcess 2Process 1Process 2(a) Flowchart199
- 34. Computer Fundamentals: Pradeep K. Sinha & Priti SinhaComputer Fundamentals: Pradeep K. Sinha & Priti SinhaSlide 34/44Chapter 11: Planning the Computer ProgramRef PageSelection LogicSelection Logic• Also known as decision logic, it is used for makingdecisions• Three popularly used selection logic structures are1. IF…THEN…ELSE2. IF…THEN3. CASE200
- 35. Computer Fundamentals: Pradeep K. Sinha & Priti SinhaComputer Fundamentals: Pradeep K. Sinha & Priti SinhaSlide 35/44Chapter 11: Planning the Computer ProgramRef PageSelection Logic (IF…THEN…ELSE Structure)Selection Logic (IF…THEN…ELSE Structure)THENProcess 2IF ConditionProcess 1ELSEENDIF(b) PseudocodeTHENProcess 1ELSEProcess 2Yes No(a) FlowchartIF (condition)200
- 36. Computer Fundamentals: Pradeep K. Sinha & Priti SinhaComputer Fundamentals: Pradeep K. Sinha & Priti SinhaSlide 36/44Chapter 11: Planning the Computer ProgramRef Page(b) PseudocodeTHENIF ConditionProcess 1ENDIFTHENProcess 1Yes No(a) FlowchartIF (condition)Selection Logic (IF…THEN Structure)Selection Logic (IF…THEN Structure)200
- 37. Computer Fundamentals: Pradeep K. Sinha & Priti SinhaComputer Fundamentals: Pradeep K. Sinha & Priti SinhaSlide 37/44Chapter 11: Planning the Computer ProgramRef PageSelection Logic (CASE Structure)Selection Logic (CASE Structure)(b) PseudocodeCase Type 1: Process 1CASE TypeENDCASECase Type 2: Process 2Case Type n: Process nType 1Type 2Type nProcess 2Process 1Process nYesYesYesNoNoNo(a) Flowchart201
- 38. Computer Fundamentals: Pradeep K. Sinha & Priti SinhaComputer Fundamentals: Pradeep K. Sinha & Priti SinhaSlide 38/44Chapter 11: Planning the Computer ProgramRef PageIteration (or Looping) LogicIteration (or Looping) Logic§ Used to produce loops in program logic when one ormore instructions may be executed several timesdepending on some conditions§ Two popularly used iteration logic structures are1. DO…WHILE2. REPEAT…UNTIL201
- 39. Computer Fundamentals: Pradeep K. Sinha & Priti SinhaComputer Fundamentals: Pradeep K. Sinha & Priti SinhaSlide 39/44Chapter 11: Planning the Computer ProgramRef PageIteration (or Looping) Logic(DO…WHILE Structure)Iteration (or Looping) Logic(DO…WHILE Structure)(b) PseudocodeDO WHILE ConditionProcess 1ENDDOProcess nProcess 1False(a) FlowchartProcess nTrueCondition?Block202
- 40. Computer Fundamentals: Pradeep K. Sinha & Priti SinhaComputer Fundamentals: Pradeep K. Sinha & Priti SinhaSlide 40/44Chapter 11: Planning the Computer ProgramRef PageIteration (or Looping) Logic(REPEAT…UNTIL Structure)Iteration (or Looping) Logic(REPEAT…UNTIL Structure)(b) PseudocodeREPEATProcess 1UNTIL ConditionProcess nProcess 1(a) FlowchartProcess nTrueFalseCondition?202
- 41. Computer Fundamentals: Pradeep K. Sinha & Priti SinhaComputer Fundamentals: Pradeep K. Sinha & Priti SinhaSlide 41/44Chapter 11: Planning the Computer ProgramRef PageSample Pseudocode (for Example 6)Sample Pseudocode (for Example 6)Set Count to zeroRead first student recordDO WHILE Sexcode is not equal to ZIF Sexcode = F THENCalculate PercentageIF Percentage = > 45 THENIF Percentage < 60 THENWrite output dataAdd 1 to CountENDIFENDIFENDIFRead next student recordENDDOWrite CountStop203
- 42. Computer Fundamentals: Pradeep K. Sinha & Priti SinhaComputer Fundamentals: Pradeep K. Sinha & Priti SinhaSlide 42/44Chapter 11: Planning the Computer ProgramRef PageAdvantages of PseudocodeAdvantages of Pseudocode§ Converting a pseudocode to a programming languageis much more easier than converting a flowchart to aprogramming language§ As compared to a flowchart, it is easier to modify thepseudocode of a program logic when programmodifications are necessary§ Writing of pseudocode involves much less time andeffort than drawing an equivalent flowchart as it hasonly a few rules to follow204
- 43. Computer Fundamentals: Pradeep K. Sinha & Priti SinhaComputer Fundamentals: Pradeep K. Sinha & Priti SinhaSlide 43/44Chapter 11: Planning the Computer ProgramRef PageLimitations of PseudocodeLimitations of Pseudocode§ In case of pseudocode, a graphic representation ofprogram logic is not available§ There are no standard rules to follow in usingpseudocode§ Different programmers use their own style of writingpseudocode and hence communication problemoccurs due to lack of standardization§ For a beginner, it is more difficult to follow the logicof or write pseudocode, as compared to flowcharting204
- 44. Computer Fundamentals: Pradeep K. Sinha & Priti SinhaComputer Fundamentals: Pradeep K. Sinha & Priti SinhaSlide 44/44Chapter 11: Planning the Computer ProgramRef PageKey Words/PhrasesKey Words/Phrases§ Algorithm§ Basic logic structures§ Control structures§ Flowchart§ Iteration logic§ Looping logic§ Micro flowchart§ Macro flowchart§ Pseudocode§ Program Design Language (PDL)§ Sequence logic§ Selection logic§ Sentinel value§ Structured programming§ Trailer record204

No public clipboards found for this slide

Be the first to comment