By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,445 Members | 1,329 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,445 IT Pros & Developers. It's quick & easy.

Fibonacci Numbers and Lucas Numbers

P: n/a
I'm having some problems with the below equation.

I have no problems when it comes to positives. Negatives create the
problem..

C
2 1
4

However, this doesn't work:

C
-60 37
-5

Any help? I appreciate it greatly!!!
#include <iostream>
#include <cstdlib>
#include <cmath>
using namespace std;

bool isFibonacciNumber (int);
unsigned findFibonacciNumber (int);
bool isLucasNumber (int);
unsigned findLucasNumber (int);
bool isInSequence (int, int, int);
unsigned findInSequence (int, int, int);

int main()
{
char choice;
int n, i;
int var1;
int var2;

cout <<
"************************************************* *********************"
<< endl;
cout << "* CGS2425 C++ Project 3 - Functions and Control
Structures *" << endl;
cout << "* Fibonacci Numbers and Lucas
Numbers *" << endl;
cout <<
"************************************************* *********************"
<< endl;
do{
cout <<
"************************************************* *********************"
<< endl;
cout << "* A - Find Fibonacci
Number *" << endl;
cout << "* B - Find Lucas
Number *" << endl;
cout << "* C - Find Term in a Similar
Sequence *" << endl;
cout << "* Q -
Quit *" <<
endl;
cout <<
"************************************************* *********************"
<< endl;
cout << " Enter menu selection : ";
cin >choice;
cout << endl;
if(choice == 'A'|| choice == 'a'){
cout << "Enter integer : ";
cin >n;
if(isFibonacciNumber (n)){
unsigned termNum = findFibonacciNumber(n);
cout << n << " is term " << termNum << " in the
Fibonacci sequence." << endl;
cout << endl;
}
else{
cout << n << " is not a Fibonacci number." <<
endl;
cout << endl;
}

}
else if(choice == 'B' || choice == 'b'){

cout << "Enter integer : ";
cin >n;
if(isLucasNumber (n)){
unsigned termNum = findLucasNumber (n);
cout << n << " is term " << termNum << " in the Lucas
sequence." << endl;
cout << endl;

}
else{
cout << n << " is not a Lucas number." << endl;
cout << endl;
}
}

else if(choice == 'C' || choice == 'c'){

int var1, var2;
cout << "Enter the first 2 terms of the integer sequence,
separated by a space : ";
cin >var1 >var2;
cout << endl;
cout << "Enter integer : ";
cin >n;

if(isInSequence (var1, var2, n)){
unsigned termNum = findInSequence (var1,var2,n);
cout << n << " is term " << termNum << " in the sequence
with seeds " << var1 << ' ' << var2 << endl;
cout << endl;
}
else{
cout << endl << n << " is not a term in the sequence with
seeds " << var1 << ' ' << var2 << endl;
cout << endl;
}
}
else if(choice != 'q' && choice != 'Q'){
cout << " ERROR: Invalid choice. Please enter A, B, C, or Q."
<< endl;
}
}while(choice != 'Q' && choice != 'q');
return(0);
}
bool isFibonacciNumber (int n){

//bool goat = true;
if(n == 0){
return(true);
}

else if(n == 1){
return(true);
}

else if(n>=2)
{
int i=1;
cout << endl;
int var1 = 0, var2 = 1;
int fib=0;
while(n >= fib){

fib = var1 + var2;
var1 = var2;
var2 = fib;
i++;
if(n == fib){
return(true);
}
}
}
return(false);

}

unsigned findFibonacciNumber(int n)
{
unsigned termNum;
if(n == 0){
termNum = 0;
}

else if(n == 1){
termNum = 1;
}

else if(n>=2)
{
int i=1;
cout << endl;
int var1 = 0, var2 = 1;
int fib;
while(n >= fib){

fib = var1 + var2;
var1 = var2;
var2 = fib;
i++;

}
if(n == fib){
termNum = i;
}

return termNum;

}
}

bool isLucasNumber (int n){

//bool horse = true;
if(n == 2){
return(true);
}

else if(n == 1){
return(true);
}

else if(n>=2)
{

int i=1;
cout << endl;
int var1 = 2, var2 = 1;
int lucas=0;
while(n >= lucas){
lucas = var1 + var2;
var1 = var2;
var2 = lucas;
i++;
if(n == lucas){
return(true);
}
}

}
return(false);
}

