http://gamingtrend.com
December 18, 2014, 04:12:52 AM *
Welcome, Guest. Please login or register.

Login with username, password and session length
News:
 
   Home   Help Search Calendar Login Register  
Pages: [1]   Go Down
  Print  
Author Topic: C++ Coding Question  (Read 1347 times)
0 Members and 1 Guest are viewing this topic.
namatoki
Gaming Trend Reader

Offline Offline

Posts: 414


View Profile
« on: April 05, 2006, 07:53:50 PM »

I wasn't sure where to ask this, so I hope this is the right place.

Anyway, I was trying to figure out a looping structure that would increment by the Fibonacci sequence.  Is it possible to have a for loop increment in this way?

The Fibonacci sequence is the first two numbers added to produce the third.  So, it would go: 1, 2, 3, 5, 8, 13, ...

Thanks!
Logged
coopasonic
Gaming Trend Senior Member

Offline Offline

Posts: 4127



View Profile WWW
« Reply #1 on: April 05, 2006, 09:16:55 PM »

I'm not really a c++ guy, but it seems you could do it in a sloppy way with a collection or in a nice neat way with a class. You need 3 properties, current, second and third or somesuch and an intialize and increment method. Initialize set current and second to 1 and third to 0, increment, pushes second to third and current to second and sets current to second + third. For the increment part of your loop call the increment method of your class.

It works in my brain, I haven't worked out the implementation, but I could probably throw it together in c# pretty quick.
Logged

It was this moment that took the movie from being a little ho-hum to “holy shit, did that shark just eat a plane!?”
happydog
Gaming Trend Senior Member

Offline Offline

Posts: 1469


View Profile
« Reply #2 on: April 05, 2006, 09:19:28 PM »

I have an general non-language specific thought on doing this, but when do you want to stop? You don't want it to go on indefinitely do you?
Logged

LiveTAG: happydog
coopasonic
Gaming Trend Senior Member

Offline Offline

Posts: 4127



View Profile WWW
« Reply #3 on: April 05, 2006, 09:32:24 PM »

When it hits the stop condition or the CPU melts, whichever comes first.
Logged

It was this moment that took the movie from being a little ho-hum to “holy shit, did that shark just eat a plane!?”
Andrew Mallon
Gaming Trend Senior Member

Offline Offline

Posts: 1901


View Profile
« Reply #4 on: April 05, 2006, 09:56:56 PM »

Something like this? I'm sure there's a more elegant way to do this that will also eliminate one of the variables. You also need to define your exit condition. Right now I set it up to exit out of the loop after it hits 13, like in your example.

Code:

        int Cur;
        int Prev;
        int x;

        Prev = 1;
        Cur = 2;

        for ( x = 1; x < 14; x =  (Cur = Prev) +  (Prev = x))
       printf("%d\n", x);
Logged
Hapfloyde
Gaming Trend Reader

Offline Offline

Posts: 131


View Profile
« Reply #5 on: April 05, 2006, 10:17:54 PM »

int counter = 1; //counter for loop
int fib = 1; //incementor
int place1 = 1; //first placeholder
int place2 = 0; //second placeholder

while (counter < (where you want it to end), step fib)
{
   place2 = fib;
   fib = fib + place1;
   place1 = place2;
};

That should work, although messily.  Don't hold me to the syntax, it's been a while since I used c++.

P.S.  Writing that caused me to switch into programmer mode and I spent 3 minutes wondering why my HTML itialic tags weren't working.

Edit: and, dammit, someone still beat me to it.
Logged

Ha, ha, ha. You have struck Hercules!
Ænima
Gaming Trend Reader

Offline Offline

Posts: 109


View Profile
« Reply #6 on: April 05, 2006, 11:37:56 PM »

Code:
int cur, prev;
for(cur=1, prev=1; cur<MAX; cur+=prev, prev=cur-prev)
printf("%d\n", cur);


edit:

This results in the 1, 2, 3, 5 ... that you asked for.
A true fibonacci starts with 0, 1, 1, 2, ... and to obtain that, you'd set cur=0 in the first statement of the for loop.  To get 1, 1, 2, ... you'd set prev=0 instead.
Logged

