-5

I am writing a simple code to calculate Fabonacci numbers as an exercise. The code works, but i don't get why. I have some special cases for n=1 and n=2 which is the place of the number in the sequence (the numbers are 0 and 1). However after those, the number is calculated in this loop.

while(n>LoopCount)
{

    Fib_n=Fib_1+Fib_2;

    Fib_2=Fib_1;

    Fib_1=Fib_n;

    LoopCount++;
}

Going in to the loop, Fib_1=0, Fib_2=0, and Fib_n=1. Why does not this loop just spit out 0 no matter what? The whole code is below.

#include <iostream>

using namespace std;

int main()  
{
    cout <<"Which number of the Fibonacci sequence do you want to calculate?" <<endl;

    int n;
    cin >>n;
    cout <<endl;

    int Fib_n;
    int Fib_1;
    int Fib_2;
    int LoopCount=1;

    if(n>1)
    {
        Fib_n=1;
        LoopCount++;

        while(n>LoopCount)
        {

            Fib_n=Fib_1+Fib_2;

            Fib_2=Fib_1;

            Fib_1=Fib_n;

            LoopCount++;
        }
    }

    cout <<Fib_n;

    return 0;
}
Martin Johnsrud
  • 161
  • 1
  • 6
  • 5
    The right tool to solve such problems is your debugger. You should step through your code line-by-line *before* asking on Stack Overflow. For more help, please read [How to debug small programs (by Eric Lippert)](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/). At a minimum, you should \[edit] your question to include a [Minimal, Complete, and Verifiable](http://stackoverflow.com/help/mcve) example that reproduces your problem, along with the observations you made in the debugger. – πάντα ῥεῖ Sep 05 '16 at 20:53
  • Your program causes [undefined behaviour](http://stackoverflow.com/a/4105123/1505939) by using uninitialized variables. You should set initial values for `Fib_1` and `Fib_2` (and `Fib_n` outside the `if`). – M.M Sep 05 '16 at 20:53
  • @AndrewL. he is asking why he did NOT get `0` for calculating `0 + 0` – M.M Sep 05 '16 at 20:55
  • Using the highest compiler warnings won't let you compile it and gives you the answer! – Peter VARGA Sep 05 '16 at 22:17

2 Answers2

1
int Fib_1;
int Fib_2;

were never initialized. Therefore, the first time you calculate Fib_n=Fib_1+Fib_2;, Fib_n will get the sum of two uninitialized variables.

I have modified your code so it would work.

#include <iostream>

using namespace std;

int main()  
{
    cout <<"Which number of the Fibonacci sequence do you want to calculate?" <<endl;

    int n;
    cin >> n;
    cout << endl;

    int Fib_1 = 1;
    int Fib_2 = 1;

    int count = 0;

    while(n > count)
    {
        Fib_1 = Fib_1 + Fib_2;
        Fib_2 = Fib_1 - Fib_2;
        count++;
    }

    cout << Fib_1;
    return 0;
}
Uriel
  • 15,579
  • 6
  • 25
  • 46
0
Fib_1

You have that as an uninitalized variable, so you may get a garbage value for output.

Fib_2 = Fib_1

Next, you initialize Fib_2 with Fib_1, meaning they both share the same (random) value.

In debug mode, these are both initialized to 0, and adding them:

Fib_n=Fib_1+Fib_2;

makes the sum equal 0. In release mode, you can expect random values from the compiler. Here is more info on Uninitialized Variables.

Arnav Borborah
  • 11,357
  • 8
  • 43
  • 88