unsigned findLucasNumber (int n){
unsigned termNum;
if(n == 2){
termNum = 0;
}

else if(n == 1){
termNum = 1;
}

else if(n>=2)
{
int i=1;
cout << endl;
int var1 = 2, var2 = 1;
int lucas;
while(n >= lucas){
lucas = var1 + var2;
var1 = var2;
var2 = lucas;
i++;

}

if(n == lucas){
termNum = i;
}
return termNum;
}
}

bool isInSequence (int var1, int var2, int n)
{

//bool cow = true;

if(n == var1){
return(true);
}
else if(n == var2){
return(true);
}
else if(n>=2)
{
int i=1;
cout << endl;
int seq=0;
while(n >= seq){
seq = var1 + var2;
var1 = var2;
var2 = seq;
i++;
if(n == seq){
return(true);
}
}
}

if(var1<0 && var2>0){
if(.5*var1<= fabs(var2)){
int i=1;
int seq = 0;
while(n>=seq){
seq = var1 + var2;
var1 = var2;
var2 = seq;
i++;
if(n == seq){
return(true);
}
}
}
}
else if(var1>0 && var2<0){
if(.5*var2<=fabs(var1)){
int i=1;
int seq = 0;
while(n<=seq){
seq = var1 + var2;
var1 = var2;
var2 = seq;
i++;
if(n == seq){
return(true);
}
}
}
}

return(false);
}

unsigned findInSequence (int var1, int var2, int n){

unsigned termNum;
if(n ==var1){
termNum = 0;
}
else if(n == var2){
termNum = 1;
}

if(n>=2)
{
int i=1;
cout << endl;
int seq=0;
while(n >= seq){
seq = var1 + var2;
var1 = var2;
var2 = seq;
i++;
}
if(n == seq){
termNum = i;
}
return termNum;
}
if(var1<0 && var2>0){
if(.5*var1<= fabs(var2)){
int i=1;
int seq = 1;
while(n>=seq){
seq = var1 + var2;
var1 = var2;
var2 = seq;
i++;
if(n == seq){
return(true);
}
}
if(n == seq){
termNum = i;
}
return termNum;
}
}
else if(var1>0 && var2<0){
if(.5*var2<=fabs(var1)){
int i=1;
int seq = 1;
while(n<=seq){
seq = var1 + var2;
var1 = var2;
var2 = seq;
i++;
if(n == seq){
return(true);
}
}
if(n == seq){
termNum = i;
}
return termNum;
}
}
}

May 25 '07 #1
Share this Question
Share on Google+
6 Replies


P: n/a
Andrew Tatum wrote:
I'm having some problems with the below equation.
So instead of trying to work out on the problem yourself you copypaste
the overly long code here and hope that someone will go through it for
you?

People will probably be more eager to help you if you show that you
have done something yourself towards finding the answers to your
questions. One thing which shows this is that you post a minimal
(but working) piece of code which is the core of the problem and is
the part you don't understand. Copypasting the entire lengthy code,
with no attempt at isolating the problem, shows just laziness.
May 26 '07 #2

P: n/a
I'm sorry. I wasn't trying to show laziness...

To be honest, I know more PHP/ASP than anything else.

My friend is working on this C++ project and was looking for my help.
I looked over it and don't see anything that sticks out in my mind.

So, I figured I would see if someone here with a TON more knowledge
and background in C++ could help me out.

I'm hoping you respect my honesty...

May 26 '07 #3

P: n/a
On Fri, 25 May 2007 16:16:38 -0700, Andrew Tatum wrote:
unsigned findLucasNumber (int n){
unsigned termNum;
if(n == 2){
termNum = 0;
}

else if(n == 1){
termNum = 1;
}

else if(n>=2)
{
int i=1;
cout << endl;
int var1 = 2, var2 = 1;
int lucas;
Uninitialized variable.
while(n >= lucas){
lucas = var1 + var2;
var1 = var2;
var2 = lucas;
i++;

}

if(n == lucas){
termNum = i;
}
return termNum;
}
}
--
Markus Schoder
May 26 '07 #4

P: n/a
I appreciate the help Markus! However, I believe the problem lies
within the unsigned FindInSequence... particularly the last part.

