Chief Delphi

Chief Delphi (http://www.chiefdelphi.com/forums/index.php)
-   Programming (http://www.chiefdelphi.com/forums/forumdisplay.php?f=51)
-   -   RAndom C++ Problem (http://www.chiefdelphi.com/forums/showthread.php?t=54166)

brennerator 14-02-2007 20:09

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?

JBotAlan 14-02-2007 21:28

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

Good luck with this and post back if you need help,

JBot

Kevin Watson 14-02-2007 22:45

Re: RAndom C++ Problem
 
Quote:

Originally Posted by brennerator (Post 578402)
#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?

Help with what? It looks like a Fibonacci number generator.

-Kevin

brennerator 14-02-2007 23:35

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

Adam Richards 14-02-2007 23:37

Re: RAndom C++ Problem
 
Quote:

Originally Posted by brennerator (Post 578551)
How can you make the user enter in one of the numbers in the sequence and have hte prog spit it out

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.

Salik Syed 15-02-2007 13:49

Re: RAndom C++ Problem
 
Code:

int fib(int number)
{
if(number==0 || number ==1) return number;
else return fib(number-1)+fib(number-2);
}

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

Salik Syed 15-02-2007 13:55

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.

FIRST JerseyKid 15-02-2007 14:14

Re: RAndom C++ Problem
 
i think u need a ; after the while

Cheezmeister 15-02-2007 15:04

Re: RAndom C++ Problem
 
Quote:

Originally Posted by FIRST JerseyKid (Post 578830)
i think u need a ; after the while

Yep, that's right.


All times are GMT -5. The time now is 04:52.

Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.
Copyright © Chief Delphi