Thread: Help for loops

  1. #1
    Registered User
    Join Date
    Feb 2008
    Posts
    25

    Help for loops

    ok so im suppose to use fibonicci number

    "The sequence of integers 1,1,2,3,5,8,13....has been name the fibonacci sequence in honor of mathematician. the first two fibonacci numbers (0 and 1st) are both1; thereaftereach number in the sequence is the sum of the two preceding numbers. Write a program that prompts the user for an integer,m,then prints the mth fibonacci number."

    Code:
    /* Fibonacci Numbers
    Daniel Latorre       
    CSCI 143 */
    
    #include <math.h>
    #include <stdio.h>
    #include <stdlib.h>
    
    int main()
       {
         int fibo,m,c;
             printf("give me m: ");
             scanf("%i",&m);
         
         if(m<0){
             printf("Enter an appropiate positive integer");
             }
         else {
             for(c=0;c<m;c++)
              {
                 
                 }   
                 
                 printf("The %i Fibonacci number is: %i",m,fibo);
    i havent done much after that cause im stuck in the part of the equation.....how can i make the for loop add the sum of the previous numbers?

    any help is appreciated

  2. #2
    uint64_t...think positive xuftugulus's Avatar
    Join Date
    Feb 2008
    Location
    Pacem
    Posts
    355
    Well the problem statement has your answer. From the definition of the fibonacci number, you have: F(1) = 1, F(2) = 1, F(N) = F(N-1) + F(N-2).
    Therefore, you need two variables to track the two previous numbers. You need a variable to track the current number that has been calculated, and should that reach your 'm', the for loop ends.
    (I suppose you need to do it with a for loop as an assignment, as a while loop would be cleaner and easier to write for this problem)
    And at every step of the loop after F(k) has been calculated it's simple to proceed as F(K+1) = F(K) + F(K-1), which means that the variable that held F(K-2) needs to contain what the variable that held F(K-1) has.

    Or you could use a recursive function which maps the problem and the solution of calculating a fibonacci number more closely than any non-recursive algorithm does.
    Code:
    ...
        goto johny_walker_red_label;
    johny_walker_blue_label: exit(-149$);
    johny_walker_red_label : exit( -22$);
    A typical example of ...cheap programming practices.

  3. #3
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    You'll have to keep the two previous numbers around in order to add them.

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by xuftugulus View Post
    Or you could use a recursive function which maps the problem and the solution of calculating a fibonacci number more closely than any non-recursive algorithm does.
    To save iMalc the trouble: the iterative version can find F_1000 in 1000 steps; the recursive version requires F_1000 = 1 billion+ steps.

  5. #5
    uint64_t...think positive xuftugulus's Avatar
    Join Date
    Feb 2008
    Location
    Pacem
    Posts
    355
    Yes i know, but it's the shortest code ever! LOL!
    Code:
    ...
        goto johny_walker_red_label;
    johny_walker_blue_label: exit(-149$);
    johny_walker_red_label : exit( -22$);
    A typical example of ...cheap programming practices.

  6. #6
    Registered User
    Join Date
    Feb 2008
    Posts
    25
    Quote Originally Posted by xuftugulus View Post
    Well the problem statement has your answer. From the definition of the fibonacci number, you have: F(1) = 1, F(2) = 1, F(N) = F(N-1) + F(N-2).
    Therefore, you need two variables to track the two previous numbers. You need a variable to track the current number that has been calculated, and should that reach your 'm', the for loop ends.
    (I suppose you need to do it with a for loop as an assignment, as a while loop would be cleaner and easier to write for this problem)
    And at every step of the loop after F(k) has been calculated it's simple to proceed as F(K+1) = F(K) + F(K-1), which means that the variable that held F(K-2) needs to contain what the variable that held F(K-1) has.

    Or you could use a recursive function which maps the problem and the solution of calculating a fibonacci number more closely than any non-recursive algorithm does.
    so i have a question should i implement a if statement, just so that the first two numbers are left alone, and then put the equation in for the loop as the else statement?
    Last edited by dals2002; 03-13-2008 at 09:19 PM.

  7. #7
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by dals2002 View Post
    so i have a question should i implement a if statement, just so that the first two numbers are left alone, and then put the equation in for the loop as the else statement?
    I can't see why you would want to. If m is 0 or 1, your for loop won't run (assuming you've set up your for loop correctly), so just let it go.

  8. #8
    Registered User
    Join Date
    Feb 2008
    Posts
    25
    Quote Originally Posted by tabstop View Post
    I can't see why you would want to. If m is 0 or 1, your for loop won't run (assuming you've set up your for loop correctly), so just let it go.
    why wouldn't i run it in 1 and 0? the fibonacci rule says that when the fibo number is 1 then its equal to 1, and as stated in the problem the number 0 must be 1 too

  9. #9
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by dals2002 View Post
    why wouldn't i run it in 1 and 0? the fibonacci rule says that when the fibo number is 1 then its equal to 1, and as stated in the problem the number 0 must be 1 too
    And since that's how it's defined, your for loop doesn't start until you get to n=2.

  10. #10
    uint64_t...think positive xuftugulus's Avatar
    Join Date
    Feb 2008
    Location
    Pacem
    Posts
    355
    Fibonacci numbers are so funny.
    ~/Projects/test$ ./fib 40
    Recursively Fib_40, bye bye stack!: 102334155 in 204668309 calls.
    I will try to find the formula for the exact number of calls when calculating fibonacci numbers recursively.
    Originally Posted by dals2002:
    why wouldn't i run it in 1 and 0? the fibonacci rule says that when the fibo number is 1 then its equal to 1, and as stated in the problem the number 0 must be 1 too
    Code:
             for(c=0;c<m;c++)
    Hint: Since 0 and 1 are known, why start the loop at 0 or 1?
    Just initialize your 'fibo' variable to 1, and start the loop from 2.
    The for loop will test the condition before entering, and if m is 0, or 1 will not go off.
    Which of course means that the loop should go off for m==2.
    Last edited by xuftugulus; 03-13-2008 at 10:04 PM.
    Code:
    ...
        goto johny_walker_red_label;
    johny_walker_blue_label: exit(-149$);
    johny_walker_red_label : exit( -22$);
    A typical example of ...cheap programming practices.

  11. #11
    Registered User
    Join Date
    Feb 2008
    Posts
    25
    thanks guys i finally got it, it took some time and understanding but atleast i got it, this is my final program for those that want to look at it

    Code:
    #include <math.h>
    #include <stdio.h>
    #include <stdlib.h>
    
    int main(void) {   
        int n,i,c=1,n2=1,two;  
        
        printf("How many Fibonacci numbers do you want to compute? ");   
        scanf("&#37;d", &n);   
                if (n<=0)   
                    printf("The number should be positive.\n");   
                else {   
                   printf("\n\n\tInteger Fibonacci(Int)\n\t=====================\n");   
       
            for (i=0; i<=n; i++) {   
              printf("%12d %12d\n", i, c);   
                 two=c+n2;   
                 c=n2;   
                 n2=two;   
          }   
        }   
        getchar();
        getchar();
    }

  12. #12
    uint64_t...think positive xuftugulus's Avatar
    Join Date
    Feb 2008
    Location
    Pacem
    Posts
    355
    Looks cool, glad we could help you on this.

    Some post notes.

    You don't need <math.h>, even though Fibonacci was a mathematician

    Try to keep statements indented based on the level of braces ( block level we call it ).
    Code:
    int main() 
    {
        int x = 1; /* Level 1, 1 tab, 4 spaces */
        if(x == 1)
        {
            for(x = 0; x<10; x++) /* Level 2, 2tabs, 8 spaces */
            {
                 printf("&#37;d\n", x);  /* Level 3, 3tabs, 12 spaces */
            }
        }
    }
    Makes other people understand just by looking at which level statements are on.
    Most people will agree it looks nice too.

    Good luck, generally
    Code:
    ...
        goto johny_walker_red_label;
    johny_walker_blue_label: exit(-149$);
    johny_walker_red_label : exit( -22$);
    A typical example of ...cheap programming practices.

  13. #13
    Registered User
    Join Date
    Mar 2008
    Location
    BITS,Pilani
    Posts
    2

    Unhappy

    Quote Originally Posted by dals2002 View Post
    thanks guys i finally got it, it took some time and understanding but atleast i got it, this is my final program for those that want to look at it

    Code:
    #include <math.h>
    #include <stdio.h>
    #include <stdlib.h>
    
    int main(void) {   
        int n,i,c=1,n2=1,two;  
        
        printf("How many Fibonacci numbers do you want to compute? ");   
        scanf("%d", &n);   
                if (n<=0)   
                    printf("The number should be positive.\n");   
                else {   
                   printf("\n\n\tInteger Fibonacci(Int)\n\t=====================\n");   
       
            for (i=0; i<=n; i++) {   
              printf("%12d %12d\n", i, c);   
                 two=c+n2;   
                 c=n2;   
                 n2=two;   
          }   
        }   
        getchar();
        getchar();
    }
    why did ya write n2=two,can u tell???

Popular pages Recent additions subscribe to a feed