Algorithm and Flowchart
What is an algorithm: The actual planning of a program involves defining its logic (the correct sequence of instructions needed to solve the corresponding problem). The term algorithm directs the logic of a program.
To authorize as an algorithm, a sequence of instructions must possess the following features: –
1. Each instruction should be precise and clear.
2. Each instruction should be executed in a finite time.
3. No instructions should repeat infinitely. It ensures that the algorithm terminates ultimately.
4. After executing the instructions (when the algorithm terminates), the desired results are obtained.
Question. Fifty students in a class appeared in their final examination. Write an algorithm- to calculate and print the total number of students who passed in the FIRST division if the division column of their marks sheets contains the division (FIRST, SECOND, THIRD, or FAIL) they obtained.
Algorithm: –
Step 1: Initialize Total_First_Division and Total_Marksheets_Checked to zero.
Step 2: Take the marksheet of the subsequent student.
Step 3: Check the division column of the marksheet to see if it is FIRST. If no, go to Step 5.
Step 4: Add 1 to Total_First_Division.
Step 5: Add 1 to Total_Marksheets_Checked.
Step 6: Is Total_Marksheets_Checked = 50? If no, go to Step 2.
Step 7: Print Total_First_Division.
Step 8: Stop
Representation of Algorithms
1. As a Program
2. As a Flowchart
3. As Pseudocode
An algorithm represented in the form of a programming language is a Program. Hence, any program is an algorithm, but the reverse is not true.
Besides Programs, programmers often use flowcharts and pseudocodes to represent algorithms while planning their programs. They often find them of immense help in developing effective and correct algorithms for their programs before coding them in programming languages.
Flowchart
What is a Flowchart: A flowchart is a pictorial illustration of an algorithm. Programmers often use it as a program planning tool for visually organizing the steps necessary to solve a problem.
The process of sketching a flowchart for an algorithm is known as flowcharting.
Why use Flowcharts?
Normally, we first represent an algorithm as a flowchart and then express the flowchart in a programming language to prepare a computer program.
Once the flowchart is prepared, the programmer can concentrate on coding the operations in each flowchart box as statements of the programming language. Normally, this two-step approach ensures an error-free program.
Experienced programmers sometimes write programs without drawing a flowchart. However, beginners should first draw a flowchart to reduce the number of errors and omissions in a program.
It’s a good practice to have a flowchart because it often serves as a document for the computer program. It is very useful during the testing of the program and while incorporating further modifications in the program.
Need for Flowchart Symbols
The American National Standards Institute (ANSI) has verified the basic flowchart symbols. A flowchart must have different boxes to convey different types of instructions.
For example, a Diamond shape always means a decision. Hence, when a programmer looks at a flowchart, they can identify the decision points quickly by looking for Diamond shaped box.
Basic Flowchart Symbols
If the program logic has a halt, we display it with a terminal symbol. Generally, we use a pause in program logic under some error condition or for doing some manual setting during the processing of a program.
2. Input/Output: In a flowchart, input/output symbols display all program instructions to input/output data from any input/output device. They also show instructions to input/output data from/to a storage device.
3. Processing: A processing symbol shows arithmetic and data movement instructions. Hence, in a flowchart, processing symbols display all arithmetic processes. They also display logical processes of moving data from one memory location to another.
4. Decision: A decision symbol indicates a decision point at which a branch to one or more alternative paths is possible. The standard for making a decision is in the related decision box.
A decision symbol also shows all possible exit paths depending on the result of a decision. Each exit path has a label indicating the condition upon which the program logic takes the path for further execution.
5. Flow lines: Flow lines with arrowheads indicate the flow of operation, which is the sequence of execution of instructions. The general flow of a flowchart is from top to bottom and left to right.
Good practice also indicates that we should prevent crossing flow lines as practicable.
Advantages of Flowcharts
1. Better communication: A flowchart is a pictorial representation of the program. It is easier for a programmer to clarify the logic of a program through a flowchart.
2. Effective analysis: A macro flowchart, which starts the main line of the logic of a software system, becomes the system’s model that can be broken down into detailed parts for study and further analysis of the system.
3. Effective synthesis: Normally, a group of programmers is associated with the design of a big software system. Each programmer is accountable for designing only a part of the entire system.
Suppose each programmer draws a flowchart of their part of the design. Then the flowcharts of all programmers put together can help visualize the overall system design.
4. Efficient coding: Once a flowchart is ready, programmers find it very easy to write the corresponding program because the flowchart acts as a roadmap for them.
It guides them from the program’s starting point to the final point without omitting any steps. The final result is an error-free program that originated at a faster rate.
5. Systematic debugging: A flowchart is very helpful in systematically detecting, locating, and removing mistakes (bugs) in a program because programmers find it easier to follow the logic of the program in flowchart form. The process of removing errors (bugs) in a program is known as debugging.
6. Systematic testing: Testing confirms whether a program successfully performs all its intended jobs under specified constraints.
Disadvantages of Flowcharts
1. Flowcharts are time-consuming and laborious to draw with proper symbols and spacing, especially for large complex programs.
2. Owing to the symbol string nature of flowcharting, any change or modification in program logic usually requires a completely new flowchart. Redrawing a flowchart is a tedious task.
Many programmers do not modify the corresponding flowcharts when they modify their programs, which leaves a program and its flowchart in an inconsistent state.
3. No standards determine the amount of detail needed in a flowchart.
Because of such limitations, many organizations have reduced the flowcharting used. In its place, they use alternative tools for program analysis.