Reputation: 111
I am trying to learn c++ having already started programming in python. This is a simple program to calculate the fibonacci numbers between two values a,b. However when i run the code only the number 1 is printed and I can not figure out why. I think it is something to do with using return inside the for loop. Any help would be greatly appreciated
#include <iostream>
using namespace std;
int fibo(int b,int a=0){
int x=0;
int y=1;
for(int i=0; i<=b; i++){
int x0=x;
int z=x+y;
x=y;
y=z;
if(x>a){
return x0;
}
}
}
int main()
{
cout << fibo(100)<<endl;
return 0;
}
Here is the python function just for reference
def fibo(b,a=0):
x=0
y=1
while x<=b:
z=x+y
x0=x
x=y
y=z
if x>a:
print x0
I have also tried the following in c++
#include <iostream>
using namespace std;
int fibo(int b,int a=0){
int x=0;
int y=1;
for(int i=0; i<=b; i++){
int x0=x;
int z=x+y;
x=y;
y=z;
if(x>a){
cout << x0 <<endl;
}
}
}
int main()
{
fibo(100);
return 0;
}
However this gives fibonacci numbers beyond the value of b
Upvotes: 0
Views: 576
Reputation: 24939
Here's the exact port of your code from Python to C++
#include <iostream>
using namespace std;
void fibo(int b,int a=0){
int x=0;
int y=1;
int z, x0;
while( x <= b ) {
z= x + y;
x0 = x;
x = y;
y = z;
if(x > a) {
cout << x0 << endl;
}
}
}
int main()
{
fibo(100);
return 0;
}
In your Python code, if there's no explicit return, the default return value of the function will be None
. In C++, that's equivalent to a void
function.
The for loop is designed to iterate a number of times. Its syntax is:
for (initialization; condition; increase) statement;
Like the while-loop, this loop repeats statement while condition is true. But, in addition, the for loop provides specific OPTIONAL locations to contain an initialization and an increase expression, executed before the loop begins the first time, and after each iteration, respectively.
Read more here: http://www.cplusplus.com/doc/tutorial/control/#for.
So let's break down your loop:
int x=0; // initialize x to 0
int y=1; // initialize y to 1
for(
int i=0; // initialize i to 0
i<=b; // keep looping until i is less than or equal to b (a variable passed in)
i++ // after every single loop iteration, increment i by 1
) {
int x0=x; // initialize x0 to x
int z=x+y; // initialize z to (x + y)
x=y; // assign the value of y to x
y=z; // assign the value of z to y
if(x>a){ // if x is greater than a, print the value of x0
cout << x0 <<endl;
}
}
In your Python code, you don't have an i
, you use x
as your loop invariant. So that should be the condition
of your for loop: x <= b
. The initialization part should be the variables you set before you loop, so: int x = 0, y = 1, x0, z
should be the initialization
. The last part is the increment. In your python code, your increment is x = y
, but in a for loop, that part is executed after the iteration is done, so we can't just set x = y
in the increment part of the for loop since y = z
is executed before the increment part. What we can do is use a bit of algebra: z = y + x
, so we can get the value of y
by subtracting x
from z
: z - x
.
This makes the for loop:
void fibo2(int b,int a=0){
for(
int x = 0, y = 1, x0, z;
x <= b;
x = (z-x)
) {
x0 = x;
z = x+y;
y = z;
if(x > a){
cout << x0 <<endl;
}
}
}
Hope this helps.
Upvotes: 3
Reputation: 1894
The usual mathematical (recursive) method to compute the sequence up to some limit
(this is not necessarily the best/most efficient method!): Link to demo here
#include <iostream>
using namespace std;
int fibo(int x)
{
if (x == 0) return 0;
if (x == 1) return 1;
return fibo(x-1)+fibo(x-2);
}
int main()
{
int j=1,limit=100;
do
{
cout<< fibo(j) <<'\t';
++j;
} while(fibo(j)<=limit);
return 0;
}
Upvotes: 1
Reputation: 153
Try this:
#include <iostream>
using namespace std;
int fibo(int b,int a=0){
int x=0;
int y=1;
while(x<=b)
{
int z=x+y;
int x0=x;
x=y;
y=z;
if(x>a && x<b)
{
cout << x << " ";
}
}
}
int main()
{
fibo(100);
return 0;
}
Upvotes: 1