namatoki
Gaming Trend Reader

Offline Offline

Posts: 414


View Profile
« Reply #7 on: April 06, 2006, 03:49:18 AM »

Thanks for the replies!  I can see where you all are coming from in a logic standpoint.  I'll test out those loops.

Thanks again!
Logged
tiny ogre
Gaming Trend Reader

Offline Offline

Posts: 234


View Profile
« Reply #8 on: April 06, 2006, 04:08:52 AM »

Did you just sucker GamingTrend into doing your CS homework for you?

Nice job!
Logged
Semaj
Gaming Trend Senior Member

Offline Offline

Posts: 1946


View Profile
« Reply #9 on: April 06, 2006, 01:05:08 PM »

lol
my c++ project was working with fibbernacci and integers hundreds of numbers long.
Logged

WoW:
Venerable
Skywarden
Eagerly awaiting Drakes Fortune 2...
coopasonic
Gaming Trend Senior Member

Offline Offline

Posts: 4127



View Profile WWW
« Reply #10 on: April 06, 2006, 03:26:07 PM »

Quote from: "tiny ogre"
Did you just sucker GamingTrend into doing your CS homework for you?

Nice job!


What, you don't think the Fibonacci sequence has any practical applications?
Logged

It was this moment that took the movie from being a little ho-hum to “holy shit, did that shark just eat a plane!?”
namatoki
Gaming Trend Reader

Offline Offline

Posts: 414


View Profile
« Reply #11 on: April 06, 2006, 07:26:35 PM »

Hehe, actually my discrete math instructor was asking me about how to do this, but since I was busy working on some data structures stuff, I thought I'd ask some people here. smile  Ugh, linked lists and trees hurt my brain!
Logged
Ænima
Gaming Trend Reader

Offline Offline

Posts: 109


View Profile
« Reply #12 on: April 06, 2006, 08:17:15 PM »

Linked lists and trees are your good, good friends.
Logged

Semaj
Gaming Trend Senior Member

Offline Offline

Posts: 1946


View Profile
« Reply #13 on: April 07, 2006, 02:40:38 AM »

I hate when someone teaches C++ like its java...

Ok now we go from this state to this state...

I almost killed that man
Logged

WoW:
Venerable
Skywarden
Eagerly awaiting Drakes Fortune 2...
tiny ogre
Gaming Trend Reader

Offline Offline

Posts: 234


View Profile
« Reply #14 on: April 07, 2006, 06:23:29 AM »

Quote from: "coopasonic"
Quote from: "tiny ogre"
Did you just sucker GamingTrend into doing your CS homework for you?

Nice job!


What, you don't think the Fibonacci sequence has any practical applications?


What, you don't think teaching people to program is practical?
Logged
happydog
Gaming Trend Senior Member

Offline Offline

Posts: 1469


View Profile
« Reply #15 on: April 07, 2006, 04:34:34 PM »

http://gottabook.blogspot.com/2006/04/fib.html  :?
Logged

LiveTAG: happydog
coopasonic
Gaming Trend Senior Member

Offline Offline

Posts: 4127



View Profile WWW
« Reply #16 on: April 07, 2006, 08:03:36 PM »

Quote from: "tiny ogre"
Quote from: "coopasonic"
Quote from: "tiny ogre"
Did you just sucker GamingTrend into doing your CS homework for you?

Nice job!


What, you don't think the Fibonacci sequence has any practical applications?


What, you don't think teaching people to program is practical?


No. I find it bad for my job prospects.

Amusingly enough we're testing out agile methodology here and we are using Fibonacci numbers to assign complexity to "requirements". everyone thought I was quite the geek when I knew the name for the sequence because of this thread.
Logged

It was this moment that took the movie from being a little ho-hum to “holy shit, did that shark just eat a plane!?”
Pages: [1]   Go Up
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.19 | SMF © 2013, Simple Machines
Valid XHTML 1.0! Valid CSS!
Page created in 0.114 seconds with 57 queries. (Pretty URLs adds 0.037s, 2q)