C++; fibonacci and GCF.

Discussion in 'Mac Programming' started by fBaran, Oct 21, 2005.

  1. macrumors regular

    Joined:
    Oct 22, 2003
    #1
    Here is my fib() function:

    Code:
    int fib (int x)
        { int i, count=0;
                    if (x==1 || x==2) return 1;
            for (i=0; i < x; i++)
                {
                    count = (x-1) + (x-2);
                }
            return count;   
        }
    I've been working on this for a couple of hours now, but I can't get it correctly calculate the numbers. A little help?

    Thank you all for your input ::cue rimshot:: !
     
  2. macrumors 6502a

    gammamonk

    Joined:
    Jun 4, 2004
    Location:
    Madison, WI
    #2
    Are you allowed to use recursion? I'm assuming this is for a class or something.

    Here's some code I found in 6 seconds on google.

     
  3. thread starter macrumors regular

    Joined:
    Oct 22, 2003
    #3
    My bad. I should have specified NO recursion (that would be too easy). The old fashion iteration!
     
  4. Administrator

    HexMonkey

    Staff Member

    Joined:
    Feb 5, 2004
    Location:
    New Zealand
    #4
    Try this:

    Code:
    int fib (int x)
    {
    	//For the first two terms, return 1
    	if (x==1 || x==2) return 1;
    	
    	int i, prev2=1, prev=1, newTerm;
    	for (i=2; i<x; i++)
    	{
    		//Find the next term
    		newTerm = prev + prev2;
    		//Update the last two terms
    		prev2 = prev;
    		prev = newTerm;
    	}
    	return newTerm;
    }
     
  5. macrumors regular

    Joined:
    Jan 31, 2005
    #5
    Cheater!!! do it and learn it!!! A CS degree is coveted!!!
     
  6. thread starter macrumors regular

    Joined:
    Oct 22, 2003
    #6
    If you're not going to contribute to my thread, don't post it in.
    -------

    Thanks a lot HexMonkey, I think what I did wrong is that I didn't sawp anything, so thing just kept getting overwritten. Is that right? Clearly, I don't have any swaps, so it must've been recycling old numbers.
     
  7. macrumors 6502a

    NewbieNerd

    Joined:
    Sep 22, 2005
    Location:
    Chicago, IL
    #7
    No, your program as it stands is just adding up numbers less than x twice for the most part. You are correct in returning 1 if x=1 or x=2, but from that step on you should keep track of variables like last and prev, which will be the last two fib numbers you have found so far.

    So in the beginning, last = prev = 1. Then, for the remaining x-2 iterations, you need to make prev = last and last = prev + last (use a temp variable to help you do this). In the end, return last.
     
  8. macrumors 6502

    Joined:
    Jan 17, 2002
    Location:
    Santiago, Chile
    #8
    Edit: Sorry, on second thought my comments on how unappropiate this thread is because it is as morally wrong as piracy might be out of place.

    I do not have enough information about the situation of the OT to judge him as doing something wrong by asking his homework to be done.

    Maybe he really tried hard and has the will to learn after all.

    If you happenned to read my post before I edited it, I apologize.
     

Share This Page