WordPress.com



Algorithm and Flowcharts472070499771A typical programming task can be divided into two phases: Problem solving phase produce an ordered sequence of steps that describe solution of problemthis sequence of steps is called an algorithm Implementation phase implement the program in some programming languageSteps in Problem SolvingFirst produce a pseudocodeRefine the pseudocode successively to get step by step detailed algorithm that is very close to a computer languageWrite the algorithmPseudocode & Algorithm: Example 1: Write an algorithm to determine a student’s final grade and indicate whether it is passing or failing. The final grade is calculated as the average of four marks.Pseudocode:Input a set of 4 marksCalculate their average by summing and dividing by 4if average is below 50Print “FAIL”elsePrint “PASS”endif Detailed Algorithm Step 1: Input M1,M2,M3,M4Step 2: AVERAGE = (M1+M2+M3+M4)/4 Step 3: if (AVERAGE < 50) thenPrint “FAIL” elsePrint “PASS”endifFlowchartFlowchart is a schematic representation of a sequence of operations in a manufacturing process or programInformation system flowcharts show how data flows from source documents through the computer to final distribution to users. Program flowcharts show the sequence of instructions in a single program or subroutine. Different symbols are used to draw each type of flowchart.PseudocodeAlgorithmFlowchartPseudocode is an artificial and informal language that helps programmers to develop algorithms. Pseudocode is similar to everyday English.An algorithm is writing and presenting the solution to a problem in a well defined set of steps or instructions.A recipe in a cookbook is a good example of an algorithmFlowchart is a representation of sequence of steps in a manufacturing process or computer program.Graphical representation of algorithm of a programAdvantages and disadvantages of FlowchartsAdvantagesDisadvantagesCommunication:?Flowcharts are better way of communicating the process graphicallyEffective analysis: With the help of flowchart, problem can be analyzed in more effective wayProper documentation:?Program flowcharts serve as a good program documentation, which is needed for various purposesEfficient Coding: The flowcharts act as a guide or blueprint during the systems analysis and program development phaseProper Debugging: The flowchart helps in detection of errorsEfficient Program Maintenance: The maintenance of operating program becomes easy with the help of flowchart. It helps the programmer to put efforts more efficiently on that partComplex logic: In case of complicated programming, flowchart becomes complex and clumsyAlterations and Modifications: ?If changes are required the flowchart may require re-drawing completely.Reproduction: ?As the flowchart symbols cannot be typed, reproduction of flowchart becomes a problem.The essentials of what is done can be lost in the technical details of how it is doneAdvantages and disadvantages of PseudocodeAdvantagesDisadvantagesIt can be done written and modified easily on a word processorIt can be read and understood easily?Clarifies algorithms in many casesHelps to detect errors before they become codeIts?implementation is?very?useful?in?structured?design elementsConverting a pseudocode to programming language is easy as compared with converting a flowchart to?programming languageIt's not visual?and it doesn’t offer clear picture of program like algorithm and flowchartIt creates an additional level of documentation to maintaintriggers possibilities of errors in translating to code45720295910Step 1: Input M1,M2,M3,M4Step 2: AVERAGE = (M1+M2+M3+M4)/4 Step 3: if (AVERAGE < 50) then Print “FAIL” else Print “PASS” endifSTARTInputM1,M2,M3,M4AVERAGE= (M1+M2+M3+M4)/4ISAVERAGE <50STOPYNPrint “Pass”Print “FAIL”There is no standardized style or format of pseudocode, so one?pseudocode may be different from anotherA Flowchartshows logic of an algorithmemphasizes individual steps and their interconnectionse.g. control flow from one action to the nextConsider the following flowchart for exampleExample 2Write an algorithm and draw a flowchart to convert the length in feet to centimeter.Pseudocode:Input the length in feet (Lft)Calculate the length in cm (Lcm) by multiplying LFT with 30Print length in cm (LCM)76835-635Algorithm Step 1: Input LftStep 2: Lcm = Lft x 30 Step 3: Print LcmSTARTInputLftLcm = Lft x 30STOPFlowchart Print LCMExample 3Write an algorithm and draw a flowchart that will read the two sides of a rectangle and calculate its area. Pseudocode Input the width (W) and Length (L) of a rectangleCalculate the area (A) by multiplying L with WPrint AAlgorithm Step 1: Input W, LStep 2: A = L x W Step 3: Print ASTARTInputW, LA = L x WSTOPPrint A441007566675isA>BYNPrint APrint BDECISION STRUCTURESThe expression A>B is a logical expression It describes a condition we want to test If A>B is true (if A is greater than B) we take the action on left print the value of A If A>B is false (if A is not greater than B) we take the action on right print the value of B 2743200-76200The algorithm for the flowchart is as follows:If A>B then print Aelse print BendifisA>BYNPrint APrint BIF–THEN–ELSE STRUCTUREThe structure is as follows If condition then true alternative else false alternativeendif Relational OperatorsRelational Operators>Greater thanGreater than or equal to<Less thanLess than or equal to=Is equal toNot equal to459740058420MAX = VALUE1STOPYNSTARTInputVALUE1,VALUE2MAX = VALUE2isVALUE1 > VALUE2Print MaxExample 5Write an algorithm that reads two values, determines the largest value and prints the largest value with an identifying message.ALGORITHMStep 1: Input VALUE1, VALUE2Step 2: if (VALUE1 > VALUE2) then MAX = VALUE1else MAX = VALUE2endif Step 3: Print “The largest value is”, MAXNESTED IFSNested ifs are one of the alternatives within an IF–THEN–ELSE statement. It may involve further IF–THEN–ELSE statement ExampleWrite an algorithm that reads three numbers and prints the value of the largest number.Step 1: Input N1, N2, N3Step 2: if (N1>N2) then if (N1>N3) then MAX = N1[N1>N2, N1>N3] else MAX = N3[N3>N1>N2] endif else if (N2>N3) then MAX = N2[N2>N1, N2>N3] else MAX = N3[N3>N2>N1] endif endif Step 3: Print “The largest number is”, MAX Exercise: Draw the flowchart of the above AlgorithmLoopA loop is a series of commands that will continue to repeat over and over again until a condition is met. For example, you want to print your name for five times. Instead of keeping five output statements you can have loop statement and one input statement 380047547625StartCount=1Count <=5Print “your name”Count = count+1yesEndNoAlgorithm to print your name for five timesStep 1 : count=1Step 2: while (count <=5)2.aprint “your name”2.bcount=count+1[end of while] Exercises on LoopWrite an algorithm and draw a flowchart to print 1 to 100 using loopWrite an algorithm and draw a flowchart to print all even number between 50 to 100 using loopWrite an algorithm and draw a flowchart to print 40 to 10 in reverse order using loop (40 39 38…………………..10) Important termsVariable: A?variable?is a?storage location?and an associated?symbolic name?(an?identifier) which contains some known or unknown quantity or information, a?value. The variable name is the usual way to?reference?the stored value.Subroutine: A?subroutine?is a sequence of program instructions that perform a specific task, packaged as a unit. This unit can then be used in programs wherever that particular task should be performed. Subprograms may be defined within programs, or separately in?libraries?that can be used by multiple programs. Answer the following questions.Explain pseudocode, algorithm and flowchartsDifferentiate among pseudocode, algorithm and flowchartsExplain advantages and disadvantages of pseudocodeExplain advantages and disadvantages of flowchartsExplain decision structure and IF-THEN-ELSE structure with the help of exampleWrite various relational operators and state what they representExplain Nested IFS with the help of exampleWhat is iteration or looping?Explain variable and subroutineName and explain various flowchart symbols and their functionsExplain the phases of creating a programWrite step by step working of problem solving phase of a processWhat are the basic elements of flowchart?Discuss uses of flowchartsName various structures of flowcharts ................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download