473,408 Members | 2,839 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,408 software developers and data experts.

Fibonacci Numbers and Lucas Numbers

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
6 4945
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
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
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
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
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

"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

28
by: dleecurt | last post by:
Hello, I have a small problem, I am trying to write a program that will calculate the Fibonacci number series, and I have the code complete with one problem. I used a long in to store the numbers,...
0
by: Alex Vinokur | last post by:
An algorithm which computes very long Fibonacci numbers http://groups.google.com/groups?selm=bnni5p%2412i47o%241%40ID-79865.news.uni-berlin.de was used as a performance testsuite to compare speed...
5
by: Niks | last post by:
Can anybody explain me what is a "Fibonacci search"? even an URL will do. Thanks for reading.
4
by: YS Sze | last post by:
If you know the exact longitude and latitude for a specific location, would anyone think it'd make any sense to find out if this set of location numbers is really part of the Fibonacci series or...
62
by: jugaaru | last post by:
How to generate fibonacci mubers in C ?
8
by: srinpraveen | last post by:
I know to write a program to print the fibonacci series. But the problem is my teacher has asked us to write a program to print the natural numbers that are not involved in the fibonacci series. For...
13
by: mac | last post by:
Hi, I'm trying to write a fibonacci recursive function that will return the fibonacci string separated by comma. The problem sounds like this: ------------- Write a recursive function that...
8
by: help2008 | last post by:
Hi I have been doing this working on an assignment for the last week and have stumbled across a part which I cant get my head around. I was hoping that someone could explain what I am missing. I...
1
by: altaey | last post by:
Question Details: Write a program to find and print a Fibonacci sequence of numbers. The Fibonacci sequence is defined as follow: Fn = Fn-2 + Fn-1, n >= 0 F0 = 0, F1 = 1, F2 = 1 Your...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.