Algorithms & Flowcharts In C
Although computer is a very powerful and fast machine capable of performing a number of task. But it has no intelligence of thinking power. It can perform a task exactly in the same manner in which it is told to do. This places responsibility on the user to specify the required job in a proper way. In order to instruct computer correctly user must have clear understanding about the task to be solved. Along with this, he should be able to develop a method in the term of sequential steps to solve it. Once the problem is well defined and a method of solving it is developed, then instructing computer to solve the problem becomes a relatively easy task.
The set of sequential steps to solve a problem, written in some natural language (e.g. Hindi, English) is termed as algorithm.
The set of sequential steps to solve a program written in some high level language (e.g. Pascal, C, C+ +) is known as program.
Preparation of programs and executing them with the help of computer is termed as programming.
The person who writes the programs is termed as programmer. Whereas any person using computer is termed as user .It is possible that same problem can be solved by different people in different ways, therefore, programming is termed as an art and not as a science.
Different methods of problem solving are:
- Decision tables
An Algorithm is a well-defined complete set of steps to solve a problem. These steps are certain some natural language (i.e. language which we use in our day to day life such as Hindi or English). The word algorithm is derived from the name of an Arab mathematician, Al-Khowarizmi. Who developed methods for solving problems which used finite, well defined step by step instructions?
Free from computer language:
Algorithms should be free from any computer language (e.g. COBOL, Pascal, C etc).it should be in some natural language so that even a non-computer person can understand.
It should have a finite number of instructions.
Each instruction in algorithm should be well defined i.e. it should have a unique of meaning i.e. it should not include instructions which can have more than one meaning which may lead to confusion to decide what meaning should be compared.
Statement which can be performed by computer and not statement like “Bring the book” or “Post this letter” which can’ not be executed by computer.
It should be effective i.e. it should produce the required output.
Write an algorithm to find sum of two numbers.
To write the algorithm, let us denote two numbers as a and b and the result (i.e. sum) as sum.
Desired algorithm is:
Step 1 input a, b
Step 2 sum=a+ b
Step 3 display sum
Step 4 end
Write in algorithm to find area of a triangle with sides a, b, c
Step 1 input a, b, c
Step 2 s=a+ b+ c/2
Step 3 area=
Step 4 display area
Step 5 end
Write an algorithm to find largest of two numbers.
Let two numbers be denoted by a, b and the largest of these be denoted large. Desired algorithm:
Steps 1 input a, b
Steps 2 large=a
Steps 3 if b>large then large=b
Steps 4 display large
Steps 5 end
Write an algorithm to find largest of three numbers.
Let three numbers be denoted by a, b, c and the largest of these three be denoted by large. Desired algorithm is:
Steps 1 input a, b, c
Steps 2 large =a
Steps 3 if b>large then large=b
Else if c> large then large=c
Steps 4 display large
Steps 5 End
Write an algorithm to finds roots of a quadratic equation.
Let a, b, c denote the coefficients of given quadratic equation, d denotes the discrement and x1, x2 denote roots of the equation. Desired algorithm is:
Steps 1 read a, b, c
Steps 2 d=b2-4ac
Steps 3 if d>0 then x1=(-b+ sqrt d)/2a, x2=(-b- sqrt d)/2a
Else if d<0 then display “No real root” and exit.
Else if d=0 then display ‘Roots are equal; and x1=x2= (-b/2a)
Steps 4 display x1, x2
Steps 5 End
Like algorithm, it is also a step by step procedure to solve a problem, but it is written in an language which is an intermediate of some natural language such as English language and the computer language.
It differs from the Algorithm in the sence that it is computer language dependent and uses some words (commands) of a particular computer language. Due to this reason a pseudocode written on the basis of one computer language may not be effective while writing the program in some other language.
It is a graphical representation of sequence of steps involved in solving a problem. In-fact, it is a graphical representation of the algorithm.
Various symbols used in flow-charts are:
|ELLIPSE||TO Define starting point and ending point.|
|PARALLELOGRAM||TO Define input data to computer and output information from computer.|
|RECTANGLE||TO define processing. All the calculations are defined in it.|
|DECISION BOX||TO Check condition and decide that what part is to be executed in case of multiple parts. It has one entry point and two (or three) exit point. One exit point indicates for yes (or true) and for no (or false).|
|ARROWS KEYS||To represent flow of|
|CIRCLE||To connect two parts of flow chart. It is termed as connecter.|
Advantages of flow-charts:-
- Since flow-chart is a pictorial (graphical) representation of steps involved in solving a program, so it is easy to understand and explain the logic of a program using flow-chart.
- Once a flow -chart is ready, programmer finds it very easy to write the program because flow-chart acts as a road map for him.
- Debugging (detecting and removing of errors) becomes easier due to use of flow –charts.
Flow –chart is a good means of communication between the different users.
Conventions of flow-charts:-
- Only appropriate symbols should be used e.g. for input/output purpose, only a parallelogram should be used.
- Within the symbols, instruction can be in capital letters or small letters or combination of two.
- Flow-charts are read from top to bottom and from left to right
- Crossing arrow lines do not imply any relationship.
- Two parts of a flow-chart present on the same page can be connected using connector symbol.
- Sequence of operation is performed until an ellipse (i.e. end symbol) with word end (or stop) is encountered.
Limitations of flow-charts:-
- Flow-chart is very time consuming and laborious to draw, particularly for complex programs.
- There are no standard rules about the details involved in drawing a flow -chart.
- Due to pictorial representation, it is very difficult to make changes in a drawn flow –chart.
Some-times it leads to redrawing of entire flow –chart.
Example1:-draw a flow –chart to find some of two numbers.
Solution- let two numbers be denoted by a, b and the result by sum:
Flow –chart to find the area of triangle.
(Given sides a, b, c)
Draw a flow –chart to find the Largest of two numbers.
Let two numbers be Denote by a, b and their Largest by large.
large” width=”147″ height=”105″> Yes NO
Draw a flow –chart to find the largest of three numbers.
Let three numbers be denoted by a, b, c and the largest of these by large:
large?” width=”152″ height=”136″>
Draw flow –chart to find the roots of a quadratic equation.
Let a, b, c denote coefficients of quadratic, d denote discriminant and x1, x2 denote the roots of the equation: