Fibonacci Series Logic..

Pages: 12
I want some one to explain Fibonacci series logic in c++..
For a Beginner its a difficult to get it completely..
Here is a one method of explain to find Fibonacci series. I Think It Is Well Explanation..

http://fahad-cprogramming.blogspot.com/2013/07/c-program-to-find-fibonaaci-series-with.html
1,2,3,5,8,13,21...

To find the next number in the Fibonacci sequence you would add 21 + 13 and get 34.

There are many ways to create this pattern in C++. You could use a for loop or even recursion.

What exactly do you need?
I quickly wrote up something which gives you the first number of terms in the Fibonacci series. Maybe not optimum by any means, but it works.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#include <iostream>

using namespace std;

int main()
{
    cout << "How many terms do you want to find?" << endl;
    int Fibonacci;
    cin >> Fibonacci;
    int FibTerms[Fibonacci]; // initializes an array with the number of elements requested.
    if (Fibonacci>=1)
    {
        FibTerms[0]=1;
        cout << FibTerms[0] << " ";
    }
    if (Fibonacci>=2)
    {
        FibTerms[1]=1;
        cout << FibTerms[1] << " ";
    }
    if (Fibonacci>=3)
    {
        for (int i=2; i<Fibonacci; i++) // we have already made the first 2 terms
        {
            FibTerms[i]=FibTerms[i-1]+FibTerms[i-2];//adds the previous 2 terms.
            cout << FibTerms[i] << " ";
        }
    }
    cin.clear();
    cin.ignore(255, '\n');
    cin.get();

}
Last edited on
closed account (Dy7SLyTq)
that doesnt work. you cant have an array with a variable space
why can't we have an array with variable space? we can define it's size during runtime.
closed account (Dy7SLyTq)
not in c++ you cant
I did test the code before I posted it.

http://s293.photobucket.com/user/manudude02/media/fibcpp_zps3024f726.png.html

Only change I made between posting and that screenshot was change the array to type long long so you can have more terms before it hits the number cap.
Last edited on
closed account (Dy7SLyTq)
what compiler are you using?
GNU GCC compiler
Last edited on
closed account (Dy7SLyTq)
thats weird its not supposed to let you do that
my compiler gives an error when I try that. says value must be constant.
Last edited on
closed account (Dy7SLyTq)
compilers dont usually allow that manga. manudude is the first ive seen to do that. everyone else uses a stl container or dynamic array. dont do what manga did because 99/100 it wont work
closed account (N36fSL3A)
Well if you are using C++ why not.... VECTORS!

1
2
3
4
5
6
7
8
9
10
cin >> Fibonacci;
std::vector<int> FibTerms;

FibTerms.reserve(Fibonacci);

for(int f = 0; f < Fibonacci; f++)
{
    int temp;
    FibTerms.push_back(temp);
}


That's all you gotta change and it should work.

EDIT - Forgot parenthesis on the pushback function
Last edited on by Fredbill30
can you blame me for trying? mandude did it first!
closed account (Dy7SLyTq)
no i dont. im just letting you know for future ref
closed account (N36fSL3A)
So does my example work?
And here's using the new command.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
// Fibonacci Sequence.cpp : main project file.

#include <iostream>

using namespace std;

int main()
{
    cout << "How many terms do you want to find?" << endl;
    int Fib;
    cin >> Fib;
	const int Fibonacci = Fib;
	int* FibTerms = new int[Fibonacci]; // initializes an array with the number of elements requested.
    if (Fibonacci>=1)
    {
        FibTerms[0]=1;
        cout << FibTerms[0] << " ";
    }
    if (Fibonacci>=2)
    {
        FibTerms[1]=1;
        cout << FibTerms[1] << " ";
    }
    if (Fibonacci>=3)
    {
        for (int i=2; i<Fibonacci; i++) // we have already made the first 2 terms
        {
            FibTerms[i]=FibTerms[i-1]+FibTerms[i-2];//adds the previous 2 terms.
            cout << FibTerms[i] << " ";
        }
    }
    delete [] FibTerms;
    cin.clear();
    cin.ignore(255, '\n');
    cin.get();
}
Last edited on
closed account (N36fSL3A)
You can't set a constant to a variable value in VC++.

That's why you just do this:
1
2
3
4
5
6
7
8
9
10
11
12
cin >> Fibonacci;
std::vector<int> FibTerms;

FibTerms.reserve(Fibonacci);

for(int f = 0; f < Fibonacci; f++)
{
    int temp;
    FibTerms.push_back(temp);
}

//And then continue on with the rest of your program. 
Last edited on by Fredbill30
There is no reason to use an array or vector at all for this.

The standard requires that array sizes are compile time constants. Compilers that allow VLAs in C++ code do so though an extension that can be disabled so that the compiler is conforming.

Here's an alternate way to approach this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#include <iostream>

class FibonacciSequence
{
public:
    typedef unsigned long long value_type ;

    FibonacciSequence() : _currentTerm(0), _prevTerm(1) {}
    value_type operator()() ;

private:
    value_type _currentTerm ;
    value_type _prevTerm ;
};

FibonacciSequence::value_type FibonacciSequence::operator()()
{
    value_type term = _currentTerm ;

    _currentTerm += _prevTerm ;
    _prevTerm = term ;

    return term ;
}

int main()
{
    std::cout << "Number of terms to find: " ;
    unsigned nTerms ;
    std::cin >> nTerms ;

    FibonacciSequence seq ;
    for ( unsigned i=0; i<nTerms; ++i )
        std::cout << seq() << ' ' ;

    std::cout << '\n' ;
}

closed account (N36fSL3A)
Well the other guy whipped out code so I was simply fixing it.
Pages: 12