C Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesC Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old January 30th, 2013, 10:54 AM
blobman23 blobman23 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2012
Posts: 16 blobman23 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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)
   {
		
	}
}

Reply With Quote
  #2  
Old January 30th, 2013, 04:39 PM
b49P23TIvg's Avatar
b49P23TIvg b49P23TIvg is offline
Contributing User
Dev Shed Loyal (3000 - 3499 posts)
 
Join Date: Aug 2011
Posts: 3,352 b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level) 
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!

Reply With Quote
  #3  
Old January 30th, 2013, 06:07 PM
admiraln admiraln is offline
Still Learning
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2012
Location: Montreal, Canada
Posts: 55 admiraln User rank is Sergeant Major (2000 - 5000 Reputation Level)admiraln User rank is Sergeant Major (2000 - 5000 Reputation Level)admiraln User rank is Sergeant Major (2000 - 5000 Reputation Level)admiraln User rank is Sergeant Major (2000 - 5000 Reputation Level)admiraln User rank is Sergeant Major (2000 - 5000 Reputation Level)admiraln User rank is Sergeant Major (2000 - 5000 Reputation Level) 
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.

Reply With Quote
  #4  
Old January 30th, 2013, 11:42 PM
elrohir elrohir is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2013
Posts: 2 elrohir User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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?

Reply With Quote
  #5  
Old January 31st, 2013, 10:53 PM
blobman23 blobman23 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2012
Posts: 16 blobman23 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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);
}

Reply With Quote
  #6  
Old February 1st, 2013, 12:14 AM
elrohir elrohir is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2013
Posts: 2 elrohir User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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

Reply With Quote
  #7  
Old February 1st, 2013, 01:04 AM
salem's Avatar
salem salem is offline
Contributed User
Click here for more information
 
Join Date: Jun 2005
Posts: 3,835 salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)  Folding Points: 153 Folding Title: Novice Folder
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

Reply With Quote
  #8  
Old February 1st, 2013, 07:21 AM
blobman23 blobman23 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2012
Posts: 16 blobman23 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesC Programming > Could someone help me out?

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

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


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap