Fibonacci Questoin

This is a discussion on Fibonacci Questoin within the C++ Programming forums, part of the General Programming Boards category; Hey guys first post here. I just switched my major to computer science last semester. So first my c++ class ...

  1. #1
    Registered User
    Join Date
    Sep 2011
    Location
    Illinois
    Posts
    8

    Fibonacci Questoin

    Hey guys first post here. I just switched my major to computer science last semester. So first my c++ class and this would be my second assignment. I need to write a function(in the from of a program for testing purposes) that prints the all the Fibonacci numbers less than 30,000. So my below code does all this except it prints one number after 30,000. Can anyone give me some idea on how to fix it? Or maybe a better a better way to implement it with a for loop? Thanks a ton in advance.

    Code:
    #include<iostream>
    
    using namespace std;
    
    int main()
    {
    	//Declaration
    	unsigned int fib1=0, fib2=1, fib3=0;
    	
            //fibonacci loop
    	while(fib3<30000)
    	{
    		fib3=fib1+fib2;
    		cout<<fib3<<endl;
    		fib1=fib2;
    		fib2=fib3;
    	}
    	return(0);
    }
    Last edited by drumerboy3841; 09-10-2011 at 05:25 PM.

  2. #2
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,866
    Quote Originally Posted by drumerboy3841 View Post
    Code:
    #include<iostream>
    
    using namespace std;
    
    int main()
    {
    	//Declaration
    	unsigned int fib1=0, fib2=1, fib3=0;
    	
            //fibonacci loop
    	while(fib<30000)
    	{
    		fib3=fib1+fib2;
    		cout<<fib3<<endl;
    		fib1=fib2;
    		fib2=fib3;
    	}
    	return(0);
    }
    My guess is that it has something to do with your while loop. However I can't tell since fib doesn't exist. Assuming you intended fib3 note when during your loop fib3 exceeds 30,000. It is after you print it. Just move your summation to the last line in the loop.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  3. #3
    Registered User
    Join Date
    Sep 2011
    Location
    Illinois
    Posts
    8
    oops i just copied wrong :/ it was supposed to be (fib3<30000). but ill give it a try switching the summation. thank you.

  4. #4
    Registered User
    Join Date
    Sep 2011
    Location
    Illinois
    Posts
    8
    Well all i did was move the cout line to the top of the loop and it fixed the problem. thanks a lot. but out of curiosity; the fib3=fib1+fib2 doesn't really seem to do anything, since, at then end of the loop fib3 is reassigned anyways.... So why does the program give me an unlimited amount of 000000 when i delete that line and run it??

    Code:
    #include<iostream>
    
    using namespace std;
    
    int main()
    {
    	//Declaration
    	unsigned int fib1=0, fib2=1, fib3=0;
    	
    	
    	while(fib3<30000)
    	{
    		cout<<fib3<<endl;
    		fib3=fib1+fib2;
    		fib1=fib2;
    		fib2=fib3;
    	}
    	return(0);
    }

  5. #5
    The larch
    Join Date
    May 2006
    Posts
    3,573
    It's not reassigned at the end of the loop. That line is the only one that assigns to fib3.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  6. #6
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    1,616
    @anos's right. Why would you think that?
    Devoted my life to programming...

  7. #7
    Registered User
    Join Date
    Sep 2011
    Location
    Illinois
    Posts
    8
    Because I was sleep deprived and blind when I was looking at it :/ thanks guys haha

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. nth fibonacci
    By meriororen in forum C Programming
    Replies: 1
    Last Post: 08-10-2009, 05:13 AM
  2. sum of fibonacci
    By bazzano in forum C Programming
    Replies: 2
    Last Post: 08-18-2005, 09:28 AM
  3. fibonacci(10,000)
    By blindman858 in forum C++ Programming
    Replies: 4
    Last Post: 05-20-2005, 12:03 AM
  4. New silly newb questoin. Returning vector from a class
    By Gatt9 in forum C++ Programming
    Replies: 8
    Last Post: 03-27-2005, 07:50 PM
  5. Very simple questoin.
    By Unishine in forum C Programming
    Replies: 1
    Last Post: 03-31-2002, 04:13 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21