You are on page 1of 7

Faculty of Management and Computing

Lecture 3: Algorithms and Flowcharts


The term algorithm (pronounced AL-go-rith-um) is a procedure or formula for solving a problem. In short, algorithm is a step by step problem solving procedure. The word derives from the name of the mathematician, Mohamed ibn-Musa Al-Khowarizmi. A computer program can be viewed as an elaborate algorithm. In mathematics and computer science, an algorithm usually means a small procedure that solves a recurrent problem. In algorithms there is set of procedures to solve the problem in such a manner that solution is definitely obtained. For solving a problem by computer, we have to be very clear in writing the instructions for the computer. The set of instruction is known as program. The solution is obtained by breaking the contents of the problem and given data in such a manner, that all instructions are given in sequence. This procedure is known as algorithm and has to be devised prior to the actual coding of the computer program. Actual computer program is written in coded language known as programming language. Algorithm is thus a design or plan of obtaining a solution to the problem. It is a logical process of analysing a mathematical problem and data step by step so as to make it easier to understand and implement solution to the problem. Certain operations must be performed on data for producing the required results. In order to perform an operation, it must be converted in a form that computer is able to execute. Moreover the operations must be carried out in a specific sequence. Writing algorithm for a solution of a problem is a method of successive iterations. Algorithm is first roughly estimated and written. Then it is carefully gone through a number of times and refined each time. The final form of an algorithm emerges through a number of stepwise refinements carried out successively, till the detailed steps become precisely clear to the person or machine, which is going to execute the steps. In an algorithm, all items of instructions are listed in the order in which they are to be carried out. This is done with the help of flow charts. However complex, the problem may be, it can always be broken into a set of smaller sequence of steps and procedures. We can always think it to be solved with a help of systematic step-by-step problem solving procedures which followed meticulously, will lead us to a definite solution. This step by step procedure is technically known as algorithm. Aims of the Algorithm The design of the algorithm had several aims: o Speed the speed of the program is depending on the structure of the algorithm used in that program to solve the problem. o Good memory utilization - The number of elements that can be sorted should closely approach the physical limits of the machine. To obtain good memory utilization when sorting small elements linked lists are avoided, but instead arrays can be used without any storage overhead for pointers. o Flexibility - The algorithm starts with a number of elements N assumed to be distributed over P processing nodes. No particular distribution of elements is assumed and the only restrictions on the size of N and P are the physical constraints of the machine. So that no restrictions are placed on N and P. In particular N should not need to be a multiple of P or a power of two. Analysis of Algorithms An algorithm is just the outline or idea behind a program. We express algorithms in pseudo code: something resembling using programming languages like C++, but with some statements in English rather than within the programming language. It is expected that one could translate each pseudo code statement to a small number of lines of actual code, easily and mechanically.
Introduction to Programming Page 1 of 7

Faculty of Management and Computing

The purpose of design of algorithms is obvious: one needs an algorithm in order to write a program. Analysis of algorithms is less obviously necessary, but has several purposes: o Analysis can be more reliable than experimentation. If we experiment, we only know the behaviour of a program on certain specific test cases, while analysis can give us guarantees about the performance on all inputs. o It helps one choose among different solutions to problems. As we will see, there can be many different solutions to the same problem. A careful analysis and comparison can help us decide which one would be the best for our purpose, without requiring that all be implemented and tested. o We can predict the performance of a program before we take the time to write code. In a large project, if we waited until after all the code was written to discover that something runs very slowly, it could be a major disaster, but if we do the analysis first we have time to discover speed problems and work on it. o By analyzing an algorithm, we gain a better understanding of where the fast and slow parts are, and what to work on or work around in order to speed it up. Efficient Algorithms The following points must be borne in mind before writing an efficient algorithm: o Every procedure should carefully specify the input and output variables. o The meaning of all variables should be defined. o The flow of the program should generally be forward except for normal looping and unavoidable instances. o Indentation rules should be established and followed, so that computation units of program text can more easily be identified. o Documentation should be short, but meaningful. Avoid comments like J is increased by one; o Use subroutines, where appropriate.

Example 1: Sequential Logic Develop an algorithm for converting given temperature in Celsius scale to Fahrenheit scale. C = 5 / 9 (F 32) The C++ statement for the above formula for Celsius temperature is: C = 5 / 9 * (F 32); The C++ statement of the above formula for Fahrenheit temperature is: F = 9 / 5 * C +32; Approximate Algorithms o Start o Read the given values of C i.e. temperature in Celsius. o Multiply this value with 9 / 5 and equate it with F, i.e. F = 9 / 5 * C. o Now, replace this value of F by F+32. o Write the answer. o Stop.

Introduction to Programming

Page 2 of 7

Faculty of Management and Computing

Example 2: Selection Logic Develop an algorithm to find the largest number from two numbers. Start Read values of A and B If (A = B) Print A and B are equal Else If (A>B) Print A is greater than B Else Print B is greater than A Stop. Example 3: Iteration Logic Develop an algorithm to print numbers from 1 to 10. Start Assign 1 to number WHILE ( number <= 10) Print number number = number + 1 End Stop

Flowcharts
A flowchart is a drawing giving step-by-step solution of a problem, using suitable annotated geometric figures connected by flow lines. The flowcharts are used for designing and documenting a process or a program. They represent program logic and the sequence of steps to be performed for writing the program. The flowchart is a means of visually presenting the flow of data through an information processing systems, the operations performed within the system and the sequence in which they are performed. In this lecture, we shall concern ourselves with the program flowchart, which describes what operations are required to solve a given problem. The program flowchart can be likened to the blueprint of a building. As we know a designer draws a blueprint before starting construction on a building. Similarly, a programmer prefers to draw a flowchart prior to writing a computer program. As in the case of the drawing of a blueprint, the flowchart is drawn according to defined rules and using standard flowchart symbols prescribed by the American National Standard Institute, Inc. Each symbol in the flowchart has a well-defined shape and meaning and expresses an operation or flow of data. Templates are available for drawing the flowchart symbols. Flowcharts are helpful in developing algorithms for solving problems. Flowcharts go by many other names like the diagram, system chart, run diagram, process chart, procedure chart and logic chart. As a programming aid, flowcharts are often prepared by system analysts and designers to describe systems and to specify the work to be accomplished by programs. Programmers use flowcharts as a basis for writing programs and as a means of communicating among each other, particularly when the programming is done as a team effort. Programmers as well as system analysts also use flowcharts as source of information for maintenance work on program and systems.

Introduction to Programming

Page 3 of 7

Faculty of Management and Computing

MEANING OF A FLOWCHART A flowchart is a diagrammatic representation that illustrates the sequence of operations to be performed to get the solution of a problem. Flowcharts are generally drawn in the early stages of formulating computer solutions. Flowcharts facilitate communication between programmers and business people. These flowcharts play a vital role in the programming of a problem and are quite helpful in understanding the logic of complicated and lengthy problems. Once the flowchart is drawn, it becomes easy to write the program in any high level language. Often we see how flowcharts are helpful in explaining the program to others. Hence, it is correct to say that a flowchart is a must for the better documentation of a complex program.

Flowchart Symbols
Flowcharts are usually drawn using some standard symbols; however, some special symbols can also be developed when required. Some standard symbols, which are frequently, required for flowcharting many computer programs are shown in the following table. Name Terminal Input Output Meaning A flattened oval shaped box. It denotes Start and Stop of a program. A flowchart starts from it and ends into it. A parallelogram shaped, showing the location where data is required to be input into the program and where the results are output by the program. A rectangular shaped box, used for indicating the types of process or action which result in singular outcome. They indicate arithmetic processes and assignment statements. It can also be a command for moving data from one place of storage to another.

Processing

Decision box

It is a rhombus shaped box. It contains a logical sequence with a Yes or No (True or False) outcome. The branch followed by the program depends on the outcome of the question.

Connector

Connector or joining of two parts of flowchart. Long flowchart panning more than one sheet of paper can be terminated at the bottom of the sheet and labelled with a number or a letter. The activity can begin at another connector with the same number or letter on the next page

Exit Connector

Exit connectors are used at the end of the page.

Introduction to Programming

Page 4 of 7

Faculty of Management and Computing

Entry Connector

Entry connectors are used to continue the flowchart from the next page.

Flow lines Arrow heads

Denotes the direction of logic flow in a program. Flow lines connect symbols to show the sequence of logical steps. The direction of flow is indicated by an arrowhead. Usually the direction of flow is indicated but in the absence of an arrowhead flow is assumed from top to bottom and from left to right.

Rules for making flowcharts a. In drawing a proper flowchart, all necessary requirements should be listed out in logical order. b. The flowchart should be clear, neat and easy to follow. There should not be any room for ambiguity in understanding the flowchart. c. The arrow in the flowcharts represents the direction of flow of control in the problem. d. The usual direction of the flow of a procedure or system is from left to right or top to bottom. The logic of a program flowchart should flow from top to bottom and from left to right. e. Each symbol (except decision box) used in a program flowchart should have one entry point and one exit point. f. Only one flow line should enter a decision symbol, but two flow lines, one for each possible answer, should leave the decision symbol. g. The instructions within the symbols of a program flowchart should be independent of any particular programming language. h. If the flowchart becomes complex, it is better to use connector symbols to reduce the number of flow lines. Avoid the intersection of flow lines if you want to make it more effective and better way of communication. i. Ensure that the flowchart has a logical start and finish. j. All decision branches should be labelled. ADVANTAGES OF USING FLOWCHARTS The benefits of flowcharts are as follows: o Communication: Flowcharts are better way of communicating the logic of a system to all concerned. o Effective analysis: With the help of flowchart, problem can be analyzed in more effective way. o Proper documentation: Program flowcharts serve as a good program documentation, which is needed for various purposes. o Efficient Coding: The flowcharts act as a guide or blueprint during the systems analysis and program development phase. o Proper Debugging: The flowchart helps in debugging process. o Efficient 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 part

Introduction to Programming

Page 5 of 7

Faculty of Management and Computing

LIMITATIONS OF USING FLOWCHARTS o Complex logic: Sometimes, the program logic is quite complicated. In that case, flowchart becomes complex and clumsy. o Alterations and Modifications: If alterations are required the flowchart may require redrawing completely. o Reproduction: As the flowchart symbols cannot be typed, reproduction of flowchart becomes a problem. o The essentials of what is done can easily be lost in the technical details of how it is done. Example 1 Draw a flowchart for the above algorithm, i.e. to convert given temperature in Celsius scale to Fahrenheit scale. Start

Read Celsius Temperature

F = 9/5*C F = F + 32

Print C and F

Stop

Introduction to Programming

Page 6 of 7

Faculty of Management and Computing

Example 2: Draw a flowchart to find the largest number from two numbers Start
Enter A and B

False

A=B

True

False

A>B

True

Numbers are equal

B is greater than A

A is greater than B

Stop Example 3: Draw a flowchart to print numbers from 1 to 10 Start

number = 1

False

Number <=10

True

Print number

number = number + 1

Stop

Introduction to Programming

Page 7 of 7