# Thread: Can someone give me tips on algorithms

1. ## 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. 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. 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.

4. 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. 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. 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

7. 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.

8. 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. 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. 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...