Search This Blog

Algorithms


ALGORITHM : It is a set of step by step instructions to solve a problem .
It is a good practice to express the stepwise procedure for solving a problem before implementing it as it helps in identifying the errors at an early stage. Use of algorithm makes decision making better and ensure that all the problem areas are have been taken up. It helps in writing efficient and consistent program

Algorithm can be written in many forms and there is no particular restriction to it. It may be expressed in simple language, pseudocode,  programming language snippets etc. It may consist of mathematical formulae/ expression or repetition of steps if required. Everyalgorithm begins with a start step and ends with a stop step.

Pseudocode is the code written in informal language used for developing algorithm and does not follow strict guidelines.

Let us write an algorithm to make mango shake.
1    Start
2.      Take peeled and diced mangoes.
3.      Pour milk in the mixer jar.
4.      Add mangoes and 1 teaspoon of sugar to the milk in jar.
5.      Put the lid on the jar properly
6.      Place the jar on the mixer.
7.      Press the button of minimum speed and increase the speed of the mixer for 15 sec.
8.      Now, reduce the speed of the mixer to zero.
9.      Put off the jar from the mixer.
10. Remove the lid and pour the shake in a glass.
11. Sprinkle some chopped dry fruits and serve.
12. Stop


Algorithm can also be used for problems involving simple arithmetic calculations:


Let us take an example to find the perimeter of a rectangle.

1. Start
2. Accept values of length and breadth of a rectangle as L and B
3. Declare P .
4. P=2X(L + B)
5. Print P.
6. Stop






This algorithm can also be written in the form of code snippet. The pseudocode of the above example in C++ can be written as follows :-

1. Start
2. int length, breadth, perimeter
3. input length
4. input breadth
5. perimeter = 2 X (length + breadth)
6. Print perimeter
7. Stop



Decision making algorithm
Algorithm might not always consist simple instructions. Algorithm for decision making problems can also be devised. Based on the decision we can skip/jump the steps in the algorithm as follows :

Let us take an example to find the greater number from two given numbers.
1. Start
2. Accept three numbers a,b from the user.
3. If a is greater than b go to step 4 else go to step 5.
4. Print “a is greater than b”. Go to step 6.
5. Print “b is greater than a”.
6. Stop.


An algorithm to check whether a person is eligible for voting or not?
1. Start
2. Accept age of the person in variable ‘age’.
3. If age is greater than or equal to 18 then go to step 4 else go to step 5.
4. Print “Eligible to vote “. Goto step 6.
5. Print “Not Eligible to vote"
6. Stop



Looping statements

In case certain set of statements need to be repeated then we can apply loop on them. A loop is a construct which keeps repeating a set of instructions. A loop might involve repetition of steps for a number of times or may involve in steps depending on some conditions.


Let us now see an example to display first n natural numbers. In this algorithm, we need to repeat a statement.

1. Start
2. Accept the value of n from the user.
3. Declare a variable ctr initializing it with 1.
4. Check value of ctr, if ctr>n go to step 8.
5. Print value of ctr.
6. Increment ctr by 1.
7. Go to step 4.
8. Stop

In above algorithm, steps 4 to 6 are repeated till first n natural numbers are printed.

No comments:

Post a Comment