Cmp104 lec 7 algorithm and flowcharts
Upcoming SlideShare
Loading in...5
×
 

Cmp104 lec 7 algorithm and flowcharts

on

  • 8,080 views

 

Statistics

Views

Total Views
8,080
Views on SlideShare
8,080
Embed Views
0

Actions

Likes
2
Downloads
143
Comments
0

0 Embeds 0

No embeds

Accessibility

Categories

Upload Details

Uploaded via as Microsoft PowerPoint

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment
  • Conveys Better Meaning Since a flowchart is a pictorial representation of a program, it is easier for a programmer to understand and explain the logic of the program to some other programmer. Analyses the Problem Effectively A macro flowchart that charts the main line of logic of a software system becomes a system model that can be broken down into detailed parts for study and further analysis of the system. Effective Joining of a Part of a System A group of programmers are normally associated with the design of large software systems. Each programmer is responsible for designing only a part of the entire system. So initially, if each programmer draws a flowchart for his part of design, the flowcharts of all the programmers can be placed together to visualize the overall system design. Any problem in linking the various parts of the system can be easily detected at this stage and the design can be accordingly modified. Flowcharts can thus be used as working models in the design of new programs and software systems. Efficient Coding :-Once a flowchart is ready, programmers find it very easy to write the concerned program because the flowchart acts as a roadmap for them. It guides them in proceeding from the starting point of the program to the final point ensuring that no steps are omitted. The ultimate result is an error free program developed at a faster rate. Systematic Testing Testing is the process of confirming whether a program will successfully do all the jobs for which it has been designed under the specified constraints. For testing a program, different sets of data are fed as input to that program to test the different paths in the program logic.

Cmp104 lec 7 algorithm and flowcharts Cmp104 lec 7 algorithm and flowcharts Presentation Transcript

  • Flow Charts, Algorithm, Pseudo Code
  • Algorithm• A set of step-by-step instructions to accomplish a task. – An algorithm must have start instruction – Each instruction must be precise. – Each instruction must be unambiguous. – Each instruction must be executed in finite time. – An algorithm must have stop instruction.
  • Algorithm Example 1• Suppose you are given a 1. Start set of mark sheets 2. Take a mark sheet and where each mark sheet read the grade. bears A, B, C or F letter 3. Print the grade grades. 4. Stop• Write an algorithm to read mark sheet and print the grade that it contains.
  • Algorithm Example 2• Suppose you are given a 1. Start set of mark sheets 2. Take a mark sheet and where each mark sheet read the grade. bears A, B, C or F 3. If grade is A then Print grades. the gradeWrite an algorithm to 4. Stop read a mark sheet and print the grade if the grade is A only.
  • Algorithm Example 3• Suppose you are given a 1. Start set of mark sheets 2. Take a mark sheet and where each mark sheet read the grade. bears A, B, C or F 3. If grade is A or B then grades. Print the gradeWrite an algorithm to 4. Stop read a mark sheet and print the grade if it is A or B only.
  • Algorithm representation• A pseudo code Pseudocode Example 1 Start• A flowchart Take a mark sheet and read the grade.• Programs statements in Print the grade a programming Stop language. Pseudocode Example 2 Start Take a mark sheet and read the grade. If grade is A then Print the grade Stop
  • Pseudo Code• Pseudo code is another • Pseudo code, therefore, program analysis tool is an imitation of actual that is used for planning computer instructions. program logic. • These pseudo• "Pseudo" means instructions are phrases imitation or false written in ordinary• "Code" refers to the natural language (e.g., instructions written in a English). programming language.
  • Pseudo code Structure• Pseudo code is made up • Sequence of the following basic • Selection logic structures that (IF...THEN...ELSE or have been proved to be IF....THEN) sufficient for writing • Iteration (DO...WHILE or any computer program. REPEAT...UNTIL)
  • Advantages of pseudo code• Writing of pseudocode • It is easier to modify the involves much less time pseudocode of a and effort than drawing program logic when an equivalent flowchart. program modifications• Converting a pseudo are necessary. code to a programming language is much more easier as compared to converting a flowchart.
  • Limitations of Pseudocode• In case of pseudo code, • communication a graphic representa- problems occur due to tion of program logic is lack of standardization. not available. • For a beginner, it is• There are no standard more difficult to follow rules to follow in using the logic or write the pseudocode. pseudo code, as• Different programmers compared to use their own style of flowcharting. writing pseudocode
  • FlowchartExample 1 STAR Example 2 T STAR T Grade Grade Grade = A Grade Yes No Grade STOP STOP
  • What is a Flowchart? START Display message “How many hours did you work?”• A flowchart is a Read Hours diagram that depicts Display message “How the “flow” of a much do you get paid per hour?” program. Read Pay Rate• The figure shown here is a flowchart for the Multiply Hours by Pay Rate. pay-calculating Store result in Gross Pay. program. Display Gross Pay END
  • Basic Flowchart START Rounded RectangleSymbols Display message “How many hours did you work?” • Notice there are three Read Hours types of symbols in this Display message “How flowchart: much do you get paid per Parallelogra m hour?” – rounded rectangles Read Pay Rate – parallelograms – a rectangle Multiply Hours by Pay Rate. Rectangle Store result in • Each symbol represents Gross Pay. a different type of Display Gross Rounded operation. Pay Rectangle END
  • Basic Flowchart START TerminalSymbols Display message “How many hours did you work?” • Terminals Read Hours – represented by rounded Display message rectangles “How much do you get paid per hour?” – indicate a starting or ending point Read Pay Rate Multiply Hours by Pay Rate. START Store result in Gross Pay. Display Gross Pay END Terminal END
  • Basic Flowchart STARTSymbols Display message “How many hours did you work?” • Input/Output Operations Read Hours – represented by Display message “How parallelograms much do you Input/Output Operation get paid per – indicate an input or hour?” output operation Read Pay Rate Multiply Hours by Pay Rate. Display Store result in Gross Pay. message “How Read Hours many hours Display Gross Pay did you work?” END
  • Basic Flowchart STARTSymbols Display message “How many hours did you work?” • Processes Read Hours – represented by rectangles Display message “How – indicates a process such as much do you get paid per a mathematical hour?” computation or variable Read Pay Rate assignment Multiply Hours by Pay Process Rate. Store Multiply Hours result in by Pay Rate. Gross Pay. Store result in Display Gross Pay Gross Pay. END
  • Basic Flowchart Symbols INPUT DECISION PROCESS TERMINAL Flow line Output (PRINT) DIRECT ACCESS SequentialSTORAGE Access MAGNETIC DISK
  • Flowchart/Pseudocode examplesif A <<B then if A B then if A < B then Print “BIG B” Print “BIG B” Start Print “ Big B” If blockelse else If A>B then If A>B then else Print “BIG A” Print “BIG A” If A>B then else else A,B Print “BIG A” Print “A=B” Print “A=B” else else block endif endif Print “A=B”endif endif endif A:B endif A<B A>B A=B BIG B A=B BIG A Stop Find the biggest of the two numbers A and B
  • Flowchart/Pseudocode examplesFind the largest ofthree numbers A, B Startand C. A B C 50 30 40 A,B,C A<B A>B A:B A=B A=B B<C B>C A<C A>C B:C A:CBIG C B=C BIG B BIG C A=C BIG A Stop
  • Flowchart/Pseudocode Selection Start Start Input A,B,CIf A < B then A,B,C Print “A<B” A<B A>B If B < C then A:B Print “Big C” A=B A>B A< B else A=B if B = C then B<C B>=C A<=C A>C B:C A:C Print “ B=C” else B=C B>C A<=C A=C Print “Big B” BIG C B:C A:C BIG A end if end if B=C BIG Belse BIG C A=C Stopend if
  • Flowcharts Start• Given the input data: – Student Roll number, student name, the marks obtained in Roll, name, 5 subjects, each subject m1,m2,m3,m4,m5 having maximum marks 100. Total = m1+m2+m3+m4+m5• Draw a flowchart for Percentage = the algorithm to: Total/5*100 – Calculate the percentage marks obtained , and Roll, name, percentage – Print student’s roll number and percentage of marks. Stop
  • Compute percentages for three students and print Start their roll numbers, names, and percentage of marks. Student no=1 Student no 1 2 3 Roll, name, m1,m2,m3,m4,m5 Roll 25 35 27 name A B C Total = m1+m2+m3+m4+m5 m1 70 80 60 m2 80 70 45 Percentage = Total/5 m3 80 70 90 Student m4 90 70 60no=Student+1 m5 40 50 30 Roll, name, percentage Total 360 340 285 Percentag 72 68 57 Is e student Variables or First Second Third Yes no <= 3 iteration iteration Memory iteration No Stop address
  • Compute percentages for many students and print Start their roll numbers, names, and percentage of marks. Roll, name, m1,m2,m3,m4,m5 Roll 25 35 27 name A B C Yes Roll =0 m1 70 80 60 m2 80 70 45 No m3 80 70 90 Total = m1+m2+m3+m4+m5 m4 90 70 60 m5 40 50 30 Total 360 340 285 Percentage = Total/5 Percentag 72 68 57 e Variables or First Second Third Roll, name, percentage Memory iteration iteration iteration addressStop
  • Flowchart Selection Start Start Input A, B A,B if A > B then Then block Print “ A is bigger” end if end YesIs A>B? “ A is bigger ” No Stop
  • Flowchart Selection Start Start Input A, B if A > B then A,B then block else else block Print “ I am watching” No end ifIs A>B? end “ I am watching” Yes Stop
  • Flowchart Selection Start Start Input A, B A,B if A > B then then block X=A*B Print X elseYes Is No if B=0 then A>B? Print “ B is zero” else block else Is X=A/B B=0? Print X X=A*B No end if Yes X=A/B end if end X B is zero Stop
  • Flowchart Selection Start Start Input A, B A,B if A > B then then block X=A*B Print X else if B=0 then Yes Print “ B is zero”No Is else A>B? else if block X=A/B Is Print X B=0? end if X=A*B No end Yes X=A/B X B is zero Stop
  • Flowchart Selection (Case)Start start A Input A If A =1 then Print “X” Is A X elseif A =2 then =1 Print “Y” elseif A=3 then start Is A Print “Z” Y Input A =2 else case A of Print “W” 1: Print “X” endif Is A 2: Print “Y” Z end =3 3: Print “Z” otherwise: Print “W” endcase W end Stop
  • Flowchart Iteration Start FOR LOOP Start for Count = 1 to 5 by 1 do Range Print Count Count=1 end for end WHILE LOOP StartCount=+1 Count Count = 1 while Count <= 5 doNo Print Count Is Range Count=Count+1 count=5? end while end Yes REPEAT LOOP Start Stop Count = 1 repeat Print Count Range Count=Count+1 until count > 5 end
  • Flowchart Iteration Start Start Input N N for I = 1 to N by 1 do i=1 Input Roll, Name, m1, m2, m3, m4, m5 Print Roll, Name, m1, m2, m3, m4, m5 end for Roll, Name, end m1,m2,m3,m4,m5 Start i=+1 Input N Roll, Name, i =1Ye m1,m2,m3,m4,m5 repeats Input Roll, Name, m1, m2, m3, m4, m5 Print Roll, Name, m1, m2, m3, m4, m5 i++ Is I < N ? until i > N No end Stop
  • Advantages of Flowchart1) Conveys Better Meaning2) Analyses the Problem Effectively3) Effective Joining of a Part of a SystemA group of programmers are normally associated with the designof large software systems. Each programmer is responsible fordesigning only a part of the entire system. So initially, if eachprogrammer draws a flowchart for his part of design, theflowcharts of all the programmers can be placed together tovisualize the overall system design. Any problem in linking thevarious parts of the system can be easily detected at this stageand the design can be accordingly modified. Flowcharts can thusbe used.4) Efficient Coding5) Systematic Testing
  • Limitations of Flowcharts1) Takes More Time to Draw2) Difficult to Make Changes:- Owing to the symbol-string natureof flowcharting, any changes or modifications in the program logicwill usually require a completely new flowchart. Redrawing aflowchart is tedious.3) Non-standardization :- There are no standards determining theamount of detail that should be included in a flowchart.
  • Informal definition of an algorithm used in a computer
  • Finding the largest integer among five integers
  • Defining actions in FindLargest algorithm
  • FindLargest refined
  • Generalization of FindLargest
  • Example 1 Write an algorithm in pseudocode that finds the average of two numbersSolution See Algorithm 8.1 on the next slide.
  • Algorithm 8.1: Average of two AverageOfTwo Input: Two numbers1. Add the two numbers2. Divide the result by 23. Return the result by step 2 End
  • Example 2 Write an algorithm to change a numeric grade to a pass/no pass grade.Solution See Algorithm 8.2 on the next slide.
  • Algorithm 8.2: Pass/no pass Grade Pass/NoPassGrade Input: One number1. if (the number is greater than or equal to 33) then 1.1 Set the grade to “pass” else 1.2 Set the grade to “nopass” End if2. Return the grade End
  • Example 3 Write an algorithm to change a numeric grade to a letter grade.Solution See Algorithm 8.3 on the next slide.
  • Algorithm 8.3: Letter grade Letter Grade Input: One number1. if (the number is between 90 and 100, inclusive) then 1.1 Set the grade to “A” End if2. if (the number is between 80 and 89, inclusive) then 2.1 Set the grade to “B” End if
  • Algorithm 8.3: Letter grade3. if (the number is between 70 and 79, inclusive) then 3.1 Set the grade to “C” End if4. if (the number is between 60 and 69, inclusive) then 4.1 Set the grade to “D” End if
  • Algorithm 8.3: Letter grade5. If (the number is less than 60) then 5.1 Set the grade to “F” End if6. Return the grade End
  • Example 4 Write an algorithm to find the largest of a set of numbers. You do not know the number of numbers.Solution See Algorithm 8.4 on the next slide.
  • Algorithm 8.4: Find largest Find Largest Input: A list of positive integers1. Set Largest to 02. while (more integers) 2.1 if (the integer is greater than Largest) then 2.1.1 Set largest to the value of the integer End if End while3. Return Largest End
  • Example 5 Write an algorithm to find the largest of 1000 numbers.Solution See Algorithm 8.5 on the next slide.
  • Algorithm 8.5:Find largest of 1000 numbers FindLargest Input: 1000 positive integers1. Set Largest to 02. Set Counter to 03. while (Counter less than 1000) 3.1 if (the integer is greater than Largest) then 3.1.1 Set Largest to the value of the integer End if 3.2 Increment Counter End while4. Return Largest End
  • 8.4 MORE FORMA DEFINITION •Ordered set •Unambiguous steps •Effectiveness •Termination
  • 8.5 SUBALGORITHMS
  • Concept of a subalgorithm
  • Algorithm 8.6: Find largest FindLargest Input: A list of positive integers1. Set Largest to 02. while (more integers) 2.1 FindLarger End while3. Return Largest End
  • Subalgorithm: Find larger FindLarger Input: Largest and current integer1. if (the integer is greater than Largest) then 1.1 Set Largest to the value of the integer End if End