|
|
|
![]() |
|
|||||||
|
||||||||
![]() |
| Thread Tools | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
RAndom C++ Problem
#include <iostream>
using namespace std; int main() { int a,b,c,d; b=0; c=1; d=1; cout << "Which Fib do you want to find?"; cin >> a; do { cout << c << endl; c = c + d; cout << d << endl; d = c + d; b++; } while (b<a) cout << "And those are the fibs" << endl; } Any help? |
|
#2
|
|||||
|
|||||
|
Re: RAndom C++ Problem
I'm not sure what this segment is supposed to do; a quick look at the source didn't reveal much (maybe I'm just being dense, but I didn't get it), but it won't even compile without a semicolon (
at the end of the while.Here's the output from the test run I gave it: Code:
Which Fib do you want to find?5 1 1 2 3 5 8 13 21 34 55 And those are the fibs JBot |
|
#3
|
||||
|
||||
|
Re: RAndom C++ Problem
Quote:
-Kevin |
|
#4
|
|||
|
|||
|
Re: RAndom C++ Problem
How can you make the user enter in one of the numbers in the sequence and have hte prog spit it out
|
|
#5
|
|||||
|
|||||
|
Re: RAndom C++ Problem
Just run a test before the number is outputted. If the number is < the inputted number, continue, if number == the inputted number, end with result, if number > inputted number, return error to user.
|
|
#6
|
|||
|
|||
|
Re: RAndom C++ Problem
Code:
int fib(int number)
{
if(number==0 || number ==1) return number;
else return fib(number-1)+fib(number-2);
}
User enters a term, it spits out the fib value for that term. Look at the inductive definition of Fibonacci series if you want to see why this works It runs in N*N time though.There are alot of clever optimizations that can be done to make this faster. |
|
#7
|
|||
|
|||
|
Re: RAndom C++ Problem
Oops, i didn't even see the code you had so far... well I think what you are doing works fine, just follow the advice above.
|
|
#8
|
|||||
|
|||||
|
Re: RAndom C++ Problem
i think u need a ; after the while
|
|
#9
|
|||
|
|||
|
Re: RAndom C++ Problem
|
![]() |
| Thread Tools | |
| Display Modes | Rate This Thread |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Random Stuff | Brm789 | Chit-Chat | 11 | 20-11-2003 16:13 |
| Random Thought... | Marygrace | Chit-Chat | 12 | 14-05-2003 15:50 |
| How random is random???? | archiver | 1999 | 0 | 23-06-2002 21:59 |
| Random | Marc P. | Chit-Chat | 138 | 13-02-2002 15:11 |
| Random Question... | Jessica Boucher | Chit-Chat | 26 | 02-07-2001 21:37 |