### Thread: Could someone help me out?

1. No Profile Picture
Registered User
Devshed Newbie (0 - 499 posts)

Join Date
Nov 2012
Posts
16
Rep Power
0

#### Could someone help me out?

I'm working on a assignment,

The assignments is:

Write a program that computes the first 40 numbers in the fibonacci sequence (store them in an array) then presents the user with the following menu:

Your options:
0. Display the first forty numbers of the fibonacci sequence
1. Display only the first number
2. Display only the second number
3. Display only the third number
...
40. Display only the fortieth number
Your choice:

If the user chooses 0, display the whole forty numbers in the array, each on its own line. If the user chooses a number from 1 to 40, display that number. If the user chooses a negative number or a number greater than 40 inform them that they have entered an invalid number.

After taking the user's choice, ask them if they would like to make another choice, displaying this menu:

Would you like to make another choice? (0 - No, 1 - Yes):

If they choose 0, exit the program. If they choose 1, present the original menu again.

Code:
```What I have so far is:

/*
Program: Fibonacci Sequence
Author: Jared Benedict
Date: 2013-01-29
*/

#include <iostream>
#include <iomanip>
#include <cstring>
#include <string>
#include <cmath>

using namespace std;
int main()
{
// Declare Variables
int n[40] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
11, 12, 13, 14, 15, 16, 17, 18, 19, 20,
21, 22, 23, 24, 25, 26, 27, 28, 29, 30,
31, 32, 33, 34, 35, 36, 37, 38, 39, 40};
int first = 0;
int second = 1;
int next;
int menuChoice;

for ( int index = 0 ; index < 40 ; ++index )
{
if ( index <= 1 )
next = index;
else
{
next = first + second;
first = second;
second = next;
}
//cout << next << endl; This prints all numbers
}

cout << "Your options:" << endl;
cout << "0. Display the first forty numbers of the fibonacci sequence." << endl;
cout << "1. Display only the first number." << endl;
cout << "2. Display only the second number." << endl;
cout << "3. Display only the third number." << endl;
cout << "..." << endl;
cout << "40. Display only the fortieth number." << endl;
cout << "Your choice: ";
cin >> menuChoice;

if (menuChoice == 0)
{

}
}```
2. I'm unsure what your array n is about. If you were to need sequential integers the computer is a better tool for creating them than entering them by hand.

Whereas the code computes and discards Fibonacci numbers, it should store them. The array n is the right size but could have a better name.

After you get a menuChoice the code should verify the value is reasonable.

Then the code should take appropriate action.
3. No Profile Picture
Still Learning
Devshed Newbie (0 - 499 posts)

Join Date
Dec 2012
Location
Montreal, Canada
Posts
55
Rep Power
40
As my father used to say you are mixing kasha and borscht.

You have the array to store the 40 numbers in but you don't use it at all.
The loop you have does calculate the fib sequence but tosses them away.
Each time through the loop you be saving the new number in the correct spot in the array.

You need to step back and understand the algorithm you are using.
Say it in words first then update to code you have. It's not far from usefull code but it shouts that you do understand what you are trying to do.

Look other implementations of generating fib number to compare.
Last edited by admiraln; January 30th, 2013 at 11:28 PM.
4. No Profile Picture
Registered User
Devshed Newbie (0 - 499 posts)

Join Date
Jan 2013
Posts
2
Rep Power
0
You have the array to store the 40 numbers in but you don't use it at all.
The loop you have does calculate the fib sequence but tosses them away.
Each time through the loop you be saving the new number in the correct spot in the array.

That seems craazy. Anyone has other ideas?
5. No Profile Picture
Registered User
Devshed Newbie (0 - 499 posts)

Join Date
Nov 2012
Posts
16
Rep Power
0

#### This is my final code.

I finished my program.
This is it:

