Here is the code that I wrote, It is quite simple. Dose anyone have
and advice on fixing it.I am compiling it under Linux with gcc.
#include<iostream.h>
#include said:
int main(void) int main()
{
long fib1 = 1;
long fib2 = 1;
long fib3;
long fib4;
while (fib4 < 400000000)
{
fib3 = fib1 + fib2;
fib4 = fib3 + fib2;
fib1 = fib3;
fib2 = fib4;
cout << fib3 << endl;
cout << fib4 << endl;
fib3 = fib1 + fib2;
fib4 = fib3 + fib2;
fib1 = fib3;
fib2 = fib4;
std::cout << fib3 << std::endl;
std::cout << fib4 << std::endl;
}
return 0;
}
I would appriciate any advice or examples on how make the code do what
I want.
Lee
Lee,
I don't know exactly how much you know so my apologies if this is at
the wrong level for you. First three minor points:
1 Replace <iostream.h> with <iostream>. The ".h" headers are
non-standard and only for backward compatibility so new code should
avoid them. With the standard headers everything is declared in the
std namespace so you will need "std::cout" and so on.
2 int main(void) is unusual style, int main() is more usual. Using
void like that looks like C rather than C++.
3 You are not indentating the body of your while loop.
The major point is that you cannot fit anything into a long beyond the
maximum you have already reached. You will have to create your own
big integer class to hold larger numbers. Other people have already
made some suggestions as to how you might implement it.
Looking at your code you only do six things with your fib# variables:
you declare them, assign to them from a long, assign to them from each
other, compare them to 400000000, add them and output them. There is
also an implicit destruction when they go out of scope. Your big
integer class will have to do those six things and possibly have a
Destructor as well depending on the implementation you pick.
You will need to implement:
Constructor
Destructor [if needed]
operator= [assign from a long]
operator= [assign from a big integer]
operator+
operator<
operator<< [stream output, not a bit shift]
You will need to decide on the details of your chosen implementation
and provide at least the six or seven functions needed for the minimal
big integer class. Make sure you know how big the largest number you
want to hold is. You mentioned 10^50 which has 51 digits; Fib(5000)
has over 1000 digits.
Lastly, there is at least one extra function that you are going to
need for your class. It is not something that you actually do in your
code, but it is implied by what you have written. I am sure that you
will notice it once you start to try out your new class.
rossum