been a week since i started learning c++, im trying to write a program for Fibonacci series using loops but i was not able to think of some logic so i took help of net. the if loop in this code, im not able to figure out hows it working. can someone plz explain it thx.
#include <iostream>
using namespace std;
int main()
{
int n, first = 0, second = 1 , next, c;
cout<<"enter no of terms";
cin>> n;
cout<<"first" << n << "terms of febonacci series are:" << "\n";
for ( c = 0 ; c < n ; c++ )
{
if ( c <= 1 )
next = c; // n = c
else
{
next = first + second;
first = second;
second = next;
}
cout<< next << "\n";
The point is: since the Fibonacci sequence is defined recursively (so value t depends on the values of t-1 and t-2), it is "undefined" without having two starting values (0 and 1, or 1 and 1, depending on preference).
Either you can skip the first two (start at c = 2), or you can use the fix they use here. If you start the series as "0, 1", then you can say that Fib(i) = i for i [0, 1].
Frankly, it's much easier (and it makes more sense) to hardcode the first two steps.
well i started c++ just a week ago and i didn't really get what is Fib(i) = i for i [0, 1] lol . i finished till here http://www.cplusplus.com/doc/tutorial/control/ ( till for loop). im taking a day break just to practice loops and other basic concepts.
I don't agree that it's stupid to start there, as fibonacci has two seeded values of 0 and 1 (F0 and F1 consecutively), and this has been compensated for in the code and the user interface quite obviously states that we are being shown the first n terms in the fibonacci sequence (0 being the first).
To show a run through of the code:
the for loop makes int c increment each time it goes through.
if c is less than or equal to 1 then int next is assigned the value of c.
otherwise next =1st + 2nd (0+1 the first time round), then first is given a new value equal to the current value of 2nd, and 2nd is then given the value currently held in next.
That all depends on how confident you feel with loops now.
If you haven't already, I'd suggest going through a few more examples of loops just to make sure you understand what's going on and perhaps try creating your own simple loop. Otherwise move on ahead.
#include <iostream>
int main ()
{
unsigned prev = 0, curr = 1, next, num_iterations;
// prev, and curr are the seeds. The names are not important besides we automatically know them at the start of the loop.
std::cin >> num_iterations;
// Here I ask for the location in the sequence they want to print. 0 will produce no output. 1 will print one number of the sequence. 2 prints 2 numbers. So on and so forth.
if (num_iterations > 0) // An else if control statement is not useful here as I want both to happen if the num_iterations > 1. There are obviously more efficient ways than this.
std::cout << prev << " ";
if (num_iterations > 1)
std::cout << curr << " ";
for (int i = 2; i < num_iterations; ++i) // Loop starts at two. If num_iterations < 2 then there is no reason to print the other numbers of the sequence besides the seeds 0 and 1.
{
next = prev + curr; // Get the next number in the sequence by adding the previous number and the current number of the sequence.
std::cout << next << " "; // Print to screen.
prev = curr; // Set previous number to the old current.
curr = next; // The current number is the one we just printed.
}
return 0; // Program ends.
}
I used unsigned because the Fibonacci sequence does not include negative numbers, so its safe to allow a larger range of positive numbers.
And std::cin is the same as cin>>; std:: is the namespace. If I put using namespace std; at the top of the program I would not need to put std:: infront of cout or cin.