funjoke88

1. Write an iterative and a recursive version of the Fibonacci series algorithm. You need to ensure the correctness of the both algorithms. Both algorithms should produce similar output if given a similar input.

a. Measure the performance of the two algorithms by measuring the time for both algorithms to listing out 10, 20, 30, 40, 50, 60, 70 and 80 of Fibonacci numbers. The listing procedure could be done with a loop. For each test, repeat 3 times and get the average value.

Piya27 4

1. Write an iterative and a recursive version of the Fibonacci series algorithm. You need to ensure the correctness of the both algorithms. Both algorithms should produce similar output if given a similar input.

a. Measure the performance of the two algorithms by measuring the time for both algorithms to listing out 10, 20, 30, 40, 50, 60, 70 and 80 of Fibonacci numbers. The listing procedure could be done with a loop. For each test, repeat 3 times and get the average value.

Hi funjoke88,

Please Post your effort first.. If you have any problem regarding code or logic then ask..

funjoke88

this is the source code i have wrote but i duno is it correct

funjoke88

when is the recursion information ?i didnt see it .u know how to do

VernonDozier 2,218

when is the recursion information ?i didnt see it .u know how to do

He has two posts linking each other. You keep clicking on them and you keep going back to where you started. It's an infinite loop. If M.C. Escher was alive, he'd paint hundreds of monks mindlessly pressing links that go nowhere on their keyboards, with two of them just watching, refusing to participate, seeing the futility of it all. At first I thought you had two threads on the same topic and tux4life was endlessly linking them as a way of telling you not to start duplicate threads, but since you only have one thread, it's probably a lesson in the danger of recursion without a base case. Or he's just having some fun.

Regarding your code and recursion, you don't have any recursion in your code because you have no function in your code to recurse to, so this must be the iterative version. Since the thread topic is "recursion assignment" and the program has nothing to do with recursion, what exactly is the question?

``````#include <iostream>
#include <conio>
#include<stdio.h>

const int num = 3;

struct time
{
double seconds[num];
double average;
double total;
};

int main()
{
time t;

cout << "======== Cauculate second ========" << endl;
cout << endl;

for (int i=0;i<num;i++)
{
{
cout << "Enter second " << (i+1) << " = ";
cin >> t.seconds[i];
}

t.total += t.seconds[i];
t.average = t.total/3;
}

cout << "\n\Average second = " << t.average << endl;
cout << "==================================" ;

getch ();
system("PAUSE");
return 0;
}``````

[EDIT]
I know this is a programming forum and this is COMPLETELY off-topic, but on a side note, as a grammar question, does anyone know whether I got the grammar correct? I wrote "If M.C. Escher was alive..." Should it have been "If M.C. Escher were alive..."? Shameless thread hijacking, I know, but I'm just curious.
[/EDIT]

commented: >it's probably a lesson in the danger of recursion without a base case. Or he's just having some fun: Well, it applies to both :P +14

NathanOliver 429

alright so you have no code to demonstrate that you have tried recursion or iteration. the iteration is the simple and the fast way of doing this. the logic with a for loop is to ask the user number they want to get and then loop up to it. so if the user entered 6 you would have a for loop like `for(int i = 0; i < 4; i++)` the reason you only need to go to 4 is beacuse the first to terms of the sequence are both 1. so in the loop you would have a first variable and second variable and both would be 1. then for each iteration of the loop you would assign the second variable to a temp then add the first and second variables together and store that as second and then set first to temp.

``````// before the loop
int first, second, temp;

// inside the loop
temp = second;
second += first;
first = temp;``````

this should set you on your way with the iteration. the recursion is a little more difficult and can be very time consuming. for recursion you write a function and pass in what fib number you want and then if it is 1 or 2 you would return 1 otherwise you you would return fib(n-2) + fib(n-1). the logic is to start at the top and then constantly work down to <3. so calling fib(6) would call fib(4) and fib(5) and each one of those would call fib(n-1) + fib(n-2) and keep working back untill they return 1. once they all return the 1 back up it is sumed up for you and you get 8. if you get stuck on the coding let use know but i think this is a good start for the logic.