Page 1 :
Algorithm, , , , Finite sequence of steps to solve a problem., , Algorithm can be defined as: “A sequence of activities to be processed for getting desired, output from a given input.”, , Webopedia defines an algorithm as: “A formula or set of steps for solving a particular, problem. To be an algorithm, a set of rules must be unambiguousand have a clear, stopping point”. There may be more than one way to solve a problem, so there may be, more than one algorithm for a problem., , Now, if we take definition of algorithm as: “A sequence of activities to be processed for, getting desired output from a given input.”, , Then we can say that:, 1. Getting specified output is essential after algorithm is executed., 2. One will get output only if algorithm stops after finite time., 3. Activities in an algorithm to be clearly defined in other words for it to be unambiguous., , Before writing an algorithm for a problem, one should find out what is/are the inputs to the, algorithm and what is/are expected output after running the algorithm. Now let us take, some exercises to develop an algorithm for some simple problems:, , While writing algorithms we will use following symbol for different operations:, ‘+’ for Addition, ‘” >for Subtraction, “> for Multiplication, ‘l’ >for Division and, ‘€’ for assignment. For exampleA €X*3 means A will have a value of X*3., Guidelines for Developing an Algorithm, Following guidelines must be followed while developing an algorithm :, 1. An algorithm will be enclosed by START (or BEGIN) and STOP (or END)., , 2. To accept data from user, generally used statements are INPUT, READ, GET or, OBTAIN.
Page 2 :
3. To display result or any message, generally used statements are PRINT, DISPLAY, or, WRITE., , 4. Generally, COMPUTE or CALCULATE is used while describing mathematical, expressions and based on situation relevant operators can be used., , Example-1, , Let us take a simple day to day example by writing algorithm for making “Maggi Noodles”, as a food, , Step 1: Start, Step 2: Take pan with water, Step 3: Put pan on the burner, Step 4: Switch on the gas/burner, Step 5: Put magi and masala, Step 6: Give two minutes to boil, Step 7: Take off the pan, Step 8: Take out the maggi with the help of fork/spoon, Step 9: Put the Maggi on the plate and serve it, Step 10: Stop., Example-2, Write an algorithm to read two numbers and find their sum., Inputs to the algorithm: First num1., Second num2., Expected output: Sum of the two numbers., Algorithm: Step1: Start, Step2: Read\input the first num1., , Step3: Read\input the second num2., , Step4: Sum num1+num2 // calculation of sum
Page 3 :
Step5: Print Sum, , Step6: End, , Example-3, , Find the area of a Circle of radius r., , Inputs to the algorithm: Radius r of the Circle., Expected output: Area of the Circle, Algorithm:, , Step1: Start, , Step2:Read\input the Radius r of the Circle, Step3: Area Pl*r*r // calculation of area, Step4: Print Area, , Step6: Stop., , Example-3, , Convert temperature Fahrenheit to Celsius., Inputs to the algorithm: Temperature in Fahrenheit, Expected output: Temperature in Celsius, Algorithm: Step1: Start, , Step 2: Read Temperature in Fahrenheit F, Step 3: C€ 5/9*(F-32), , Step 4: Print Temperature in Celsius: C, Step5: End, , Type of Algorithms, , The algorithm and flowchart, classification to the three types of control structures. They, are:, , 1. Sequence
Page 4 :
2. Branching (Selection), 3. Loop (Repetition), , These three control structures are sufficient for all purposes. The sequence is exemplified, by sequence of statements place one after the other — the one above or before another, gets executed first. In flowcharts, sequence of statements is usually contained in the, rectangular process box., , e The branch refers to a binary decision based on some condition. If the condition is true,, one of the two branches is explored; if the condition is false, the other alternative is taken., This is usually represented by the ‘if-then’ construct in pseudo-codes and programs., , In flowcharts, this is represented by the diamond-shaped decision box. This structure is, also known as the selection structure., , Example-01/04 : write algorithm to find the greater number between two numbers, Step1: Start, , Step2: Read/input A and B, , Step3: If A greater than B then C=A, , Step4: if B greater than A then C=B, , Step5: Print C, , Step6: End, , Example-02/05 :A algorithm to find the largest value of any three numbers., Step1: Start, , Step2: Read/input A,B and C, , Step3: If (A>=B) and (A>=C) then Max=A, , Step4: If (B>=A) and (B>=C) then Max=B, , Step5:If (C>=A) and (C>=B) then Max=C, , Step6: Print Max, , Step7: End
Page 5 :
¢ The loop allows a statement or a sequence of statements to be repeatedly executed, based on some loop condition. It is represented by the ‘while’ and ‘for’ constructs in most, programming languages, for unbounded loops and bounded loops respectively., (Unbounded loops refer to those whose number of iterations depends on the eventuality, that the termination condition is satisfied; bounded loops refer to those whose number of, iterations is known before-hand.) In the flowcharts, a back arrow hints the presence of a, loop. A trip around the loop is known as iteration. You must ensure that the condition for, the termination of the looping must be satisfied after some finite number of iterations,, otherwise it ends up as an infinite loop, a common mistake made by inexperienced, programmers. The loop is also known as the repetition structure., , Example-01/06: An algorithm to calculate even numbers between 0 and 99, Step1. Start, , Step2.1< 0, , Step3. Write | in standard output, , Step4. 1 1+2, , Step5. If (| <=98) then go to step3, , Step6. End, , Example-02/07: Design an algorithm which gets a natural value, n,as its input and, calculates odd numbers equal or less than n. Then write them in the standard output:, , Step1. Start, , Step2. Read n, , Step3.1¢1, , Step4. Write |, , Step5. | < 1+ 2 Step6. If ( <= n) then go to step4, Step7. End, , Problem3: Design an algorithm which generates even numbers between 1000 and 2000 and, then prints them in the standard output. It should also print total sum:, , 1. Start, , 2.1€ 1000 andS ¢ O