Dec 11, 2013 at 9:58pm UTC
New target:
I would like to generate number from sumerizing of the previous numbers like this:
1 2
// 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
// 2 4 7 11 16 23 30 38 47 57 68 80 93 107 122 138 150 168
Can you help what do I need change?
Based on this thread:
http://www.cplusplus.com/forum/beginner/3212/
code of pet:
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
#include <iostream>
#include <conio.h>
using namespace std;
int fib(int n)
{
if (1 == n || 2 == n)
{
return 1;
}
else
{
return fib(n-1) + fib(n-2);
}
}
int main()
{
for (int i=1; i<=15; i++)
{
cout << fib(i) << endl;
}
getch();
return 0;
}
Is it posible to make function to this one, having ability to get the number i, when you know the Fibonacci number?
Last edited on Dec 12, 2013 at 8:19am UTC
Dec 11, 2013 at 10:35pm UTC
You mean a function with output like this?
Please input Fibonacci number: 8
8 is 6. Fibonacci number
It certainly is possible, but you will have to create a new function :)
Last edited on Dec 11, 2013 at 10:36pm UTC
Dec 12, 2013 at 7:42am UTC
MatthewRock
I mean, when FN is 610, what is n? n is 15.
When I know that n is 15, so I can generate new FN: either lower or greater:
fib(15-1);fib(15+1); depending on what I need.
Dec 12, 2013 at 8:20am UTC
Topic changed:
I would like to generate number from sumerizing of the previous numbers like this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
#include <iostream>
#include <conio.h>
using namespace std;
// 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
// 2 4 7 11 16 23 30 38 47 57 68 80 93 107 122 138 150 168
int sum(int i, int &r)
{
r = i+r;
return r;
}
int main()
{
int result=-1;
int &r = result;
for (int i=1; i<=30; i++)
{
cout << i << " : " << sum(i, r) << endl;
}
_getch();
return 0;
}
I tried to initiate the number result as 0 (higher numbers) or -1 a smaller numbers (not ideal because at i=1 should be r=1 or higher. Any idea how to correct this to give this?
2 4 7 11 16 23 30 38 47 57 68 80 93 107 122 138 150 168
I tried to simplify as much as possible
Last edited on Dec 12, 2013 at 9:31am UTC