Wrox Programmer Forums
|
C++ Programming General discussions for the C++ language. For questions specific to Microsoft's Visual C++ variant, see the Visual C++ forum instead.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the C++ Programming section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
  #1 (permalink)  
Old February 22nd, 2004, 02:54 PM
Authorized User
 
Join Date: Jan 2004
Posts: 18
Thanks: 0
Thanked 0 Times in 0 Posts
Default help please if u can

hello everybody....i have a question about the following code...its about fibonacci series...

# include <iostream.h>
# include <conio.h>

int main()
{
clrscr();

int n,i,c;
int a=0;
int b=1;
cout<<"Enter number of terms:";
cin>>n;

cout<<"\n"<<a<<""<<b;
for(i=2;i<n;i++)
{
c=a+b;
cout<<""<<c;
a=b;
b=c;
}
getch();
}

my question is in the part of :
 for(i=2;i<n;i++)
{
c=a+b;
cout<<""<<c;
a=b;
b=c;
}

i couldnt get the logic of this part....so can any pne explain to me please...?


Note: sorry if the question may look stupid..but im a beginner in c++.


Reply With Quote
  #2 (permalink)  
Old February 22nd, 2004, 11:47 PM
Authorized User
 
Join Date: Feb 2004
Posts: 76
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I'm guessing that the "" should be " " (space between the quotes).
THis keeps the individual outputs separated.

Dave

Quote:
quote:Originally posted by amahja56
 hello everybody....i have a question about the following code...its about fibonacci series...

# include <iostream.h>
# include <conio.h>

int main()
{
clrscr();

int n,i,c;
int a=0;
int b=1;
cout<<"Enter number of terms:";
cin>>n;

cout<<"\n"<<a<<""<<b;
for(i=2;i<n;i++)
{
c=a+b;
cout<<""<<c;
a=b;
b=c;
}
getch();
}

my question is in the part of :
 for(i=2;i<n;i++)
{
c=a+b;
cout<<""<<c;
a=b;
b=c;
}

i couldnt get the logic of this part....so can any pne explain to me please...?


Note: sorry if the question may look stupid..but im a beginner in c++.


Reply With Quote
  #3 (permalink)  
Old February 23rd, 2004, 08:09 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 836
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Quote:
quote:Originally posted by amahja56
for(i=2;i<n;i++)
{
    c = a + b;
    cout << " " << c;
    a = b;
    b = c;
}
Well, the Fibonacci sequence is one where each successive number in the sequence is the sum of the two previous numbers:

0 1 1 2 3 5 8 13...

Starting with 2, notice that:

Code:
   2 = 1 + 1,
   3 = 1 + 2,
   5 = 2 + 3
   8 = 3 + 5
  13 = 5 + 8

   c = a + b   <--  This is the pattern using the variable names
                    defined in your for loop.
So basically what your loop is doing is calculating the current number, c, as the sum of the two previous numbers, a and b.

Once you output the value of c, then you set up your numbers for the next iteration.

The old value of b becomes the new value of a. The old value of c becomes the new value of b.

Thus, a and b are the two most recent consecutive numbers of the sequence. The next iteration will reassign the value of a + b to c, yielding the next sum in the series.


Often it makes everything a LOT more readable if you use proper indentation and spacing.


Take care,

Nik
http://www.bigaction.org/
Reply With Quote









Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.