/*
2. Create a function that returns the next value in a
Fibonacci sequence every time you call it. Add an
argument that is a bool with a default value of false such
that when you give the argument with true it “resets” the
function to the beginning of the Fibonacci sequence.
Exercise this function in main( ).
*/
#include<iostream>
using namespace std;
int Fibonacci(bool flag = false) {
static int count = 1;
static int num1 = 1;
static int num2 = 1;
if (flag)
count = 0;
if (count++ == 0) {
num1 = 1;
num2 = 1;
}
if (count++ == 1)
return num1;
else if (count++ == 2)
return num2;
else {
num2 = num1 + num2;
num1 = num2 - num1;
return num2;
}
}
int main() {
cout << Fibonacci() << endl;
cout << Fibonacci() << endl;
cout << Fibonacci() << endl;
cout << Fibonacci() << endl;
cout << Fibonacci() << endl;
cout << Fibonacci() << endl;
cout << Fibonacci() << endl;
cout << Fibonacci(true) << endl;
cin.get();
}
2. Create a function that returns the next value in a
Fibonacci sequence every time you call it. Add an
argument that is a bool with a default value of false such
that when you give the argument with true it “resets” the
function to the beginning of the Fibonacci sequence.
Exercise this function in main( ).
*/
#include<iostream>
using namespace std;
int Fibonacci(bool flag = false) {
static int count = 1;
static int num1 = 1;
static int num2 = 1;
if (flag)
count = 0;
if (count++ == 0) {
num1 = 1;
num2 = 1;
}
if (count++ == 1)
return num1;
else if (count++ == 2)
return num2;
else {
num2 = num1 + num2;
num1 = num2 - num1;
return num2;
}
}
int main() {
cout << Fibonacci() << endl;
cout << Fibonacci() << endl;
cout << Fibonacci() << endl;
cout << Fibonacci() << endl;
cout << Fibonacci() << endl;
cout << Fibonacci() << endl;
cout << Fibonacci() << endl;
cout << Fibonacci(true) << endl;
cin.get();
}