Go Back   MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Reply
 
Thread Tools Search this Thread Display Modes
Old Oct 21, 2005, 08:48 PM   #1
fBaran
macrumors regular
 
Join Date: Oct 2003
Send a message via AIM to fBaran
C++; fibonacci.

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:: !
__________________
PowerMac Digital Audio: 250GB SATA, 1.5GB
30GB White videoPod.
QC@CUNY BA in CS.

Last edited by fBaran; Oct 21, 2005 at 08:55 PM.
fBaran is offline   0 Reply With Quote
Old Oct 21, 2005, 09:16 PM   #2
gammamonk
macrumors 6502a
 
gammamonk's Avatar
 
Join Date: Jun 2004
Location: Madison, WI
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.

Quote:
//
// This file contains the C++ code from Program 3.4 of
// "Data Structures and Algorithms
// with Object-Oriented Design Patterns in C++"
// by Bruno R. Preiss.
//
// Copyright (c) 1998 by Bruno R. Preiss, P.Eng. All rights reserved.
//
// http://www.pads.uwaterloo.ca/Bruno.P...s/pgm03_04.cpp
//
unsigned int Fibonacci (unsigned int n)
{
if (n == 0 || n == 1)
return n;
else
return Fibonacci (n - 1U) + Fibonacci (n - 2U);
}
__________________
17" MBP, 11" Air, iPad2, Touch 4G, 2gig Nano, 30 shares and counting.
gammamonk is offline   0 Reply With Quote
Old Oct 21, 2005, 09:49 PM   #3
fBaran
Thread Starter
macrumors regular
 
Join Date: Oct 2003
Send a message via AIM to fBaran
My bad. I should have specified NO recursion (that would be too easy). The old fashion iteration!
__________________
PowerMac Digital Audio: 250GB SATA, 1.5GB
30GB White videoPod.
QC@CUNY BA in CS.
fBaran is offline   0 Reply With Quote
Old Oct 22, 2005, 12:47 AM   #4
HexMonkey
Administrator
 
HexMonkey's Avatar
 
Join Date: Feb 2004
Location: New Zealand
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;
}
HexMonkey is offline   0 Reply With Quote
Old Oct 22, 2005, 04:07 AM   #5
g0gie
macrumors regular
 
Join Date: Jan 2005
Cheater!!! do it and learn it!!! A CS degree is coveted!!!
g0gie is offline   0 Reply With Quote
Old Oct 22, 2005, 08:07 AM   #6
fBaran
Thread Starter
macrumors regular
 
Join Date: Oct 2003
Send a message via AIM to fBaran
Quote:
Originally Posted by g0gie
Cheater!!! do it and learn it!!! A CS degree is coveted!!!
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.
__________________
PowerMac Digital Audio: 250GB SATA, 1.5GB
30GB White videoPod.
QC@CUNY BA in CS.

Last edited by fBaran; Oct 22, 2005 at 08:15 AM.
fBaran is offline   0 Reply With Quote
Old Oct 22, 2005, 10:17 AM   #7
NewbieNerd
macrumors 6502a
 
Join Date: Sep 2005
Location: Chicago, IL
Send a message via AIM to NewbieNerd
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.
NewbieNerd is offline   0 Reply With Quote
Old Oct 22, 2005, 01:19 PM   #8
colocolo
macrumors 6502
 
Join Date: Jan 2002
Location: Santiago, Chile
Quote:
Originally Posted by g0gie
Cheater!!! do it and learn it!!! A CS degree is coveted!!!
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.

Last edited by colocolo; Oct 22, 2005 at 01:24 PM. Reason: Not enough facts to make a statement
colocolo is offline   0 Reply With Quote

Reply
MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Forum Jump

All times are GMT -5. The time now is 01:12 PM.

Mac Rumors | Mac | iPhone | iPhone Game Reviews | iPhone Apps

Mobile Version | Fixed | Fluid | Fluid HD
Copyright 2002-2013, MacRumors.com, LLC