Fibonacci setup

For everyone, just starting with C++ or programming at all. Ask newbie questions in this forum!

Moderators: Darobat, RecursiveS, Dante Shamest, Bugdude, Wizard

Fibonacci setup

Postby bill_marly » Mon Dec 06, 2004 6:37 pm

Here is my code for my fibonacci sequence. It is a function, where I call it in the main funtion.
It prints everything on one line, up to the the users max value. How is it possible to ask the user how many fib. numbers they want per line?

int fibonacci(int N)
{
int a,b,c,x;
a=1;
b=1;

for(x=1; x <= N; x++)
{
if (a <= N)
{
cout<< a << endl;
}
c= a+b;
a= b;
b= c;
}
}
bill_marly
 

Postby t i l e x » Mon Dec 06, 2004 6:45 pm

Code: Select all
int fibonacci(int N)  {
    int a,b,c, nb_fib;
    a=1;
    b=1;
    N -= 1;

    std::cout << "Enter the number of fibonacci numbers before eol character:";
    std::cin >> nb_fib;

    for(int i = 1; i <= N; ++i)  {
        if(a <= N)         std::cout << a << '\t';
        if(!(i % nb_fib))  std::cout <<  std::endl;
    }
    std::cout << b;

    c = a+b;
    a = b;
    b = c;
}
PS: Might want to look at this:
Code: Select all
int nb_fib; // assumed you will give it a value

void Fibonnacci(int a, int b, int N) {
    if(N) {
        std::cout << a;
        if(!(N % nb_fib))
            std::cout << std::endl;
        Fibonnacci(b, a+b, --N);
    }
    std::cout << std::endl << "End of the fibonnacci sequence.";
}
User avatar
t i l e x
 
Posts: 3602
Joined: Wed Dec 03, 2003 3:59 pm
Location: Québec (Canada)

Postby Guest » Mon Dec 06, 2004 7:06 pm

I tried the code.... but it doens't work. All I get is a bunch of ones?
Also, what is the purpose of the 2nd function that you coded? void function?
Guest
 

Postby Guest » Mon Dec 06, 2004 7:07 pm

I'm using bloodshed compiler, in C++
Guest
 

Postby t i l e x » Mon Dec 06, 2004 7:14 pm

Sorry, this is my error on the first function:
Code: Select all
int fibonacci(int N)  {
    int a,b,c, nb_fib;
    a=1;
    b=1;
    N -= 1;

    std::cout << "Enter the number of Fibonacci numbers before eol character:";
    std::cin >> nb_fib;

    for(int i = 1; i <= N; ++i)  {
        if(a <= N)         std::cout << a << '\t';
        if(!(i % nb_fib))  std::cout <<  std::endl;

        c = a+b;
        a = b;
        b = c;
    }
    std::cout << b;
}
This one should work fine.

The other function is a recursive version of the one above. Try this:
Code: Select all
#include <iostream>

int nb_fib; // assumed you will give it a value

void Fibonnacci(int N, int a = 1, int b = 1) {
    if(N) {
        std::cout << a;
        if(!(N % nb_fib))
            std::cout << std::endl;
        Fibonnacci(b, a+b, --N);
    }
    std::cout << std::endl << "End of the fibonnacci sequence.";
}

int main(void) {
    std::cout << "Enter the number of fibonnacci numbers to output on each line: ";
    std::cin >> nb_fib;

    int N;
    std::cout << "Enter the number of fibonnacci numbers to generate: ";
    std::cin >> N;

    Fibonnacci(N);
    return 0;
}


*EDIT* Wow, that is close to being nearly close of looking somewhat like Alvaro code. :wink:
User avatar
t i l e x
 
Posts: 3602
Joined: Wed Dec 03, 2003 3:59 pm
Location: Québec (Canada)

Postby Darryl » Mon Dec 06, 2004 8:30 pm

t i l e x wrote:
*EDIT* Wow, that is close to being nearly close of looking somewhat like Alvaro code. :wink:


Naw, here is Alvaro Fibonacci code.

:)
User avatar
Darryl
 
Posts: 1342
Joined: Wed Sep 01, 2004 10:50 am
Location: Cayman Islands

Postby Guest » Mon Dec 06, 2004 8:41 pm

Here is my code....
int fibonacci1(int N)
{
int a,b,c, nb_fib;
a=1;
b=1;
N -= 1;

cout << "Enter the number of Fibonacci numbers per line: ";
cin >> nb_fib;

for(int i = 1; i <= N; ++i)
{
if(a <= N)
cout << a << '\t';
if(!(i % nb_fib))
cout << endl;

c = a+b;
a = b;
b = c;
}
cout << b;
}



Here is my output.....
Enter the number of Fibonacci numbers per line: 4
1 1 2 3
5 8 13 21
34








-1323752223
-811192543 -1109825406
Press any key to continue . . .




Why am I getting those huge negative numbers at the end??? It does that when I type anything over 34, I think. Have any idea??
Guest
 

Postby Guest » Mon Dec 06, 2004 8:44 pm

Also, in your code, you use '\t'
What exactly is that? I've never seen that before??
Guest
 

Postby Darobat » Mon Dec 06, 2004 8:45 pm

escape sequence for tab.
Code: Select all
#include <stdio.h>
struct W{char m,M[4??),w;void x(char
*W)??<w^=w;while(w[W]!=0)putchar(W[w
]^M[w++%5??));}W():m(040),w(0){char*
X="d@PLfAU\x05P)sHEMoTTPF""\31";for(
;w<5;w++[M??)=m++);x(X);}}w;main(){}
User avatar
Darobat
Moderator
 
