recursive definition function

This is a discussion on recursive definition function within the C Programming forums, part of the General Programming Boards category; i am writing a small prog which will return thr nth term of the fibonacci but keep getting constant expression ...

  1. #1
    Registered User
    Join Date
    Mar 2003
    Posts
    8

    recursive definition function

    i am writing a small prog which will return thr nth term of the fibonacci

    but keep getting
    constant expression required
    any help would be great



    #include<stdio.h>
    #include<math.h>
    #include<conio.h>

    main();

    // function fibonacci(int)

    /**
    * long fibonacci(int n)
    * Returns the nth term in a
    * Fibonacci sequence.
    * Parameters:
    * n - an integer >= 0.
    * Return value:
    * long - The Fibonacci value of the nth term
    * (-1 for illegal input).
    */
    long fibonacci(int n)
    {
    if ( n < 0 )
    return -1;

    if ( n == 0 || n == 1 )
    return 1;

    long sequence[n+1]; // constant expression required //
    sequence[0] = 1;
    sequence[1] = 1;
    for ( int i = 2; i <= n; i++ )
    sequence[i] = sequence[i-2] + sequence[i-1];
    return sequence[n];
    }
    Last edited by jwalder; 07-29-2003 at 12:46 PM.

  2. #2
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Did you miss this post?
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  3. #3
    Registered User
    Join Date
    Mar 2003
    Posts
    8
    thanks for the advice hammer i am new here as you can se

  4. #4
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    >constant expression required

    C90 does not support variable-length arrays. The diagnostic message means the following.
    Code:
    int n = 5;
    long sequence[n+1]; /* illegal, but... */
    long seq[6]; /* ... this is ok */
    Searching for 'fibonacci' and 'recursion' -- both here and on Google -- ought to turn up numerous examples of a recursive fibonacci function.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  5. #5
    Registered User
    Join Date
    Mar 2003
    Posts
    8
    many thanks i'll try a loop

  6. #6
    Registered User
    Join Date
    Mar 2003
    Posts
    8
    many thanks i'll try a loop

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Seg Fault in Compare Function
    By tytelizgal in forum C Programming
    Replies: 1
    Last Post: 10-25-2008, 04:06 PM
  2. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 07:39 AM
  3. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM
  4. structure vs class
    By sana in forum C++ Programming
    Replies: 13
    Last Post: 12-02-2002, 07:18 AM
  5. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 06:06 PM

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