Code:
```/*
Program: Fibonacci Sequence
Author: Jared Benedict
Date: 2013-01-31

This program will calculate the first ninty numbers of the Fibonacci Sequence.
*/

#include <iostream>
#include <iomanip>
#include <cstring>
#include <string>
#include <cmath>

using namespace std;

int main()
{
//Declare variables
int lengthOfSequence = 90;
long long fibNumber[90];
int userChoice;
int number;
int answer;

fibNumber[0] = 0;
fibNumber[1] = 1;

// Fibonacci sequence
for (int index = 2; index < lengthOfSequence; index++)
{
fibNumber[index] = fibNumber[index - 2] + fibNumber[index-1];
}

do{
do{
cout << endl << (char) 201 << (char)205 << (char)205 << (char)205 << (char)205
<< (char)205 << (char)205 << (char)205 << (char)205 << (char)205 << (char)205
<< (char)205 << (char)205 << (char)205 << (char)205 << (char)205 << (char)205
<< (char)205 << (char)205 << (char)205 << (char)205 << (char)205 << (char)205
<< (char)205 << (char)205 << (char)205 << (char)205 << (char)205 << (char)205
<< (char)205 << (char)205 << (char)205 << (char)205 << (char)205 << (char)205
<< (char)205 << (char)205 << (char)205 << (char)205 << (char)205 << (char)205
<< (char)205 << (char)205 << (char)205 << (char)205 << (char)205 << (char)205
<< (char)205 << (char)205 << (char)205 << (char)205 << (char)205 << (char)205
<< (char)205 << (char)205 << (char)205 << (char)205 << (char)205 << (char)205
<< (char)205 << (char)205 << (char)205 << (char) 187 << endl;
cout << " Your options: " << endl
<< " 0. Display the first ninety numbers of the fibonacci sequence. " << endl
<<	" 1. Display only the first number. " << endl
<< " 2. Display only the second number. " << endl
<< " 3. Display only the third number. " << endl
<< " ... " << endl
<< " 90. Display only the ninetieth number. " << endl
<< " Your choice: ";
cin >> number;

}while(number < 0 and number > 91);

if (number == 0)
{
cout << endl << (char) 201 << (char)205 << (char)205 << (char)205 << (char)205
<< (char)205 << (char)205 << (char)205 << (char)205 << (char)205 << (char)205
<< (char)205 << (char)205 << (char)205 << (char)205 << (char)205 << (char)205
<< (char)205 << (char)205 << (char)205 << (char)205 << (char)205 << (char)205
<< (char)205 << (char)205 << (char)205 << (char)205 << (char)205 << (char)205
<< (char)205 << (char)205 << (char)205 << (char)205 << (char)205 << (char)205
<< (char)205 << (char)205 << (char)205 << (char)205 << (char)205 << (char)205
<< (char)205 << (char)205 << (char)205 << (char)205 << (char)205 << (char)205
<< (char)205 << (char)205 << (char)205 << (char)205 << (char)205 << (char)205
<< (char)205 << (char)205 << (char)205 << (char)205 << (char)205 << (char)205
<< (char)205 << (char)205 << (char)205 << (char) 187 << endl;
for (int index = 0; index < lengthOfSequence; index++)
{
cout << " " << fibNumber[index] << endl;
}
}

else if (number > 90 or number < 0)
{
cout << endl << (char) 201 << (char)205 << (char)205 << (char)205 << (char)205
<< (char)205 << (char)205 << (char)205 << (char)205 << (char)205 << (char)205
<< (char)205 << (char)205 << (char)205 << (char)205 << (char)205 << (char)205
<< (char)205 << (char)205 << (char)205 << (char)205 << (char)205 << (char)205
<< (char)205 << (char)205 << (char)205 << (char)205 << (char)205 << (char)205
<< (char)205 << (char)205 << (char)205 << (char)205 << (char)205 << (char)205
<< (char)205 << (char)205 << (char)205 << (char)205 << (char)205 << (char)205
<< (char)205 << (char)205 << (char)205 << (char)205 << (char)205 << (char)205
<< (char)205 << (char)205 << (char)205 << (char)205 << (char)205 << (char)205
<< (char)205 << (char)205 << (char)205 << (char)205 << (char)205 << (char)205
<< (char)205 << (char)205 << (char)205 << (char) 187 << endl;
cout << " " << number << " is an invalid number." << endl;
}

else
{
cout << endl << (char) 201 << (char)205 << (char)205 << (char)205 << (char)205
<< (char)205 << (char)205 << (char)205 << (char)205 << (char)205 << (char)205
<< (char)205 << (char)205 << (char)205 << (char)205 << (char)205 << (char)205
<< (char)205 << (char)205 << (char)205 << (char)205 << (char)205 << (char)205
<< (char)205 << (char)205 << (char)205 << (char)205 << (char)205 << (char)205
<< (char)205 << (char)205 << (char)205 << (char)205 << (char)205 << (char)205
<< (char)205 << (char)205 << (char)205 << (char)205 << (char)205 << (char)205
<< (char)205 << (char)205 << (char)205 << (char)205 << (char)205 << (char)205
<< (char)205 << (char)205 << (char)205 << (char)205 << (char)205 << (char)205
<< (char)205 << (char)205 << (char)205 << (char)205 << (char)205 << (char)205
<< (char)205 << (char)205 << (char)205 << (char) 187 << endl;
cout << " The " << number << " fibonacci number is: " << fibNumber[number - 1] << endl;
}

do
{
cout << endl << (char) 201 << (char)205 << (char)205 << (char)205 << (char)205
<< (char)205 << (char)205 << (char)205 << (char)205 << (char)205 << (char)205
<< (char)205 << (char)205 << (char)205 << (char)205 << (char)205 << (char)205
<< (char)205 << (char)205 << (char)205 << (char)205 << (char)205 << (char)205
<< (char)205 << (char)205 << (char)205 << (char)205 << (char)205 << (char)205
<< (char)205 << (char)205 << (char)205 << (char)205 << (char)205 << (char)205
<< (char)205 << (char)205 << (char)205 << (char)205 << (char)205 << (char)205
<< (char)205 << (char)205 << (char)205 << (char)205 << (char)205 << (char)205
<< (char)205 << (char)205 << (char)205 << (char)205 << (char)205 << (char)205
<< (char)205 << (char)205 << (char)205 << (char)205 << (char)205 << (char)205
<< (char)205 << (char)205 << (char)205 << (char) 187 << endl;
cout << " Would you like to make another choice? (0 - No, 1 - Yes)" << endl
<< " Your choice: ";
cin >> answer;
}while(answer != 0 and answer != 1);

}while(answer != 0);
}```
6. No Profile Picture
Registered User
Devshed Newbie (0 - 499 posts)

