Can someone point out where I went wrong?

This is a discussion on Can someone point out where I went wrong? within the C Programming forums, part of the General Programming Boards category; Code: // Fiboncacci Test--------------------------------- int n; int* fib; printf("\nFibonacci test 1: enter an integer\n"); scanf("%d",&n); fib = a(n); printf("fib(%d) = ...

  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 03: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
    2,541
    return b[i]; /* likely off by one error; also called fence post error */

    Tim S.
    "Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the universe trying to produce bigger and better idiots. So far, the Universe is winning." Rick Cook

  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, 08: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, 10:34 PM

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