unsigned findInSequence (int var1, int var2, int n){

unsigned termNum;
if(n ==var1){
termNum = 0;
}
else if(n == var2){
termNum = 1;
}

if(n>=2)
{
int i=1;
cout << endl;
int seq=0;
while(n >= seq){
seq = var1 + var2;
var1 = var2;
var2 = seq;
i++;
}
if(n == seq){
termNum = i;
}
return termNum;
}

if(var1<0 && var2>0){
if(.5*var1<= fabs(var2)){
int i=1;
int seq = 1;
while(n>=seq){
seq = var1 + var2;
var1 = var2;
var2 = seq;
i++;
if(n == seq){
return(true);
}
}
if(n == seq){
termNum = i;
}
return termNum;
}
}
else if(var1>0 && var2<0){
if(.5*var2<=fabs(var1)){
int i=1;
int seq = 1;
while(n<=seq){
seq = var1 + var2;
var1 = var2;
var2 = seq;
i++;
if(n == seq){
return(true);
}
}
if(n == seq){
termNum = i;
}
return termNum;
}
}

May 26 '07 #5

P: n/a
These are the errors I'm getting:

1>------ Build started: Project: fibonacci, Configuration: Debug Win32
------
1>Compiling...
1>fib.cpp
1>.\fib.cpp(260) : error C2668: 'fabs' : ambiguous call to overloaded
function
1 C:\Program Files\Microsoft Visual Studio 8\VC\include
\math.h(557): could be 'long double fabs(long double)'
1 C:\Program Files\Microsoft Visual Studio 8\VC\include
\math.h(509): or 'float fabs(float)'
1 C:\Program Files\Microsoft Visual Studio 8\VC\include
\math.h(119): or 'double fabs(double)'
1 while trying to match the argument list '(int)'
1>.\fib.cpp(276) : error C2668: 'fabs' : ambiguous call to overloaded
function
1 C:\Program Files\Microsoft Visual Studio 8\VC\include
\math.h(557): could be 'long double fabs(long double)'
1 C:\Program Files\Microsoft Visual Studio 8\VC\include
\math.h(509): or 'float fabs(float)'
1 C:\Program Files\Microsoft Visual Studio 8\VC\include
\math.h(119): or 'double fabs(double)'
1 while trying to match the argument list '(int)'
1>.\fib.cpp(324) : error C2668: 'fabs' : ambiguous call to overloaded
function
1 C:\Program Files\Microsoft Visual Studio 8\VC\include
\math.h(557): could be 'long double fabs(long double)'
1 C:\Program Files\Microsoft Visual Studio 8\VC\include
\math.h(509): or 'float fabs(float)'
1 C:\Program Files\Microsoft Visual Studio 8\VC\include
\math.h(119): or 'double fabs(double)'
1 while trying to match the argument list '(int)'
1>.\fib.cpp(343) : error C2668: 'fabs' : ambiguous call to overloaded
function
1 C:\Program Files\Microsoft Visual Studio 8\VC\include
\math.h(557): could be 'long double fabs(long double)'
1 C:\Program Files\Microsoft Visual Studio 8\VC\include
\math.h(509): or 'float fabs(float)'
1 C:\Program Files\Microsoft Visual Studio 8\VC\include
\math.h(119): or 'double fabs(double)'
1 while trying to match the argument list '(int)'
1>Build log was saved at "file://c:\Users\Andrew\Documents\Visual
Studio 2005\Projects\fibonacci\fibonacci\Debug\BuildLog.h tm"
1>fibonacci - 4 error(s), 0 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped
==========

May 26 '07 #6

P: n/a

"Andrew Tatum" <an**********@gmail.comwrote in message
news:11**********************@w5g2000hsg.googlegro ups.com...
These are the errors I'm getting:

1>------ Build started: Project: fibonacci, Configuration: Debug Win32
------
1>Compiling...
1>fib.cpp
1>.\fib.cpp(260) : error C2668: 'fabs' : ambiguous call to overloaded
function
1 C:\Program Files\Microsoft Visual Studio 8\VC\include
\math.h(557): could be 'long double fabs(long double)'
1 C:\Program Files\Microsoft Visual Studio 8\VC\include
\math.h(509): or 'float fabs(float)'
1 C:\Program Files\Microsoft Visual Studio 8\VC\include
\math.h(119): or 'double fabs(double)'
1 while trying to match the argument list '(int)'
Ok, so what do those tell you?

The compiler doesn't know which version of fabs to use, because you're
passing an int, not a float. The fabs function expects a floating-point
value (thus, the preceding 'f' in the name). Either use abs(), or pass a
floating-point value (such as by using "1.0 * var1"), or cast the parameter
to a float (or double).

-Howard
May 27 '07 #7

This discussion thread is closed

Replies have been disabled for this discussion.