Thread: C++ Help
View Single Post
Old 11-08-2008   #8
Tawa
 
Tawa's Avatar
 
Last Online: 06-12-2019
Join Date: Mar 2006
Posts: 6,245
Thanks: 2,120
Thanked 3,365 Times in 1,740 Posts
Groans: 29
Groaned at 44 Times in 35 Posts
Default Re: C++ Help

Quote:
Originally Posted by bitarno View Post

2nd Exercise

Consider the following series
x(n) = 1/n + 1/(n-1) + + 1 for all n > 0
x(1) = 1
Develop a recursive function to compute the value of x(n)
Code:
#include <iotream>
using namespace std;

double function(int);

int main(void)
{
    int n;
    double x;

    do{
        cout << "Enter n: ";
        cin >> n;
    }while(n<1);
    
    x = function(n);

    cout << "X(n) = " << x << endl;

    return 0;
}

double function(int n)
{
    if(n == 1)
        return 1;
    return 1/n + function(n-1);
}

Damn I Miss The C++
__________________
What we do in life, echoes in eternity.
Tawa is offline   Reply With Quote