Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support |

#1
January 30th, 2013, 10:54 AM
 blobman23
Registered User

Join Date: Nov 2012
Posts: 16
Time spent in forums: 2 h 41 m 15 sec
Reputation 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:

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

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;

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;

{

}
}```

#2
January 30th, 2013, 04:39 PM
 b49P23TIvg
Contributing User

Join Date: Aug 2011
Posts: 3,352
Time spent in forums: 1 Month 2 Weeks 3 Days 7 h 49 m 23 sec
Reputation Power: 383
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.
__________________
[code]Code tags[/code] are essential for python code!

#3
January 30th, 2013, 06:07 PM
Still Learning

Join Date: Dec 2012
Posts: 55
Time spent in forums: 18 h 57 m 51 sec
Reputation Power: 38
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
January 30th, 2013, 11:42 PM
 elrohir
Registered User

Join Date: Jan 2013
Posts: 2
Time spent in forums: 8 m 26 sec
Reputation 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
January 31st, 2013, 10:53 PM
 blobman23
Registered User

Join Date: Nov 2012
Posts: 16
Time spent in forums: 2 h 41 m 15 sec
Reputation 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;

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

}```

#6
February 1st, 2013, 12:14 AM
 elrohir
Registered User

Join Date: Jan 2013
Posts: 2
Time spent in forums: 8 m 26 sec
Reputation Power: 0
Quote:
 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 #include #include #include #include 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
February 1st, 2013, 01:04 AM
 salem
Contributed User

Join Date: Jun 2005
Posts: 3,835
Time spent in forums: 2 Months 3 Weeks 2 Days 16 h 3 m 48 sec
Reputation Power: 1774
Did you check all your results all the way up to 90?
list of numbers
__________________
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper

#8
February 1st, 2013, 07:21 AM
 blobman23
Registered User

Join Date: Nov 2012
Posts: 16
Time spent in forums: 2 h 41 m 15 sec
Reputation Power: 0
Quote:
 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.

 Viewing: Dev Shed Forums > Programming Languages > C Programming > Could someone help me out?