The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> C Programming
|
Could someone help me out?
Discuss Could someone help me out? in the C Programming forum on Dev Shed. Could someone help me out? C programming forum discussing all C derivatives, including C#, C++, Object-C, and even plain old vanilla C. These languages are low level languages, and used on projects such as device drivers, compilers, and even whole computer operating systems.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

January 30th, 2013, 10:54 AM
|
|
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:
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)
{
}
}
|

January 30th, 2013, 04:39 PM
|
 |
Contributing User
|
|
|
|
|
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!
|

January 30th, 2013, 06:07 PM
|
|
Still Learning
|
|
Join Date: Dec 2012
Location: Montreal, Canada
|
|
|
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.
|

January 30th, 2013, 11:42 PM
|
|
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?
|

January 31st, 2013, 10:53 PM
|
|
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;
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);
}
|

February 1st, 2013, 12:14 AM
|
|
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 <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
|

February 1st, 2013, 01:04 AM
|
 |
Contributed User
|
|
|
|
|

February 1st, 2013, 07:21 AM
|
|
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 |
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. 
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|