This document provides examples of algorithms and explains key aspects of algorithms. It begins with four examples of algorithms: 1) adding two numbers, 2) finding the largest of three numbers, 3) solving a quadratic equation, and 4) calculating a factorial. It then discusses qualities of good algorithms such as being precise, unambiguous, and effective. Finally, it provides an example algorithm to check if a number is prime and explains that algorithms are sets of instructions, not computer code. Flowcharts are also discussed as a way to represent algorithms graphically.
This document provides examples of algorithms and explains key aspects of algorithms. It begins with four examples of algorithms: 1) adding two numbers, 2) finding the largest of three numbers, 3) solving a quadratic equation, and 4) calculating a factorial. It then discusses qualities of good algorithms such as being precise, unambiguous, and effective. Finally, it provides an example algorithm to check if a number is prime and explains that algorithms are sets of instructions, not computer code. Flowcharts are also discussed as a way to represent algorithms graphically.
This document provides examples of algorithms and explains key aspects of algorithms. It begins with four examples of algorithms: 1) adding two numbers, 2) finding the largest of three numbers, 3) solving a quadratic equation, and 4) calculating a factorial. It then discusses qualities of good algorithms such as being precise, unambiguous, and effective. Finally, it provides an example algorithm to check if a number is prime and explains that algorithms are sets of instructions, not computer code. Flowcharts are also discussed as a way to represent algorithms graphically.
Introduction to Programming Display a is the largest
number. Algorithm: In programming, algorithm Else are the set of well defined instruction in Display c is the largest sequence to solve a program. OR The number. sequence of steps to be performed in order Else to solve a problem by the computer is If b>c known as an algorithm. An algorithm Display b is the largest should always have a clear stopping number. point. Else Display c is the greatest Qualities of a good algorithm number. 1. Inputs and outputs should be defined Step 5: Stop precisely. Example 3: Write an algorithm to find 2. Each steps in algorithm should be clear all roots of a quadratic equation and unambiguous. ax 2 +bx+c=0. 3. Algorithm should be most effective Step 1: Start among many different ways to solve a Step 2: Declare variables a, b, c, D, problem. x1, x2, rp and ip; 4. An algorithm shouldn't have computer Step 3: Calculate discriminant code. Instead, the algorithm should be D←b2-4ac written in such a way that, it can be used Step 4: If D≥0 in similar programming languages. r1←(-b+√D)/2a r2←(-b-√D)/2a Examples Of Algorithms In Display r1 and r2 as roots. Programming Else Example 1: Write an algorithm to add Calculate real part and two numbers entered by user. imaginary part Step 1: Start rp←b/2a Step 2: Declare variables num1, num2 ip←√(-D)/2a and sum. Display rp+j(ip) and rp-j(ip) Step 3: Read values num1 and num2. as roots Step 4: Add num1 and num2 and Step 5: Stop assign the result to sum. Example 4: Write an algorithm to find sum←num1+num2 the factorial of a number entered by Step 5: Display sum user. Step 6: Stop Step 1: Start Step 2: Declare variables n,factorial Example 2: Write an algorithm to find and i. the largest among three different Step 3: Initialize variables numbers entered by user. factorial←1 Step 1: Start i←1 Step 2: Declare variables a,b and c. Step 4: Read value of n Step 3: Read variables a,b and c. Step 5: Repeat the steps until i=n Step 4: If a>b 5.1: factorial←factorial*i 5.2: i←i+1 else Step 6: Display factorial print "number odd" Step 7: Stop endif Example 5: Write an algorithm to step 5 : stop check whether a number entered by user is prime or not. Note: Algorithm is not the computer Step 1: Start code. Algorithm are just the instructions Step 2: Declare variables n,i,flag. which gives clear idea to you idea to Step 3: Initialize variables write the computer code. flag←1 i←2 Three reasons for using algorithms are Step 4: Read n from user. efficiency, abstraction and reusability. Step 5: Repeat the steps until i<(n/2) Efficiency: Certain types of problems, like 5.1 If remainder of n÷i equals 0 sorting, occur often in computing. Efficient flag←0 algorithms must be used to solve such Go to step 6 problems considering the time and cost 5.2 i←i+1 factor involved in each algorithm. Step 6: If flag=0 Abstraction: Algorithms provide a level of Display n is not prime abstraction in solving problems because else many seemingly complicated problems can Display n is prime be distilled into simpler ones for which well Step 7: Stop known Example 6: Write an algorithm to find algorithms exist. Once we see a more the Fibonacci series till term≤1000. complicated problem in a simpler light, we Step 1: Start can think of the simpler problem as just an Step 2: Declare variables abstraction of the more complicated one. For first_term,second_term and temp. example, imagine trying to find the shortest Step 3: Initialize variables way to route a packet between two gateways first_term←0 second_term←1 in an internet. Once we realize that this Step 4: Display first_term and problem is just a variation of the more second_term general shortest Step 5: Repeat the steps until path problem, we can solve it using the second_term≤1000 generalized approach. 5.1: temp←second_term Reusability: Algorithms are often reusable 5.2: in many different situations. Since many second_term←second_term+first term well-known algorithms are the 5.3: first_term←temp generalizations of more complicated ones, 5.4: Display second_term and since many Step 6: Stop complicated problems can be distilled into simpler ones, an efficient means of solving Example 7: Write a algorithm to find out certain simpler problems potentially lets us number is odd or even? solve many complicated problems. step 1 : start step 2 : input number Flow chart: step 3 : rem=number mod 2 A Flowchart is a type of diagram (graphical step 4 : if rem=0 then or symbolic) that represents an algorithm or print "number even" process. Each step in the process is represented by a different symbol and easy with the help of flowchart. It helps the contains a short description of the process programmer to put efforts more efficiently step. The flow chart symbols are linked on that part. together with arrows showing the process Limitations of Using Flowcharts: flow direction. A flowchart typically shows Although a flowchart is a very useful tool, the flow of data in a process, detailing the there are a few limitations in using operations/steps in a pictorial format which flowcharts which are listed below: is easier to understand than in a textual _ Complex logic: Sometimes, the program format. logic is quite complicated. In that case, A flowchart describes what operations (and flowchart becomes complex and clumsy. in what sequence) are required to solve a _ Alterations and Modifications: If given problem. A flowchart can be likened alterations are required the flowchart may to the blueprint of a building. Flowcharts are require re-drawing completely. used in analyzing, designing, documenting or managing a process or program in various When to Use a Flowchart: fields. _ To communicate to others how a process Flowcharts are generally drawn in the early is done. stages of formulating computer solutions. _ A flowchart is generally used when a new Flowcharts often facilitate communication project begins in order to plan for the between programmers and business people. project. These flowcharts play a vital role in the _ A flowchart helps to clarify how things are programming of a problem and are quite currently working and how they could be helpful in understanding the logic of improved. It also assists in finding the key complicated and lengthy problems. Once the elements of a process, while drawing clear flowchart is drawn, it becomes easy to write lines between where one process ends and the program in any high level language. the next one starts. _ Developing a flowchart stimulates Advantages of Using Flowcharts: communication among participants and The benefits of flowcharts are as follows: establishes a common understanding about _ Communication: Flowcharts are better the process. Flowcharts also uncover steps way of communicating the logic of a system that are redundant or misplaced. to all concerned. _ Flowcharts are used to help team _ Effective analysis: With the help of members, to identify who provides inputs or flowchart, problem can be analysed in more resources to whom, to establish important effective way. areas for monitoring or data collection, to _ Proper documentation: Program identify areas for flowcharts serve as a good program improvement or increased efficiency, and to documentation, which is needed for various generate hypotheses about causes. purposes. _ It is recommended that flowcharts be _ Efficient Coding: The flowcharts act as a created through group discussion, as guide or blueprint during the systems individuals rarely know the entire process analysis and program development phase. and the communication contributes to _ Proper Debugging: The flowchart helps in improvement. debugging process. _ Flowcharts are very useful for _ Efficient Program Maintenance: The documenting a process (simple or complex) maintenance of operating program becomes as it eases the understanding of the process. _ Flowcharts are also very useful to communicate to others how a process is performed and enables understanding of the logic of a process. Input/Output Data: A parallelogram that Flowchart Symbols & Guidelines: indicates data input or output (I/O) for a Flowcharts are usually drawn using some process. Examples: Get X from the user, standard symbols; however, some special Display X. symbols can also be developed when required. Some standard symbols, which are frequently required for flowcharting many computer programs are shown. Terminator: An oval flow chart shape indicates the start or end of the process, Arrow: used to show the flow of control in usually containing the word “Start” or a process. An arrow coming from one “End”. symbol and ending at another symbol represents that control passes to the symbol the arrow points to.
These are the basic symbols used generally.
Process: A rectangular flow chart shape Now, the basic guidelines for drawing a indicates a normal/generic process flow step. flowchart with the above symbols are that: For example, “Add 1 to X”, “M = M*F” or _ In drawing a proper flowchart, all similar. necessary requirements should be listed out in logical order. _ The flowchart should be neat, clear and easy to follow. There should not be any room for ambiguity in understanding the Decision: A diamond flow chart shape flowchart. indicates a branch in the process flow. This _ The flowchart is to be read left to right or symbol is used when a decision needs to be top to bottom. made, commonly a Yes/No question or _ A process symbol can have only one flow True/False test. line coming out of it.
Connector: A small, labelled, circular flow
chart shape used to indicate a jump in the process flow. Connectors are generally used in complex or multi-sheet diagrams. Example 1: Example 3:
Python Advanced Programming: The Guide to Learn Python Programming. Reference with Exercises and Samples About Dynamical Programming, Multithreading, Multiprocessing, Debugging, Testing and More