How to write algorithms for any given problem?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • carma
    New Member
    • Dec 2010
    • 2

    How to write algorithms for any given problem?

    Hello everyone,I am java programmer.I am developing several applications in Swings and other frameworks.I am good in OOPS and most of the Core Java concepts.Curren tly developing websites in JSP and PHP.But one day i was asked to write an algorithm for fibonacci numbers generation which took more than 10 minutes to write for me(and its wrong too).Everytime when i try to write some algorithm like fibonacci,sorti ng,..i cant progress well.What skill do i need to develop?Please suggest...Thank s all.
  • Shafi hashmi
    New Member
    • Dec 2010
    • 14

    #2
    hi dear if u r a good programmer then writing an algorithm shud be easy for u..

    actually when we start writing program an algorithm is created in our mind(of course in different syntax) we just need to write then on the paper.

    the basic idea to write algorithm is

    Step 1. Start
    Step 2. Accepts inputs
    Step 3. Declare variables
    Step 4. Calculation/Manipulation
    Step 5. Display result
    Step 6. Stop.

    OK ok too much theory i am making u understand??
    lets do something practically.

    suppose u have to write a program of fibbinaci in java what wud u have done. something like thes

    Note: i will write C# syntax coz i dint know java.

    Code:
    void Main()
    {
    Console.WriteLine("enter the length");
              int n= int.Parse(Console.ReadLine());
              int old = 0;
              int older = 1;
              int disp = 0;
              for (int i = 0; i <= n; i++)
              {
                  disp = older + old;
                  Console.WriteLine(disp);
                  older = old;
                  old = disp;
              }
    }
    now we have to write its algorithm...

    Step 1. Start
    Step 2. Accept value of n
    Step 3. declare old=0,older=1,d isp=0,i=0
    Step 4. if(i<=n) goto next Step else goto Step 10
    Step 5. calculate disp=older+old
    Step 6. Display disp
    Step 7. set older=old
    Step 8. Set old=disp
    Step 9. Goto Step 4
    Step 10.Stop




    that's enough...
    want know more ?? mail me
    Last edited by Meetee; Apr 7 '11, 06:32 AM. Reason: Code tags added

    Comment

    • jkmyoung
      Recognized Expert Top Contributor
      • Mar 2006
      • 2057

      #3
      Algorithms like Fibonacci are more mathematically oriented than the rest of your work. You may want to study data structures and see how they work, eg the back end implementation of queues, hashes, trees, binary sorts, etc.. Other than that, your question is quite vague and would have to be expanded upon.

      Comment

      Working...