Posts: 2572
Joined: Sat Sep 27, 2003 1:19 pm

Postby . » Mon Dec 06, 2004 9:24 pm

Anonymous wrote:Here is my code....
Code: Select all
int  fibonacci1(int N)
{
   int a,b,c, nb_fib;
   a=1;
   b=1;
   N -= 1;

   cout << "Enter the number of Fibonacci numbers per line: ";
   cin >> nb_fib;

   for(int i = 1; i <= N; ++i)
   {
      if(a <= N)
         cout << a << '\t';
      if(!(i % nb_fib))
         cout << endl;

      c = a+b;
      a = b;
      b = c;
   }
   cout << b;
}




Here is my output.....
Enter the number of Fibonacci numbers per line: 4
1 1 2 3
5 8 13 21
34


-1323752223
-811192543 -1109825406
Press any key to continue . . .


Why am I getting those huge negative numbers at the end??? It does that when I type anything over 34, I think. Have any idea??

If I pass the function the value 100 for N.
Code: Select all
for(int i = 1; i <= N; ++i)

with your code
Code: Select all
        if(a <= N)
            cout << a << '\t';

this will fail after 11 iterations and that leaves 89 more iteration in your for loop with no way to break out of the loop.
.
 

Postby Guest » Mon Dec 06, 2004 10:00 pm

When I pass it 100, here's what I get.

0
1
1
2
3
5
8
13
21
34
55
89
-1323752223
-811192543
-298632863
-1109825406
-1408458269
-1781832971
-1418756969
-1055680967
-1709589543
-944741150
-1958435240
-1262539787
-188547518
-2015728079
-433386095
-1036647147
-660827267
-285007387
-945834654
-1230842041
-1289228135
-401779575
-1691007710
-2092787285
-1581614984
-1070442683
-2079590721
-1507123775
-798870975
-90618175
-889489150

Press any key to continue . . .



What is going wrong?[/code]
Guest
 

Postby . » Mon Dec 06, 2004 10:18 pm

You are over running the range of an int. I think that you will agree that
Code: Select all
-1323752223
-811192543

These values are less than 100?
But the root of this problems is in you for statement.
Code: Select all
for(int i = 1; i <= N; ++i)

You have not given the loop a way to stop if
Code: Select all
if(a <= N)

This will be after 12 iterations and you go on for another 88 which exceed the range of an int. You could try "unsigned int" but that is probably a band aid approach. The real solution would be to break out of the loop when
Code: Select all
if(a <= N)

so you could try this
Code: Select all
   for(int i = 1; i <= N && a <= N; ++i)
   {
      if(a <= N)
.
 

Postby . » Mon Dec 06, 2004 10:28 pm

also if this is you complete function there is a little problem
Code: Select all
int  fibonacci1(int N)
{
   int a,b,c, nb_fib;
   a=1;
   b=1;
   N -= 1;

   cout << "Enter the number of Fibonacci numbers per line: ";
   cin >> nb_fib;

   for(int i = 1; i <= N; ++i)
   {
      if(a <= N)
         cout << a << '\t';
      if(!(i % nb_fib))
         cout << endl;

      c = a+b;
      a = b;
      b = c;
   }
   cout << b;
}

You give this function a return type of int and there is no return statement ( this should be a compiler error for you ) so you should give it a return statement or make it void
Code: Select all
void  fibonacci1(int N)
.
 

Postby not the OP » Mon Dec 06, 2004 10:46 pm

t i l e x wrote:Sorry, this is my error on the first function:
Code: Select all
int fibonacci(int N)  {
    int a,b,c, nb_fib;
    a=1;
    b=1;
    N -= 1;

    std::cout << "Enter the number of Fibonacci numbers before eol character:";
    std::cin >> nb_fib;

    for(int i = 1; i <= N; ++i)  {
        if(a <= N)         std::cout << a << '\t';
        if(!(i % nb_fib))  std::cout <<  std::endl;

        c = a+b;
        a = b;
        b = c;
    }
    std::cout << b;
}
This one should work fine.

The other function is a recursive version of the one above. Try this:
Code: Select all
#include <iostream>

int nb_fib; // assumed you will give it a value

void Fibonnacci(int N, int a = 1, int b = 1) {
    if(N) {
        std::cout << a;
        if(!(N % nb_fib))
            std::cout << std::endl;
        Fibonnacci(b, a+b, --N);
    }
    std::cout << std::endl << "End of the fibonnacci sequence.";
}

int main(void) {
    std::cout << "Enter the number of fibonnacci numbers to output on each line: ";
    std::cin >> nb_fib;

    int N;
    std::cout << "Enter the number of fibonnacci numbers to generate: ";
    std::cin >> N;

    Fibonnacci(N);
    return 0;
}


*EDIT* Wow, that is close to being nearly close of looking somewhat like Alvaro code. :wink:

you do realize that nether of the two work?
The first one does not break out of the loop before overflows the int and and the second one does not print all the values to the screen? :)
not the OP
 

Postby Guest » Mon Dec 06, 2004 10:53 pm

Finally... I got it to work....

Thank You guys!!!!! 8)
Guest
 

Next

Return to For Beginners

Who is online

Users browsing this forum: No registered users and 1 guest

cron