You are here:

C/How to write Algorithms..

Advertisement


Question
what are the steps to be followed to write an algorithms for a given problem.?and please give guidelines to write an algorithm?

Answer
Hi Dear Asha,


Well Its pretty simple.. Its just a systematic procedure how you are going to accomplish a solution to a problem .. A simple way of getting familiar with algorithms is to start with writing pseudo code hope you must have be thought how to write a pseudo code!.Its just some informal way of writing algorithms .. but with a defined meaning.

Lets have an example .. say if you are about to prepare tea..probably this might be the pseudo-code

  get water
   put water in kettle
   while water is not boiling
     do
         wait 1 minute
     od
   scald teapot
   for each person drinking tea
     do
       put 1 teaspoon of tea into teapot
     od
   put 1 teaspoon of tea into teapot
   while water is not boiling
     do
         wait 1 minute
     od
   pour water into teapot
   put cosy on teapot
   set brewtime to 0
   while brewtime is < 5 minutes
     do
         wait 1 minute
         increment brewtime by 1
     od
   for each person drinking tea
     do
       pour milk into teacup
     od
   for each person drinking tea
     do
       pour tea into teacup
     od

but an algorithm is a well formatted way of presenting a pseudo-code, it  do follow some grammar, and better way of representing the looping including the counter variables ..

Hints I would like to recommend when writing Algorithms.
1. You should have clear idea about what you are going to implement through the algorithm
2. You should not mess up with the order of Execution of the steps , say for example  

Ill reformat the above pseudo code just like this
 
   get water
   put water in kettle
  for each person drinking tea
     do
       pour tea into teacup
     od
 This really doesn't make any sense .. how  come the water i poured in the kettle become tea ? Magic ..?? well you messed up with the steps in between .. :) , Ensure you will be executing the steps at right order .

3. If you are using looping constructs / Iterations .. Make it definite for example don't try like this
 
 while brewtime is < Infinite minutes
     do
         wait 1 minute
         increment brewtime by 1
     od

Ultimately this loop will never end up.. sad thing is you wont get a tea probably things might have boiled off or your fuel that lights up the burner might exhaust ..

so the two things  is What you are going to do..? and how you are going to do..??
if you are clear with the above mentioned.. well you can write any algorithms..

Hope this will help you ..

Thanks and Regards
Prince M. Premnath  

C

All Answers


Answers by Expert:


Ask Experts

Volunteer


Prince M. Premnath

Expertise


I'm sure that I can solve any doubts in Turbo C ,Graphics Programing ,Mouse, Hardware Programming ,File System ,Interrupts, BIOS handling , TSR Programming , General Concepts in C Language, handling inline Assembly statements

Experience

Research over 6+ Years

Organizations
CG-VAK Softwares and Exports Limited

Education/Credentials
Masters in Computer Applications

©2012 About.com, a part of The New York Times Company. All rights reserved.