Thread: Updating Variables Within Loop

  1. #1
    Registered User
    Join Date
    Feb 2018
    Posts
    1

    Updating Variables Within Loop

    Hey everyone,

    To preface, I have very little programming knowledge and I'm only just beginning with the very basics.
    I'm supposed to be using the fibonacci sequence to estimate the golden ratio. So, I managed to create a function (which I believe works as I've tested it) and everything seems to be fine... EXCEPT I can't get the a & b variable to update at the end of my while loop. I've copied all my code below. I really can't figure out what's wrong (though I'm sure it's something super obvious) and I would really appreciate any help.

    THanks
    --> formatting is a little weird for comments after I copied the code her... sorry

    Code:
    #include <stdio.h>
    Code:
    #include <math.h>
    
    
    int fibonacci(int N) {    //N is the term of the Fibonacci sequence starting at the 0th term
    
    
        int i = 0;            //loop index
    
        if (N == 0 || N == 1) {
            return 1;
        }
    
    int previousN = 1;            //last integer in fibonacci sequence preceeding new value
    int previousPreviousN = 1;    //last integer preceeding the previousN in fibonacci sequence
    int newValue = 0;             //current value of fibonnaci sequence at term N
    
        for (i = 0; i < (N-1); i++) {
            newValue = previousN + previousPreviousN;
            previousPreviousN = previousN;
            previousN = newValue;
        }
        return newValue;
    }
    
    
    int main(void) {
    
    constdouble truePhi = (1 + sqrt(5))/2; //the true value of phi used in error calculations
        int i = 0;                          // loop index
        int a = 2;                          // a = n+1 term of Fibonacci
        int b = 1;                          // b = n term of Fibonacci
        double previousApprox = 1.0;        // previous approximation of phi
        double newApprox = 0.0;             // new approximation of phi
    double relError;                    // relative error between the computed approximation and the true value of phi
    
        while (fabs(previousApprox - newApprox) >= 0.001) {
            printf("%d  ", i);
    printf("Phi approximation = %lf  ", newApprox);
    
            relError = fabs((newApprox-truePhi)/truePhi);
            printf("Error = %lf\n", relError);
    
            previousApprox = newApprox;
            newApprox = fibonacci(a)/fibonacci(b);
    
            a++;
            b++;
            i++;
        }
    return0;
     }
    
    

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    38,621
    When posting code, make sure you do 'copy as text / paste as text' where the options are available. Copying font tags from your IDE messes up.

    > newApprox = fibonacci(a)/fibonacci(b);
    You should make your fibonacci return double, so you don't get massive rounding errors with int/int truncation.

    Code:
    #include <stdio.h>
    #include <math.h>
    
    
    double fibonacci(int N) {    //N is the term of the Fibonacci sequence starting at the 0th term
        int i = 0;            //loop index
    
        if (N == 0 || N == 1) {
            return 1;
        }
    
        int previousN = 1;            //last integer in fibonacci sequence preceeding new value
        int previousPreviousN = 1;    //last integer preceeding the previousN in fibonacci sequence
        int newValue = 0;             //current value of fibonnaci sequence at term N
    
        for (i = 0; i < (N-1); i++) {
            newValue = previousN + previousPreviousN;
            previousPreviousN = previousN;
            previousN = newValue;
        }
        return newValue;
    }
    
    
    int main(void) {
        const double truePhi = (1 + sqrt(5))/2; //the true value of phi used in error calculations
        int i = 0;                          // loop index
        int a = 2;                          // a = n+1 term of Fibonacci
        int b = 1;                          // b = n term of Fibonacci
        double previousApprox = 1.0;        // previous approximation of phi
        double newApprox = 0.0;             // new approximation of phi
        double relError;                    // relative error between the computed approximation and the true value of phi
    
        while (fabs(previousApprox - newApprox) >= 0.001) {
            printf("%d  ", i);
            printf("Phi approximation = %lf  ", newApprox);
    
            relError = fabs((newApprox-truePhi)/truePhi);
            printf("Error = %lf\n", relError);
    
            previousApprox = newApprox;
            newApprox = fibonacci(a)/fibonacci(b);
    
            a++;
            b++;
            i++;
        }
    
        return 0;
     }
     
    
    
    $ gcc -Wall -Wextra -g foo.c -lm
    $ ./a.out 
    0  Phi approximation = 0.000000  Error = 1.000000
    1  Phi approximation = 2.000000  Error = 0.236068
    2  Phi approximation = 1.500000  Error = 0.072949
    3  Phi approximation = 1.666667  Error = 0.030057
    4  Phi approximation = 1.600000  Error = 0.011146
    5  Phi approximation = 1.625000  Error = 0.004305
    6  Phi approximation = 1.615385  Error = 0.001637
    7  Phi approximation = 1.619048  Error = 0.000626
    8  Phi approximation = 1.617647  Error = 0.000239
    $
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Dec 2017
    Posts
    1,101
    I was going to mention Salem's point. Although I would've cast (one of) the return values instead of changing fibonacci's return type. I suggest making fibonacci use long ints to maximize it's range.

    Also, you only need one of the variables i, a, and b, since it's always true that a == i + 2 and b == i + 1.
    The best argument against democracy is a five minute conversation with the average voter. - Churchill

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 07-19-2013, 05:29 PM
  2. How to show input variables from loop etc
    By sakuraleaf in forum C Programming
    Replies: 9
    Last Post: 11-10-2012, 09:53 AM
  3. Help with Variables & updating Variables
    By DrC in forum C Programming
    Replies: 9
    Last Post: 01-30-2011, 02:46 AM
  4. while loop with int variables
    By joeman in forum C++ Programming
    Replies: 1
    Last Post: 02-07-2010, 01:33 PM
  5. Variables in a struct not updating
    By Frobozz in forum C++ Programming
    Replies: 6
    Last Post: 09-28-2006, 11:09 AM

Tags for this Thread