12 Replies - 8188 Views - Last Post: 18 October 2008 - 02:27 PM Rate Topic: -----

#1 dreamygirl  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 67
  • Joined: 08-October 08

A program that prints a sequence of fibonacci numbers

Post icon  Posted 16 October 2008 - 12:51 PM

Good morning or evening :rolleyes:

My program should prints a sequence of 20 fibonacci numbers in a simple way

As u all know the f(n) of fibonacci numbers is as follows:

0 if n=0
1 if n=1
fn-1 + fn+1 if n>1

I made the code and there's no errors , but it gives me nothing !!!

Can anyone tell me what's wrong with my code ?




#include <iostream>
using namespace std;

int main () {

		

					cout<<" --(( W E L C O M E ))-- \n\n\n"<<endl;
		cout<<" This program shows a series of fibonacci numbers \n\n\n"<<endl;
					cout<<"-------------------------------"<<endl;


		int n=0;
		int fnum;
		int counter=-1;


while  (n<=20)  {

	if (n==1){

		fnum=1;
		counter=counter+1;
	}

	while (n>1){
		
	fnum=(n-1)+(n+1);
	counter=counter+1;
	}
	


	}

	cout<<fnum<<endl;


	if (n==0){
		 fnum=0;
		 counter=counter+1;
	}



		return 0;

}


Is This A Good Question/Topic? 0
  • +

Replies To: A program that prints a sequence of fibonacci numbers

#2 Gloin  Icon User is offline

  • Expert Schmexpert...
  • member icon

Reputation: 235
  • View blog
  • Posts: 4,489
  • Joined: 04-August 08

Re: A program that prints a sequence of fibonacci numbers

Posted 16 October 2008 - 12:56 PM

The program enters this loop and it never gets out of there.

while (n>1){

fnum=(n-1)+(n+1);
counter=counter+1;
}

No, sorry, that as hasty..
n is never updated at all..

This post has been edited by Gloin: 16 October 2008 - 12:58 PM

Was This Post Helpful? 0
  • +
  • -

#3 Gloin  Icon User is offline

  • Expert Schmexpert...
  • member icon

Reputation: 235
  • View blog
  • Posts: 4,489
  • Joined: 04-August 08

Re: A program that prints a sequence of fibonacci numbers

Posted 16 October 2008 - 01:04 PM

Why do you concern with the special case where n = 0 anyways..

If the assignment is to print the 20 first numbers in the fibonacci sequence, start from 1.

