 CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com

1. Junior Member Join Date
Oct 2010
Posts
7

## Fibonacci Sequence

Can someone throw me a hint on how to only print the end result of an iteration? This is in regards to Case C in the program. I just copied and pasted the loop I have in Case B, but like I said I don't want to print the whole sequence, I only want to print the final result.

Code:
```#include <iostream>
using namespace std;

void PrintHistory();

int main ()
{
char choice;
int n = 1,
a = 1,
b = 0,
sum;

do
{
// Prompts users to enter a menu selection.
cout << "A. Facts about the Fibonacci Sequence." << endl;
cout << "B. Output the first N Fibonacci Numbers." << endl;
cout << "C. Output the Nth Fibonacci Number." << endl;
cout << "D. Mystery Number Sequence." << endl;
cout << "E. Quit" << endl;
cout << "Enter your choice: ";
cin >> choice;
cout << endl;

// Respond to the user's menu selection.
switch (choice)
{
case 'A':
case 'a':
PrintHistory();
cout << endl;
break;

case 'B':
case 'b':
cout << "How many numbers do you want to compute: ";
cin >> n;

for (int i = 1; i <= n; i++)
{
sum = a + b;
a = b;
b = sum;
cout << endl << sum << endl;
}
cout << endl;
break;

case 'C':
case 'c':
cout << "How many numbers do you want to compute: ";
cin >> n;

for (int i = 1; i <= n; i++)
{
sum = a + b;
a = b;
b = sum;
cout << sum << endl;
}
cout << endl;
break;

case 'D':
case 'd':
cout << "You entered D." << endl;
cout << endl;
break;

case 'E':
case 'e':
cout << "Quit." << endl;
cout << endl;
break;

default:
cout << "Please enter A, B, C, D, or E." << endl;
cout << endl;
break;

}
}

while (choice != 'E' && choice != 'e');
return 0;
}

void PrintHistory()
{
cout << "The Fibonacci Sequence is based on a recursive formula." << endl;
cout << "The subsequent number is the sum of the two values that precedes it." << endl;
cout << "The sequence is prominent in nature such as in the seashell, snails, sunflowers." << endl;
cout << endl;
}```  Reply With Quote

2. Elite Member Power Poster           Join Date
Apr 1999
Posts
27,449

## Re: Fibonacci Sequence Originally Posted by heminapatel Can someone throw me a hint on how to only print the end result of an iteration? This is in regards to Case C in the program. I just copied and pasted the loop I have in Case B, but like I said I don't want to print the whole sequence, I only want to print the final result.
Write a function that returns the Fibonacci number instead of trying to do everything in a case statement:
Code:
```#include <iostream>

using namespace std;

int Fibonacci( int n )
{
// returns the Fibnocacci(n)
}

int main()
{
cout << Fibonacci( 10 ) << "\n"; // for example
}```
I didn't write the code in the function that computes the Fibonacci number, since this looks like a homework problem.
Code:
```case 'B':
case 'b':
cout << "How many numbers do you want to compute: ";
cin >> n;
cout << "The Fibonnaci of " << n << " is " << Fibonacci( n ) << endl;
break;```
This is what your code would look like if you wrote the function and just called it.

Regards,

Paul McKenzie
Last edited by Paul McKenzie; October 24th, 2010 at 01:30 PM.  Reply With Quote

3. Junior Member Join Date
Oct 2010
Posts
7

## Re: Fibonacci Sequence

I think I got it I fixed it to be this, but now my issue is I'm asking for it to return the sum for Case B but it's duplicating the last entry i.e. when I put in 5 I get 1, 1, 2, 3, 5, 5. I tried many different returns like 0, n, i, etc. Is there something I can return without getting an actual value?

