hi all,
my son is stuck with this programme about recursion,
I would like to help him out.
this is what he wants to do:
1 1 2 0 3 -1 4 -2 5....
such that the 3 terms are 1, 1 and 2 and each term thereafter is defined recusively as the sum of the first 2 items minus the third, that is:
seq(n) = seq(n-3)+seq(n-2)-seq(n-1)
the programme accepts the number into the vairable n
implement the recursive function that computes the above series, call the function recursiveSeq().
Can anyone write this code out so i can analise it and help my son out?? thank you
Comments
{
int i1=1,i2=1,i3=2;
cout<<i1<<" ";
cout<<i2<<" ";
cout<<i3<<" ";
for(int i=1;i<=n;i++)
{
cout<<i1+i2-i3<<" ";
int temp=i1+i2-i3;
i1=i2;
i2=i3;
i3=temp;
}
return 0;
}
Code over.
THe function must recieve the number of terms as argument as 'n'.
: recursiveSeq(int n)
: {
: int i1=1,i2=1,i3=2;
: cout<<i1<<" ";
: cout<<i2<<" ";
: cout<<i3<<" ";
: for(int i=1;i<=n;i++)
: {
: cout<<i1+i2-i3<<" ";
: int temp=i1+i2-i3;
: i1=i2;
: i2=i3;
: i3=temp;
: }
: return 0;
: }
[/code]
Although it works, this is not a recursive function: a recursive function, by definition, calls itself.
The code below is recursive and works:
[code]
#include <iostream>
int recursiveSeq(const int n)
{
if (1==n || 2==n)
{
return 1;
}
else if (3==n)
{
return 2;
}
else
{
return recursiveSeq(n-3) + recursiveSeq(n-2) - recursiveSeq(n-1);
}
}
int main()
{
for (int i=1; i!=11; ++i)
{
std::cout << i << " : " << recursiveSeq(i) << std::endl;
}
}
[/code]
How is this series called?
See ya,
bilderbikkel
: : recursiveSeq(int n)
: : {
: : int i1=1,i2=1,i3=2;
: : cout<<i1<<" ";
: : cout<<i2<<" ";
: : cout<<i3<<" ";
: : for(int i=1;i<=n;i++)
: : {
: : cout<<i1+i2-i3<<" ";
: : int temp=i1+i2-i3;
: : i1=i2;
: : i2=i3;
: : i3=temp;
: : }
: : return 0;
: : }
: [/code]:
:
: Although it works, this is not a recursive function: a recursive
: function, by definition, calls itself.
:
: The code below is recursive and works:
:
: [code]:
: #include <iostream>
:
: int recursiveSeq(const int n)
: {
: if (1==n || 2==n)
: {
: return 1;
: }
: else if (3==n)
: {
: return 2;
: }
: else
: {
: return recursiveSeq(n-3) + recursiveSeq(n-2) - recursiveSeq(n-1);
: }
: }
:
: int main()
: {
: for (int i=1; i!=11; ++i)
: {
: std::cout << i << " : " << recursiveSeq(i) << std::endl;
: }
: }
: [/code]:
:
: How is this series called?
:
: See ya,
: bilderbikkel
im sorry but i forgot to mention this:
The main() program should looki like this:
cout<< "Enter n = ";
cin>>n;
cout<<"Recursive computation: " <<recursiveSeq(n)<<endl;
: The main() program should looki like this:
: cout<< "Enter n = ";
: cin>>n;
: cout<<"Recursive computation: " <<recursiveSeq(n)<<endl;
Then let it be so!
[code]
: : #include <iostream>
: :
: : int recursiveSeq(const int n)
: : {
: : if (1==n || 2==n)
: : {
: : return 1;
: : }
: : else if (3==n)
: : {
: : return 2;
: : }
: : else
: : {
: : return recursiveSeq(n-3) + recursiveSeq(n-2) - recursiveSeq(n-1);
: : }
: : }
: :
using std::cout;
using std::cin;
: : int main()
: : {
: cout<< "Enter n = ";
: cin>>n;
: cout<<"Recursive computation: " <<recursiveSeq(n)<<endl;
: : }
: : [/code]
See ya,
bilderbikkel
this is what ive type up:
#include
using namespace std;
int recursiveSeq(const int n)
{
if (1==n || 2==n)
{
return 1;
}
else if (3==n)
{
return 2;
}
else
{
return recursiveSeq(n-3) + recursiveSeq(n-2) - recursiveSeq(n-1);
}
}
using std::cout;
using std::cin;
int main()
{
int n;
cout<< "Enter n = ";
cin>>n;
cout<<"Recursive computation: " <<recursiveSeq(n)<<endl;
}
Then i compile it
the output was enter number:
then i enter the number and nothing happens??
: this is what ive type up:
:
: #include
: using namespace std;
:
:
:
: int recursiveSeq(const int n)
: {
: if (1==n || 2==n)
: {
: return 1;
: }
: else if (3==n)
: {
: return 2;
: }
: else
: {
: return recursiveSeq(n-3) + recursiveSeq(n-2) - recursiveSeq(n-1);
: }
: }
:
: using std::cout;
: using std::cin;
: int main()
: {
: int n;
:
: cout<< "Enter n = ";
: cin>>n;
: cout<<"Recursive computation: " <<recursiveSeq(n)<<endl;
: }
:
:
: Then i compile it
: the output was enter number:
: then i enter the number and nothing happens??
:
sorry it did work my bad but only for certain numbers??
what if i want any number??or negative numbers??
The number #1 FAQ: 'Why does my program close immediatly?'. Because the program does not prompt the user for a key press and therefore closes.
Add the line
[code]
std::cin.get();
[/code]
before the end of main() so your program will wait for a key press after displaying the outcome.
See ya,
bilderbikkel
: : #include
: : using namespace std;
: :
: : [red]double[/red] recursiveSeq(const [red]double[/red] n)
: : {
: : if (1==n || 2==n) [red]//What to do with this line?[/red]
: : {
: : return 1.0;
: : }
: : else if (3==n) [red]//What to do with this line?[/red]
: : {
: : return 2;
: : }
: : else
: : {
: : return recursiveSeq(n-3.0) + recursiveSeq(n-2.0) - recursiveSeq(n-1.0);
: : }
: : }
: :
: : using std::cout;
: : using std::cin;
: : int main()
: : {
: : [red]double[/red] n;
: :
: : cout<< "Enter n = ";
: : cin>>n;
: : cout<<"Recursive computation: " <<recursiveSeq(n)<<endl;
[red]cin.get();[/red]
: : }
[/code]
: :
: sorry it did work my bad but only for certain numbers??
: what if i want any number??or negative numbers??
:
It is possible. You can change the data type from int to double, but then the function makes less sense...
bilderbikkel