Thread: Can someone point out where I went wrong?

  1. #1
    Registered User
    Join Date
    Jan 2013
    Posts
    28

    Can someone point out where I went wrong?

    Code:
      // Fiboncacci Test---------------------------------        
              int n;
              int* fib;
    
    
              printf("\nFibonacci test 1: enter an integer\n");
              scanf("%d",&n);
      
              fib = a(n);
              printf("fib(%d) = ", n);
    
    
              for(int i = 0; i < n; ++i)
            {
                    printf("%d ", fib[i]);
            }
    Code:
    int a(const int c)
    {
        int i;
        int b[c];
    
    
        for(i = 2; i<c; ++i)
        {
            b[0] = 0;
            b[1] = 1;
            b[i] = b[i-1] + bi-2];
        }
            return b[i];
    }
    I have no idea where I went wrong, I have check through my course book and still I am stuck, any help appreciated! Thank you.
    Last edited by AFCKING; 03-07-2013 at 04:32 PM.

  2. #2
    Registered User TheBigH's Avatar
    Join Date
    May 2010
    Location
    Melbourne, Australia
    Posts
    426
    Set b[0] and b[1] before your loop.

    Also, in line 11, you're missing the opening square bracket in b[i-2].
    Code:
    while(!asleep) {
       sheep++;
    }

  3. #3
    Registered User
    Join Date
    Jan 2013
    Posts
    28
    Quote Originally Posted by TheBigH View Post
    Set b[0] and b[1] before your loop.

    Also, in line 11, you're missing the opening square bracket in b[i-2].
    Hi, thanks for your input the missing bracket is the copying i miss it. When I take b[0] and b[1] out it still does not show, apparently it shows some weird number

  4. #4
    Registered User TheBigH's Avatar
    Join Date
    May 2010
    Location
    Melbourne, Australia
    Posts
    426
    Do you even know what data type your fib variable is supposed to be? You declare it as a pointer to an integer, then you set it to an integer with the function "a", then you treat it as an array of ints in your printf.
    Code:
    while(!asleep) {
       sheep++;
    }

  5. #5
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    return b[i]; /* likely off by one error; also called fence post error */

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  6. #6
    Registered User
    Join Date
    Jan 2013
    Posts
    28
    Yeah I got guys, thank you. I use the int* b = malloc(....) and it works, maybe should learn dynamic array again.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 04-27-2011, 04:34 AM
  2. Point to Point Protocol and Bluetooth DUN
    By PSLoh in forum Networking/Device Communication
    Replies: 2
    Last Post: 03-03-2008, 09:44 AM
  3. fixed point / floating point
    By confuted in forum Game Programming
    Replies: 4
    Last Post: 08-13-2002, 01:25 PM
  4. floating point numbers display something wrong i think
    By Unregistered in forum C Programming
    Replies: 3
    Last Post: 04-27-2002, 03:18 AM
  5. Floating point faster than fixed-point
    By VirtualAce in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 11-08-2001, 11:34 PM