As you may know, I am learning C++. After I learn something new, I make a small little program to demonstrate and get the hand of things. After I had read about functions, I decided to make a program demonstrating recursion. I did a Fibonacci pattern, where you input a number to jump to in the sequence and it tells what number is there.
#include <iostream>
using namespace std;
int fib(int n);
int main() {
int n, answer;
cout << "\t\t\t Fibonacci Using Recursion\n\n";
cout << "Enter the number to find: ";
cin >> n;
cout << "\n\n";
answer = fib(n);
cout << answer << " is the " << n << "th Fibonacci number.\n";
return 0;
}
int fib(int n){
cout << "Processing fib(" << n << ")...";
if (n < 3) {
cout << "Return 1!\n";
return 1;
}
else {
cout << "Call fib(" << n-2 << ") and fib(" << n-1 << ").\n";
return( fib(n-2) + fib(n-1) );
}
}
Anyway, it works, which is kinda the point. I started by typing in 6, and it returned 8, and showed every step. Sadly, I inputted 20 next. OMFG! It took 15 minutes to compute!! It went through the function 13,529 times to receive the answer!! DON'T TYPE IN 20 AND RUN IT! I thought I screwed it up, but it did end. lol
Not only did you post two of this topic, but you also posted it in the complete wrong forum.
You're really pissing me off with your lack of respect for seperate forums. This is a warning, the next time it happens I'll delete the topic without notice.
Why the hell are you posting pure programming along with source code in PC Dev? Douche bag...
Dunno what you're looking for here. No comment lines, code is bloated, instead of just taking time to calculate the 20th you make it print out tons of crap it really doesn't need to.
As I ALREADY stated, the object of the program is to demonstrate recursion. If it didn't print all that "crap" then how could I tell what function branched off to what and if it froze or not? Recursion used in such a way is a memory eater, and could easily crash your computer. I could do it a stable, way faster way without "bloated" code, but I chose not to.
Now, I'm going to say this nicely one more time: I don't want you in my programming topics anymore, you're too big a douche. :spin:
None "bloated" code with way too many comments that dosn't eat memory:
#include <iostream>
// Function prototype(s)
int fib(int position);
int main() {
int answer,position;
// Displays Question and assigns anser to variable "position"
std::cout << "Which position in the Fibonacci Sequence would you like to view?\n";
std::cin >> position;
std::cout << std::endl;
//passes position to the fib function to be solved.
answer = fib(position);
// displays the answer
std::cout << answer << " is the ";
std::cout << position << "th Fibonacci number.\n";
// returns 0 so windows or some other program won't detect an error.
return 0;
}
//Finds the passed position in the Fibonacci sequence
int fib(int n) {
int minusTwo=1, minusOne=1, answer = 2;
//checks to see if you inputted something less than 3, if so the anser is 1, so return it.
if (n < 3)
return 1;
//go through sequence until position is reached, then return value at corresponding position.
for (n -= 3; n; n--) {
minusTwo = minusOne;
minusOne = answer;
answer = minusOne + minusTwo;
}
//Return the answer
return answer;
}
Well one way to easily speed it up would be to not post so much crap to 'keep you updated'. Just post the answer. Then do it yourself on a calculator or with good old fashioned brain power to check. If it doesn't check out, THEN add a cout somewhere.
I'd appreciate it if you took some criticism instead of acting like a 12 year old prepubescent little girl.
Notice the second post that it doesn't display all the crap? And, notice I just turned 13?
And, the point of doing the program was FUNCTION RECURSION, NOT GETTING THE ANSWER! How hard is that to understand? I DO need it to display the other crap, because that's the whole point of the program. If ANYTHING, I should make it NOT display the answer.
is there some other way that might be faster to create the fibronocci sequence? not to insult your logic, that would take me a while to write, but is there?
Yeah, it can be done using loops. I might make a looping version later tonight just to show you, but as far as the best way, I'd seriously use the recursion.
It'd be all complicated with loops and just plain ugly. The recursion, while ugly and a memory whore, is still the best programming method for this kind of thing.
But then don't forget that recursion is not the answer to most other things. Don't take it for granted as a recursive approach is generally a crappy one.
If you end up with the right answer, guess what, your recursion worked. And you'd have a much more efficient program.
Why is the concept so hard to grasp that I made the program how I wanted it? I don't see how it could be so hard to understand that it is a correctly written program that does what it's supposed to: VISUALLY display recursion. The point of the program as not to answer the Fibonacci sequence, like the second one I posted was designed to do, but it was made to do exactly what it does.
Gyrovorbis, there is no need to post the source using loops because of the fact that I already did in my third post of this topic.
#include<iostream.h>
void fibb(unsigned long int x,unsigned long int y,unsigned long int a,unsigned long int b){
if(x==y)cout<<a+b<<"\n";
else{cout<<a+b<<"\n";
fibb(x,++y,b,a+b);}}
main()
{
unsigned long int x;
system("title Fibbonacci Sequence");
cin>>x;
system("cls");
fibb(x,1,0,1);
system("PAUSE");
}