Also the loop is a controlled loop, use for instead of while (if you're allowed)
Was This Post Helpful? 0
  • +
  • -

#4 NickDMax  Icon User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2250
  • View blog
  • Posts: 9,245
  • Joined: 18-February 07

Re: A program that prints a sequence of fibonacci numbers

Posted 16 October 2008 - 01:08 PM

well my first suggestion is to clean up the code. You need your code to be clean, clear, and easily scanned with the eye. When you code is a hodgepodge of lines of code scattered about the editor it is very easy to miss what is going on.

For example, as mentioned above. You have an infinite loop. You never update the variable n, so n never changes, so the program can never reach the condition of (n > 20) need to exit the loop.

You also don't have anything that outputs the current number.
Was This Post Helpful? 0
  • +
  • -

#5 >>codingGeek<<  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 28
  • Joined: 12-June 08

Re: A program that prints a sequence of fibonacci numbers

Posted 16 October 2008 - 01:24 PM

I do believe the logic is wrong here.

The Fibonacci Sequence is represented as F(n) = F(n-1) + F(n-2).

F(1) = 1
F(2) = 1
F(3) = F(2) + F(1) = 1 + 1 = 2
F(4) = F(3) + F(2) = 2 + 1 = 3

I recommend:
a = 1;
b = 1;
fib = 0; 

for (int i = 0; i < 20; i++)
{
  fib = a + b;
  b = a;
  a = fib;
 }



Was This Post Helpful? 0
  • +
  • -

#6 dreamygirl  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 67
  • Joined: 08-October 08

Re: A program that prints a sequence of fibonacci numbers

Posted 16 October 2008 - 01:49 PM

hmmmm so you guys mean that i can't put " if" inside the while loop ??

how can i include all the cases within the while loop then ?


can any one give me a hint on how to start over?

coz i feel i'm lost again XD

>>codingGeek<< thanx but we're not allowed to use this yet.
Was This Post Helpful? 0
  • +
  • -

#7 NickDMax  Icon User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2250
  • View blog
  • Posts: 9,245
  • Joined: 18-February 07

Re: A program that prints a sequence of fibonacci numbers

Posted 16 October 2008 - 02:47 PM

Well there is a recent post about this topic you may want to look over. If you look back a page or two and search of Fibonacci I am sure you will see it.

You CAN use and if inside a while and visa-versa -- question is -- do you want to? does it help you solve your problem.

generally the algorithm followed is:

output 1
let n = 1
let oldn = n;
begin:
output n
let next = n + oldn;
oldn = n;
n = next
loop until(n > max value)

there are various versions (some in the snippets section here I believe). I am not saying the above is the best code (its not the way I would do it, but then again I like recursion).
Was This Post Helpful? 0
  • +
  • -

#8 David W  Icon User is offline

  • DIC supporter
  • member icon

Reputation: 281
  • View blog
  • Posts: 1,788
  • Joined: 20-September 08

Re: A program that prints a sequence of fibonacci numbers

Posted 16 October 2008 - 02:55 PM

#include <iostream>
#include <iomanip> // for setw(w) ... 'w' is the width of a print field

/*
    Fibonacci series:  (a DIC dream team project )
    This is a simple example and does not use recursive calls
    which is often how this Fibonacci series is obtained.
    
    F(1) = 1
    F(2) = 1
    F(3) = F(2) + F(1) = 1 + 1 = 2
    F(4) = F(3) + F(2) = 2 + 1 = 3
*/

const int max =32;

int main()
{
    using std::cin;
    using std::cout;
    using std::setw;
    
    int a = 1;
    int b = 1;
    int fib = 0;
    cout << "The Fibonacci Sequence for the first " << max << " numbers is\n\n"; 
    cout << "1 1 ...\n\n";
    for (int i = 0; i < max-2; i++)
    {
        fib = a + b;
        cout << setw(8) << fib;
        b = a;
        a = fib;   
    }
    cin.get();
} 

This post has been edited by David W: 16 October 2008 - 02:57 PM

Was This Post Helpful? 0
  • +
  • -

#9 dreamygirl  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 67
  • Joined: 08-October 08

Re: A program that prints a sequence of fibonacci numbers

Posted 17 October 2008 - 05:13 AM

View PostNickDMax, on 16 Oct, 2008 - 02:47 PM, said:

Well there is a recent post about this topic you may want to look over. If you look back a page or two and search of Fibonacci I am sure you will see it.

You CAN use and if inside a while and visa-versa -- question is -- do you want to? does it help you solve your problem.

generally the algorithm followed is:

output 1
let n = 1
let oldn = n;
begin:
output n
let next = n + oldn;
oldn = n;
n = next
loop until(n > max value)

there are various versions (some in the snippets section here I believe). I am not saying the above is the best code (its not the way I would do it, but then again I like recursion).


Actually i've seen the post and it was all good but the problem is that we're not allowed to use any ideas that we still didnt take during the classes.

I'll try using your algorithm

but what does oldn stands for ??




View PostDavid W, on 16 Oct, 2008 - 02:55 PM, said:

#include <iostream>
#include <iomanip> // for setw(w) ... 'w' is the width of a print field

/*
    Fibonacci series:  (a DIC dream team project )
    This is a simple example and does not use recursive calls
    which is often how this Fibonacci series is obtained.
    
    F(1) = 1
    F(2) = 1
    F(3) = F(2) + F(1) = 1 + 1 = 2
    F(4) = F(3) + F(2) = 2 + 1 = 3
*/

const int max =32;

int main()
{
    using std::cin;
    using std::cout;
    using std::setw;
    
    int a = 1;
    int b = 1;
    int fib = 0;
    cout << "The Fibonacci Sequence for the first " << max << " numbers is\n\n"; 
    cout << "1 1 ...\n\n";
    for (int i = 0; i < max-2; i++)
    {
        fib = a + b;
        cout << setw(8) << fib;
        b = a;
        a = fib;   
    }
    cin.get();
} 



Thanx i'll save it for later use , we're currently not allowed to use <iomanip> and setw =(
Was This Post Helpful? 0
  • +
  • -

#10 Gloin  Icon User is offline

  • Expert Schmexpert...
  • member icon

Reputation: 235
  • View blog
  • Posts: 4,489
  • Joined: 04-August 08

Re: A program that prints a sequence of fibonacci numbers

Posted 17 October 2008 - 05:30 AM

let next = n + oldn;
oldn = n;
n = next

Compare the above to:

Fib[3] = Fib[2] + Fib[1]
next = n + oldn (After this calculation we no longer care about the value in oldn and we replace it with n)
Fib[1] = Fib[2]
oldn = n (We also replace n with next since the value in n was put in oldn)
Fib[2] = Fib[3]

Then we iterate.
Was This Post Helpful? 0
  • +
  • -

#11 David W  Icon User is offline

  • DIC supporter
  • member icon

Reputation: 281
  • View blog
  • Posts: 1,788
  • Joined: 20-September 08

Re: A program that prints a sequence of fibonacci numbers

Posted 17 October 2008 - 09:43 AM

Quote

Thanx i'll save it for later use , we're currently not allowed to use <iomanip> and setw =(


You are very welcome ... but ...it is not a big do to take that extra formatting away like this ...

#include <iostream>

using namespace std;

/*
    Fibonacci series:  (a DIC dream team project)
    This is a simple example and does not use recursive calls
    which is often how this Fibonacci series is obtained.
    
    F(1) = 1
    F(2) = 1
    F(3) = F(2) + F(1) = 1 + 1 = 2
    F(4) = F(3) + F(2) = 2 + 1 = 3
*/

const int MAX=32;

int main()
{
    int a = 1;
    int b = 1;
    int fib = 0;
    cout << "The Fibonacci Sequence for the first " 
         << MAX << " numbers is:\n\n1 1 ";
    for (int i = 0; i < MAX-2; i++)
    {
        fib = a + b;
        cout << fib << " ";
        b = a;
        a = fib;   
    }
    cin.get(); // keep window open until 'Enter' key pressed
}

This post has been edited by David W: 17 October 2008 - 09:50 AM

Was This Post Helpful? 0
  • +
  • -

#12 David W  Icon User is offline

  • DIC supporter
  • member icon

Reputation: 281
  • View blog
  • Posts: 1,788
  • Joined: 20-September 08

Re: A program that prints a sequence of fibonacci numbers

Posted 18 October 2008 - 09:19 AM

If you would like to see, yet an other, but RECURSIVE, Fibonacci series, see this:

#include <iostream>
#include <cctype> /* for toupper */
using namespace std;

/*
    Fibonacci series:  with recursive calls
    F(1) = 1
    F(2) = 1
    F(3) = F(2) + F(1) = 1 + 1 = 2
    F(4) = F(3) + F(2) = 2 + 1 = 3
    
    by D.W. Zavitz 
    developers-heaven.net/forum/index.php/topic,46.0.html
*/

int fib( int n ) 
{
    /*         
        To handle the special cases: fib(2)=1; fib(1)=1;
        THIS SPECIAL definition for fib(2) and fib(1) ...
        is properly HANDLED by the next statement:
    */
    if (n <= 2 ) return 1; 
    
    /*
        The following will push onto the 'stack memory' 
        ALL (function) pairs like this:
        <fib(n-1), fib(n-2)>, .... <fib(3),fib(2)>,  <fib(2),fib(1)>
        BUT WE then have put on the stack 
        THAT fib(2) is 1 and fib(1) is 1 ....
        So ... (popping the stack on return ...)
        'pop' (i.e. TAKE) from the TOP of stack ... 
        starting with fib(1) = 1 and fib(2) =1
        
        WE NOW KNOW how to RETURN all these values: 
        
        fib(1)= 1; // special well defined case
        fib(2)= 1; // special well defined case
        fib(3) = fib(2)+fib(1) = 2; // recursively defined cases begin ...
        fib(4) = fib(3)+fib(2) = 3;
        fib(5) = fib(4)+fib(3) = 5;
        fib(6) = fib(5)+fib(4) = 8;
        .
        .
        .
        fib(n) = fib(n-1) + fib(n-2)
    */

    else return fib(n-1) + fib(n-2);
}

int main()
{
    cout << "The following will find the Fibonacci series ...\n\n"
         << "Notice that it will take progressively longer to\n"
         << "find the next term, if you ask for much more than about\n"
         << "40 or so terms ...depending on the speed of your CPU.\n\n"
         << "Also ... there is an upper limit (46) to the number of terms\n"
         << "that may be accurately found, depending on the number of bits\n"
         << "in an 'int' used by your Compiler, (if ... 32 bits as in 2008).\n\n"; 
    do
    {
        cout << "How many terms of Fibonacci do you wish to see: ";
        int count;
        cin  >> count;
        cout << "The Fibonacci Sequence for the first " << count << " numbers is\n\n";
                       
        for( int i=1; i<=count ; ++i ) std::cout << fib( i ) << " ";
        
        cout << "\n\nPress 'Enter' to continue ...   or 'q' to quit: ";
        cin.sync(); /* 'flush' cin stream */   
        
    }while( toupper( cin.get() ) != 'Q' ); /* wait for 'Enter' to be pressed */
}

Was This Post Helpful? 0
  • +
  • -

#13 NickDMax  Icon User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2250
  • View blog
  • Posts: 9,245
  • Joined: 18-February 07

Re: A program that prints a sequence of fibonacci numbers

Posted 18 October 2008 - 02:27 PM

oldn -- old n -- the old value of n -- the value of n from the last iteration.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1