Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > C# and C > C++ and Visual C++ > C++ Programming
Password Reminder
Register
Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
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 tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old February 22nd, 2004, 02:54 PM
Authorized User
 
Join Date: Jan 2004
Location: , , .
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
Location: , , .
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
Points: 2,570, Level: 21
Points: 2,570, Level: 21 Points: 2,570, Level: 21 Points: 2,570, Level: 21
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: San Diego, CA, USA
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
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off




All times are GMT -4. The time now is 09:37 AM.


Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.