A
Andrew Tatum
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;
}
}
}
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;
}
}
}