# Thread: Updating Variables Within Loop

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. 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
\$```

3. 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.