Thread: Can someone give me tips on algorithms

  1. #1
    NewbieUser
    Guest

    Can someone give me tips on algorithms

    For example I see a function without any comments on it but say some complicated nested for-loop and weird if statements. Is there any easy way in deciephering what the code means without subsitution for what ever is needed. For example, I come across a weird program for GCF. This person did it in an akward way. Is there a way to deciepher it without plugging in numbers?

    Also one more question, when thinking up algorithms to solve equations. Whats the best way in thinking up algorithms that are effiecient and easy to code without asking someone for help?

    For example I am given a precondition and a postcondition that basically takes a number in and the post condition is returning the Fibonacci number of it. How would someone go on figuring out how to write the algorithm of the code?

  2. #2
    i want wookie cookies the Wookie's Avatar
    Join Date
    Oct 2002
    Posts
    455
    i think the fibonacci numbers are done by recursion. i dont see how you could do it without plugging in numbers...do you mean in the program or in your head? usually i find it usefull to make a flowchart, because it just helps put a visual down which helps to decipher how something will work...especially with complicated stuff

  3. #3
    Pursuing knowledge confuted's Avatar
    Join Date
    Jun 2002
    Posts
    1,916
    To figure out what the code does, I just read it a few times, and figure out what all the variables are from code in the rest of the program. Then I figure out what the function is supposed to be doing and figure out exactly how it does it.
    Away.

  4. #4
    NewbieUser
    Guest
    What if say you are given a function with a screwed up title. So the title doesn't tell you what the function does and there are no comments. You mean I should just read through the code a few times and just try to decipher it? Is being able to decipher what the code says that way a gift some people have and some don't? or is it something gained from expierience?

  5. #5
    Registered User
    Join Date
    Nov 2002
    Posts
    1,109
    Originally posted by NewbieUser
    What if say you are given a function with a screwed up title. So the title doesn't tell you what the function does and there are no comments. You mean I should just read through the code a few times and just try to decipher it? Is being able to decipher what the code says that way a gift some people have and some don't? or is it something gained from expierience?
    it should be something gained from experience. just 'walk through the code' and you'll begin to see what exactly the loop does to the variable, how it is modified, etc.

    as for fibonacci, it can be done iteratively, but i believe the code is shorter if written, as the Wookie said, using recursion.

    recursion is kindof tough for some at first, some people just get it, some just have to walk through and practice it.

    keep coding, figuring out what code does is something that comes with practice and experience.

  6. #6
    Senior Member joshdick's Avatar
    Join Date
    Nov 2002
    Location
    Phildelphia, PA
    Posts
    1,146
    I can only tell you a little about my thinking process when it comes to figuring out certain algorithms. Most specifically, let's talk about mathematical algorithms like finding Fibonacci numbers.

    First, I go through the steps that I would take to solve the problem in my head or on a sheet of paper. In this case, say I want to find the fourth Fibonacci number. I think, well, I just add 1+1+2+3 = 8. Then, I generalize my solution. I just start with 1 and 1, and then add the two preceeding numbers. Next, I formalize this generalized solution. Fib(n) = Fib(n-1) + Fib(n-2). Now, before I think I have this thing finished, I think about extreme cases. I need a special condition for Fib(1) and Fib(2). Finally, I code out a solution:
    Code:
    int Fib(int n)
    {
         if(n < 3)
             return 1;
         return Fib(n-1) + Fib(n-2);
    }
    And there you have it. I did give you the solution to finding Fibbonacci numbers, but I hope what you gain from this is the thought process. Usually, if you can think of a problem in terms of variables, you can code it. I hope this has helped you
    FAQ

    "The computer programmer is a creator of universes for which he alone is responsible. Universes of virtually unlimited complexity can be created in the form of computer programs." -- Joseph Weizenbaum.

    "If you cannot grok the overall structure of a program while taking a shower, you are not ready to code it." -- Richard Pattis.

  7. #7
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    The code might be shorter, but the iterative fibonacci function is better because it is faster (function overhead!) and won't overflow the stack as the recursive can do easily.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  8. #8
    Crazy Fool Perspective's Avatar
    Join Date
    Jan 2003
    Location
    Canada
    Posts
    2,640
    Originally posted by CornedBee
    The code might be shorter, but the iterative fibonacci function is better because it is faster (function overhead!) and won't overflow the stack as the recursive can do easily.
    that is true, and if you want to get creative, fib numbers can be calculated by doing matrix multiplication!

  9. #9
    Hardware Engineer
    Join Date
    Sep 2001
    Posts
    1,398
    Is there a way to deciepher it without plugging in numbers?
    For simple programs, it's not too hard to "play compiler". You can look-up all the standard library functions, and you can see the programmer's function definitions.

    For larger programs, it can be quite difficult... functions called from other modules (files), functions from other libraries (or from windows.h) ... lots of variables to keep track of etc.

    Sometimes I have to modify programs that are a few thousand lines of code long. I consider myself lucky if by the end of the first day, I'm able to re-compile as-is, find the part of the code to be modified, and write some experimental code to confirm I'm working on the correct section/function! (To do that I don't have to fully understand the entire program.)

    When you write your code, remember how difficult this is, and include lots of comments for the next guy. (Or for yourself when you go back to it a few months later.)

  10. #10
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    I have an app where I stopped developing a while ago. Now that I wanted to continue I found that I have only a rough idea what the code parts do.
    I ended up starting the project from scratch in Java...
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Programming Tips
    By webmaster in forum General Discussions
    Replies: 27
    Last Post: 10-29-2018, 12:49 PM
  2. Sorting algorithms, worst-case input
    By Leftos in forum C++ Programming
    Replies: 17
    Last Post: 06-15-2009, 01:33 PM
  3. C++ Programming Tips
    By gflores in forum C++ Programming
    Replies: 20
    Last Post: 09-14-2004, 07:53 PM
  4. any useful tips to increase speed when parsing files
    By Shadow12345 in forum C++ Programming
    Replies: 2
    Last Post: 01-18-2003, 05:52 PM
  5. How To Give A Font Colour ?
    By Unregistered in forum Windows Programming
    Replies: 1
    Last Post: 09-14-2001, 01:22 PM