Code:
```#include <iostream>
using namespace std;

void PrintHistory();
int Fibonacci(int n);
int FibonacciN(int n);

int main ()
{
char choice;
int n = 1,
a = 1,
b = 0,
sum;

do
{
// Prompts users to enter a menu selection.
cout << "A. Facts about the Fibonacci Sequence." << endl;
cout << "B. Output the first N Fibonacci Numbers." << endl;
cout << "C. Output the Nth Fibonacci Number." << endl;
cout << "D. Mystery Number Sequence." << endl;
cout << "E. Quit" << endl;
cout << "Enter your choice: ";
cin >> choice;
cout << endl;

// Respond to the user's menu selection.
switch (choice)
{
case 'A':
case 'a':
PrintHistory();
cout << endl;
break;

case 'B':
case 'b':
cout << "How many numbers do you want to compute: ";
cin >> n;
cout << endl << Fibonacci(n) << endl;
cout << endl;
break;

case 'C':
case 'c':
cout << "How many numbers do you want to compute: ";
cin >> n;
cout << endl << FibonacciN(n) << endl;
cout << endl;
break;

case 'D':
case 'd':
cout << "You entered D." << endl;
cout << endl;
break;

case 'E':
case 'e':
cout << "Quit." << endl;
cout << endl;
break;

default:
cout << "Please enter A, B, C, D, or E." << endl;
cout << endl;
break;

}
}

while (choice != 'E' && choice != 'e');
return 0;
}

void PrintHistory()
{
cout << "The Fibonacci Sequence is based on a recursive formula." << endl;
cout << "The subsequent number is the sum of the two values that precedes it." << endl;
cout << "The sequence is prominent in nature such as in the seashell, snails, sunflowers." << endl;
}

int Fibonacci (int n)
{
int a = 1,
b = 0,
sum;

for (int i = 1; i <= n; i++)
{
sum = 0;
sum = a + b;
a = b;
b = sum;
cout << endl << sum << endl;
}

return sum;
}

int FibonacciN (int n)
{
int a = 1,
b = 0,
sum;

for (int i = 1; i <= n; i++)
{
sum = 0;
sum = a + b;
a = b;
b = sum;
}

return sum;
}```  Reply With Quote

4. Elite Member Power Poster           Join Date
Apr 1999
Posts
27,449

## Re: Fibonacci Sequence Originally Posted by heminapatel I think I got it I fixed it to be this, but now my issue is I'm asking for it to return the sum for Case B but it's duplicating the last entry i.e. when I put in 5 I get 1, 1, 2, 3, 5, 5.
The Fibonacci functions should not be showing output. Their only purpose is to calculate a number, and return to you that number.

When you call sqrt(), pow(), cos(), tan(), etc. you don't see output for those functions -- you get a return value that represents the quantity you're trying to compute. The same thing with the Fibonacci function you're writing.

Regards,

Paul McKenzie  Reply With Quote

5. Elite Member Power Poster           Join Date
Apr 1999
Posts
27,449

## Re: Fibonacci Sequence

Let's make this simple:
Code:
```int main()
{
cout << "The Fibonacci of 10 is " << Fibonacci(10);
}```
Something as simple as this. Then test with various numbers to make sure that the Fibonacci function actually works correctly. All of that other stuff with menus is just there to initially confuse you. You worry about that after you've coded a working Fibonacci function.

Many new programmers get tripped up by writing fancy menus and output first, and then they have hardly any time left to work on the stuff that's really important -- in your case, whether you can write a Fibonacci function or not.

Regards,

Paul McKenzie
Last edited by Paul McKenzie; October 24th, 2010 at 04:11 PM.  Reply With Quote

6. Junior Member Join Date
Oct 2010
Posts
7

## Re: Fibonacci Sequence

My function is working, but thanks I think I understand what you're trying to say

Code:
```int Fibonacci (int n)
{
int a = 1,
b = 0,
sum;

for (int i = 1; i <= n; i++)
{
sum = 0;
sum = a + b;
a = b;
b = sum;
cout << endl << sum << endl;
}

return sum;
}```  Reply With Quote

7. Elite Member Power Poster           Join Date
Apr 1999
Posts
27,449

## Re: Fibonacci Sequence

You are setting sum to 0 each time the loop iterates. That is not what you should be doing. The sum should be initialized to 0 outside the loop, not within the loop.

Regards,

Paul McKenzie  Reply With Quote

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
• 