Join Date
Jan 2013
Posts
2
Rep Power
0
Originally Posted by blobman23
I finished my program.
This is it:

Code:
```/*
Program: Fibonacci Sequence
Author: Jared Benedict
Date: 2013-01-31

This program will calculate the first ninty numbers of the Fibonacci Sequence.
*/

#include <iostream>
#include <iomanip>
#include <cstring>
#include <string>
#include <cmath>

using namespace std;

int main()
{
//Declare variables
int lengthOfSequence = 90;
long long fibNumber[90];
int userChoice;
int number;
int answer;

fibNumber[0] = 0;
fibNumber[1] = 1;

// Fibonacci sequence
for (int index = 2; index < lengthOfSequence; index++)
{
fibNumber[index] = fibNumber[index - 2] + fibNumber[index-1];
}

do{
do{
cout << endl << (char) 201 << (char)205 << (char)205 << (char)205 << (char)205
<< (char)205 << (char)205 << (char)205 << (char)205 << (char)205 << (char)205
<< (char)205 << (char)205 << (char)205 << (char)205 << (char)205 << (char)205
<< (char)205 << (char)205 << (char)205 << (char)205 << (char)205 << (char)205
<< (char)205 << (char)205 << (char)205 << (char)205 << (char)205 << (char)205
<< (char)205 << (char)205 << (char)205 << (char)205 << (char)205 << (char)205
<< (char)205 << (char)205 << (char)205 << (char)205 << (char)205 << (char)205
<< (char)205 << (char)205 << (char)205 << (char)205 << (char)205 << (char)205
<< (char)205 << (char)205 << (char)205 << (char)205 << (char)205 << (char)205
<< (char)205 << (char)205 << (char)205 << (char)205 << (char)205 << (char)205
<< (char)205 << (char)205 << (char)205 << (char) 187 << endl;
cout << " Your options: " << endl
<< " 0. Display the first ninety numbers of the fibonacci sequence. " << endl
<<	" 1. Display only the first number. " << endl
<< " 2. Display only the second number. " << endl
<< " 3. Display only the third number. " << endl
<< " ... " << endl
<< " 90. Display only the ninetieth number. " << endl
<< " Your choice: ";
cin >> number;

}while(number < 0 and number > 91);

if (number == 0)
{
cout << endl << (char) 201 << (char)205 << (char)205 << (char)205 << (char)205
<< (char)205 << (char)205 << (char)205 << (char)205 << (char)205 << (char)205
<< (char)205 << (char)205 << (char)205 << (char)205 << (char)205 << (char)205
<< (char)205 << (char)205 << (char)205 << (char)205 << (char)205 << (char)205
<< (char)205 << (char)205 << (char)205 << (char)205 << (char)205 << (char)205
<< (char)205 << (char)205 << (char)205 << (char)205 << (char)205 << (char)205
<< (char)205 << (char)205 << (char)205 << (char)205 << (char)205 << (char)205
<< (char)205 << (char)205 << (char)205 << (char)205 << (char)205 << (char)205
<< (char)205 << (char)205 << (char)205 << (char)205 << (char)205 << (char)205
<< (char)205 << (char)205 << (char)205 << (char)205 << (char)205 << (char)205
<< (char)205 << (char)205 << (char)205 << (char) 187 << endl;
for (int index = 0; index < lengthOfSequence; index++)
{
cout << " " << fibNumber[index] << endl;
}
}

else if (number > 90 or number < 0)
{
cout << endl << (char) 201 << (char)205 << (char)205 << (char)205 << (char)205
<< (char)205 << (char)205 << (char)205 << (char)205 << (char)205 << (char)205
<< (char)205 << (char)205 << (char)205 << (char)205 << (char)205 << (char)205
<< (char)205 << (char)205 << (char)205 << (char)205 << (char)205 << (char)205
<< (char)205 << (char)205 << (char)205 << (char)205 << (char)205 << (char)205
<< (char)205 << (char)205 << (char)205 << (char)205 << (char)205 << (char)205
<< (char)205 << (char)205 << (char)205 << (char)205 << (char)205 << (char)205
<< (char)205 << (char)205 << (char)205 << (char)205 << (char)205 << (char)205
<< (char)205 << (char)205 << (char)205 << (char)205 << (char)205 << (char)205
<< (char)205 << (char)205 << (char)205 << (char)205 << (char)205 << (char)205
<< (char)205 << (char)205 << (char)205 << (char) 187 << endl;
cout << " " << number << " is an invalid number." << endl;
}

else
{
cout << endl << (char) 201 << (char)205 << (char)205 << (char)205 << (char)205
<< (char)205 << (char)205 << (char)205 << (char)205 << (char)205 << (char)205
<< (char)205 << (char)205 << (char)205 << (char)205 << (char)205 << (char)205
<< (char)205 << (char)205 << (char)205 << (char)205 << (char)205 << (char)205
<< (char)205 << (char)205 << (char)205 << (char)205 << (char)205 << (char)205
<< (char)205 << (char)205 << (char)205 << (char)205 << (char)205 << (char)205
<< (char)205 << (char)205 << (char)205 << (char)205 << (char)205 << (char)205
<< (char)205 << (char)205 << (char)205 << (char)205 << (char)205 << (char)205
<< (char)205 << (char)205 << (char)205 << (char)205 << (char)205 << (char)205
<< (char)205 << (char)205 << (char)205 << (char)205 << (char)205 << (char)205
<< (char)205 << (char)205 << (char)205 << (char) 187 << endl;
cout << " The " << number << " fibonacci number is: " << fibNumber[number - 1] << endl;
}

do
{
cout << endl << (char) 201 << (char)205 << (char)205 << (char)205 << (char)205
<< (char)205 << (char)205 << (char)205 << (char)205 << (char)205 << (char)205
<< (char)205 << (char)205 << (char)205 << (char)205 << (char)205 << (char)205
<< (char)205 << (char)205 << (char)205 << (char)205 << (char)205 << (char)205
<< (char)205 << (char)205 << (char)205 << (char)205 << (char)205 << (char)205
<< (char)205 << (char)205 << (char)205 << (char)205 << (char)205 << (char)205
<< (char)205 << (char)205 << (char)205 << (char)205 << (char)205 << (char)205
<< (char)205 << (char)205 << (char)205 << (char)205 << (char)205 << (char)205
<< (char)205 << (char)205 << (char)205 << (char)205 << (char)205 << (char)205
<< (char)205 << (char)205 << (char)205 << (char)205 << (char)205 << (char)205
<< (char)205 << (char)205 << (char)205 << (char) 187 << endl;
cout << " Would you like to make another choice? (0 - No, 1 - Yes)" << endl
<< " Your choice: ";
cin >> answer;
}while(answer != 0 and answer != 1);

}while(answer != 0);
}```
thanx buddy
7. Did you check all your results all the way up to 90?
list of numbers
8. No Profile Picture
Registered User
Devshed Newbie (0 - 499 posts)

Join Date
Nov 2012
Posts
16
Rep Power
0
Originally Posted by salem
Did you check all your results all the way up to 90?
list of numbers
Yes I did, and everything appeared to check out. However, there was a mistake and corrected it, I'm not sure if that was before or after I posted the code on here though. :o