Fibbonacci Code

This is a discussion on Fibbonacci Code within the C Programming forums, part of the General Programming Boards category; Hey all, Programming newbie here. I'm programming the Fibbonacci code, but I can't figure out how to set the condition ...

  1. #1
    Registered User
    Join Date
    Sep 2006
    Posts
    7

    Fibbonacci Code

    Hey all, Programming newbie here.

    I'm programming the Fibbonacci code, but I can't figure out how to set the condition

    "if NOT EQUAL to any numerical value"

    This is what I have so far:

    Code:
    #include <stdio.h>
    #include <conio.h>
    
    long fib(int);
    
    int main()
    {
      int n;
    
      printf("Enter the Fibbonacci number you would like to view: ");
      scanf("%i", &n);
    
      if (WHAT GOES HERE?)
      {
        printf("Input error.\n");
        getch();
      }
      else if (n<0) {
        printf("Fibonacci is undefined for negative integers.\n");
        getch();
      }
      else {
        printf("The %i-th Fibonacci number is %ld\n", n, fib(n));
      }
      getch();
      return 0;
    }
    
    long fib(int n)
    {
      if (n == 1 || n == 2) 
      {
        return 1;
      } 
      else 
      {
        return (fib(n-1) + fib(n-2));
      }
    }
    Thanks for the help in advance!

  2. #2
    The Richness... Richie T's Avatar
    Join Date
    Jan 2006
    Location
    Ireland
    Posts
    469
    Was writing the following in the other thread when it was closed - might be what you're looking for:

    fgets and strtol is the way I'd do it, but you can use scanf cleverly too:

    Code:
    int num = 0;
    
    if (!scanf ("%d", &num))
    {
        printf ("Invalid input");
    }
    this code will try to read in an integer from the value entered - if a character is entered, it will print the error message - however, if the value entered is something like 213g, it will read in the 213 part, and if the value was like 12.2, it will read in the 12 part - not bullet-proof, but it might do for what you need.
    No No's:
    fflush (stdin); gets (); void main ();


    Goodies:
    Example of fgets (); The FAQ, C/C++ Reference


    My Gear:
    OS - Windows XP
    IDE - MS Visual C++ 2008 Express Edition


    ASCII stupid question, get a stupid ANSI

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    7
    Thanks Richie, I was trying your code, but every return was "the 2th number is 1". I think it just keeps refering to the base value for some reason

    But, I was playing with it, and this one worked somehow :S

    Code:
    if (scanf("%i", &n) != 1) {
        printf("Input error.\n");
        getch();
    Could someone explain Why it worked? From what I understand, it means "If the inputted integer is not equal to 1, print "input error" (which makes no sence to me, since 1 is an integer...)

  4. #4
    The Richness... Richie T's Avatar
    Join Date
    Jan 2006
    Location
    Ireland
    Posts
    469
    scanf returns the number of values successfully read in - since you are using it to read in one value, if it fails, it'll return 0 (which is not equal to 1)
    No No's:
    fflush (stdin); gets (); void main ();


    Goodies:
    Example of fgets (); The FAQ, C/C++ Reference


    My Gear:
    OS - Windows XP
    IDE - MS Visual C++ 2008 Express Edition


    ASCII stupid question, get a stupid ANSI

  5. #5
    Registered User
    Join Date
    Sep 2006
    Posts
    7
    Thank you VERY Much. ALl's working fine, now just to inprove the GUI. Thanks to all who've helped me!

    Final Code
    Code:
    #include <stdio.h>
    #include <conio.h>
    
    long fib(int);
    
    int main()
    {
      int n;
      int num;
      
      num = 0;
      
      printf("Enter the Fibbonacci number you would like to view: ");
    
      if (scanf("%i", &n) != 1) 
      {
        printf("Input error.\n");
        getch();
      }
      else if (n<0) {
        printf("Fibonacci is undefined for negative integers.\n");
        getch();
      }
      else {
        printf("The %i-th Fibonacci number is %ld\n", n, fib(n));
      }
      getch();
      return 0;
    }
    
    long fib(int n)
    {
      if (n == 1 || n == 2) 
      {
        return 1;
      } 
      else 
      {
        return (fib(n-1) + fib(n-2));
      }
    }

  6. #6
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,068
    Why the non-standard code? Dump the conio.h... change all the getch() calls to getchar() calls... it's that simple.
    Sent from my iPad®

  7. #7
    Registered User theFOX's Avatar
    Join Date
    Sep 2006
    Location
    Egypt
    Posts
    6

    Post

    A deadly more efficient fibo function using dynamic programming. it may help you later.

    Code:
     
    long long int fibo( int n )
    {
     long long int* mem = new long long int[n +1];
     long long int ret;
     
     mem[1] = mem[2] = 1;
    
     for (int i = 3 ; i <= n ; i ++)
     {
      mem[i] = mem[i -1] + mem[i -2];
     }
     
     ret = mem[n];
     
     delete[] mem;
     return ret;
    }

  8. #8
    The Richness... Richie T's Avatar
    Join Date
    Jan 2006
    Location
    Ireland
    Posts
    469
    Quote Originally Posted by theFOX
    A deadly more efficient fibo function using dynamic programming. it may help you later.

    Code:
     
    long long int fibo( int n )
    {
     long long int* mem = new long long int[n +1];
     long long int ret;
     
     mem[1] = mem[2] = 1;
    
     for (int i = 3 ; i <= n ; i ++)
     {
      mem[i] = mem[i -1] + mem[i -2];
     }
     
     ret = mem[n];
     
     delete[] mem;
     return ret;
    }
    Ehem... C board, C++ code
    No No's:
    fflush (stdin); gets (); void main ();


    Goodies:
    Example of fgets (); The FAQ, C/C++ Reference


    My Gear:
    OS - Windows XP
    IDE - MS Visual C++ 2008 Express Edition


    ASCII stupid question, get a stupid ANSI

  9. #9
    Registered User theFOX's Avatar
    Join Date
    Sep 2006
    Location
    Egypt
    Posts
    6

    Post

    uhm, sorry

    Code:
    long long int fibo( int n )
    {
        long long int* mem;
        mem = (long long int*) calloc( 0, sizeof(long long int) * (n +1) );
    
        long long int ret;
     
        mem[1] = mem[2] = 1;
    
        int i;
        for ( i = 3 ; i <= n ; i ++)
        {
             mem[i] = mem[i -1] + mem[i -2];
        }
     
        ret = mem[n];
     
        free( mem );
        return ret;
    }
    Last edited by theFOX; 09-20-2006 at 03:47 PM.

  10. #10
    Registered User
    Join Date
    Oct 2004
    Posts
    151
    Read the FAQ for why you shouldn't cast *alloc().
    System: Debian Sid and FreeBSD 7.0. Both with GCC 4.3.

    Useful resources:
    comp.lang.c FAQ | C++ FQA Lite

  11. #11
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    There are only 46 Fibonacci numbers that can fit within a 32-bit unsigned type. Use a table.
    My best code is written with the delete key.

  12. #12
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Quote Originally Posted by Prelude
    There are only 46 Fibonacci numbers that can fit within a 32-bit unsigned type. Use a table.
    Is that also true for calculating factorial?

  13. #13
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >Is that also true for calculating factorial?
    Factorial is even easier, you can only fit 12! into a 32-bit unsigned int.
    My best code is written with the delete key.

  14. #14
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,262
    Yeah, also windows calculator gives me warnings whenever I try to do 1,000,000!. It says that it will take a long time. I've not out waited it yet :/ I hope to do so some time.

  15. #15
    cwr
    cwr is offline
    Registered Luser cwr's Avatar
    Join Date
    Jul 2005
    Location
    Sydney, Australia
    Posts
    868
    Quote Originally Posted by Prelude
    There are only 46 Fibonacci numbers that can fit within a 32-bit unsigned type. Use a table.
    Actually 48

    0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, 1597, 2584, 4181, 6765, 10946, 17711, 28657, 46368, 75025, 121393, 196418, 317811, 514229, 832040, 1346269, 2178309, 3524578, 5702887, 9227465, 14930352, 24157817, 39088169, 63245986, 102334155, 165580141, 267914296, 433494437, 701408733, 1134903170, 1836311903, 2971215073

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Enforcing Machine Code Restrictions?
    By SMurf in forum Tech Board
    Replies: 21
    Last Post: 03-30-2009, 07:34 AM
  2. Values changing without reason?
    By subtled in forum C Programming
    Replies: 2
    Last Post: 04-19-2007, 10:20 AM
  3. Obfuscated Code Contest
    By Stack Overflow in forum Contests Board
    Replies: 51
    Last Post: 01-21-2005, 03:17 PM
  4. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM
  5. Replies: 0
    Last Post: 02-21-2002, 05:05 PM

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