Explain Algorithm and Flowchart with Examples


ALGORITHM


An algorithm is the brain of programming. Algorithms help the programmer to find a solution to a problem. An algorithm is a list of instructions to reach a target or objective. We are doing different tasks daily basis. Each task has a sequence of steps to complete the job or solve a problem.

Can you think of a day in your life which goes without problem-solving? The answer to this question is of course, No. In our life, we are bound to solve problems. In our day-to-day activities such as purchasing something from a general store and making payments, depositing fee in school, or withdrawing money from a bank account. All these activities involve some kind of problem-solving. It can be said that whatever activity a human being or machine does for achieving a specified objective comes under problem-solving. To make it clearer, let us see some other examples.

Example1: If you are watching a news channel on your TV and you want to change it to a sports channel, you need to do something i.e., move to that channel by pressing that channel number on your remote. This is a kind of problem-solving

An 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 unambiguous and 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 the definition of an 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 the 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.



For example, a problem statement is that find the more significant number between two numbers.

The algorithm for this problem is

Step 1: Start

Step 2: Read N1:number 1 and N2:number 2

Step 3: Compare the values of two numbers. N1>N2 if true, then R = N1; otherwise, R = N2.

Step 4: Print R: Greater number

Step 5: End

Type of Algorithms The algorithm and flowchart, classification to the three types of control structures. They are:
 1. Sequence
 2. Branching (Selection)
 3. Loop (Repetition)

 These three control structures are sufficient for all purposes. The sequence is exemplified by a sequence of statements place one after the other – the one above or before another gets executed first. In flowcharts, a sequence of statements is usually contained in the rectangular process box. 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. 

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 beforehand.) In the flowcharts, a back arrow hints at 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.





FLOWCHART

The flowchart is a diagram which visually presents the flow of data through processing systems. This means by seeing a flow chart one can know the operations performed and the sequence of these operations in a system. Algorithms are nothing but a sequence of steps for solving problems. So, a flow chart can be used for representing an algorithm. A flowchart will describe the operations (and in what sequence) are required to solve a given problem. You can see a flow chart as a blueprint of a design you have made for solving a problem. For example, suppose you are going for a picnic with your friends then you plan for the activities you will do there. If you have a plan of activities, then you know clearly when you will do what activity. Similarly, when you have a problem to solve using a computer or in other words, you need to write a computer program for a problem then it will be good to draw a flowchart prior to writing a computer program. A flowchart is drawn according to defined rules.





Comments