Writing An Algorithm

A programming task can be divided into two phases:

  • Problem-solving phase – produce an ordered sequence of steps that describe the solution of a problem this sequence of steps is called an algorithm.
  • Implementation phase – implement the program in some programming language.

Steps In Problem-Solving

  • First, produce a general algorithm (one can use pseudo-code)
  • Refine the algorithm successively to get step by step detailed algorithm that is very close to a computer language.
  • Pseudo code is an artificial and informal language that helps programmers develop algorithms. Pseudo-code is very similar to everyday English.

What is an Algorithm?

An algorithm is a step-by-step process of solving any problem. Algorithm means “a process or set of rules to be followed in calculations or other problem-solving operations”. Therefore, an Algorithm refers to a set of rules/instructions that step-by-step define how a work is to be executed to get the expected results. 

Features of Algorithm

The first step of every algorithm should be Start, and the last step should be Stop.

An algorithm can be written in sentence form, or it can be written using mnemonics and pseudo codes.

An algorithm is mainly written to specify the following structure of the problem-solving technique:

  • Input operation
  • Process or computation
  • Output operation

Assignment operation in algorithm is done using these operators:  := or ¬

e.g. To show assignment of a value say 5 to a variable a:

            a:=5 or a ¬ 5

Note: Question may arise why = (assignment operator) is not used here?

The answer is, (=) operator is used to show comparison (equals to) in algorithm.

Myths in writing Algorithm

  • Algorithm is dependent on the program code.
    • Algorithm does not depend upon any programming language and program code.
    – It is independent of any programming language.
  • Algorithm cannot be written without writing the program code.
    • Algorithm can be written prior to write the program code as it only describes the IPO of the problem-solving technique.
  • Algorithm which follows any programming language and program code is the best algorithm
    • Algorithm that is concise, clear and unambiguous, is the best algorithm. It should describe the input, process and output clearly, that’s it.

It can be understood by taking an example of cooking a new recipe. To cook a new recipe, one reads the instructions and steps and execute them one by one, in the given sequence. The result thus obtained is the new dish cooked perfectly. Similarly, algorithms help to do a task in programming to get the expected output.

The Algorithm designed are language-independent, i.e. they are just plain instructions that can be implemented in any language, and yet the output will be the same, as expected.

Characteristics of an Algorithm

One would not follow any written instructions to cook the recipe, but only the standard one. Similarly, not all written instructions for programming is an algorithm. For some instructions to be an algorithm, it must have the following characteristics:

  • Clear and Unambiguous: The algorithm should be clear and unambiguous. Each step should be clear in all aspects and lead to only one meaning.
  • Well-Defined Inputs: If an algorithm says to take inputs, it should be well-defined inputs.
  • Well-Defined Outputs: The algorithm must clearly define what output will be yielded and should also be well-defined.
  • Finite-ness: The algorithm must be finite, i.e. it should not end up in an infinite loop or similar.
  • Feasible: The algorithm must be simple, generic and practical, so it can be executed with the available resources. It must not contain some future technology or anything.

Language Independent: The Algorithm designed must be language-independent, i.e. it must be just plain instructions that can be implemented in any language, and yet the output will be same, as expected.

Q. Write down the algorithm and draw the flowchart for the given question: Two shopkeepers are giving discount on the same product in two different ways. 1st one is giving directly 20% and 2nd one is giving in break of 15% and 5%. Find out whose discount is more.

Working:

P = Rs. 100.00

A = 20% on Rs100 = Rs. 20

B = 15% on Rs100 then 5% on the reduced amount

   = 15 + (100 – 15)*5%

   = 15 + 4.25 = Rs. 19.25   

Algorithm:

Step 1: Start

Step 2: Input the price of the product, P

Step 3: A := (20.0*P)/100.0

Step 4: B := (15.0*P)/100.0

Step 5: B := B + (5.0*(P – B))/100.0

Step 6: If A>B then

Print “A’s discount is more”

else

Print “B’s discount is more”

Step 7: Stop

Q. Algorithm for Scary Number program (A number is said to be scary number if that contains 13 within itself e.g 113, 132, 3130)

Step 1. Start

Step 2. Input 2 nos. in L, B

Step 3. If L<1 OR B<1 then goto Step 2

Step 4. SP¬0

Step 5. For A<=L to B repeat Step 6 to Step 12

Step 6. T¬A : C¬0

Step 7. While T>0 repeat Step 8 to Step10

Step 8. R¬T%100

Step 9. If R=13 then C¬C+1

Step 10. T¬T/10

Step 11. If C=1 then Print A : SP¬SP+1

Step 12. A¬A+1

Step 13. If SP=0 Print 0 else Print SP

Step 14. Stop

@ Spondon Ganguli, 27 July 2023