Question

**C++** Write a recursive function called sumover that has one argument n, which is an unsigned...

**C++**

Write a recursive function called sumover that has one argument n, which is an unsigned integer. The function returns a double value, which is the sum of the reciprocals of the first n positive integers. (The reciprocal of x is the fraction 1/x.)

For example,

sumover(1) returns 1.0 (which is 1/1);

sumover(2) returns 1.5 (which is 1/1 + 1/2);

sumover(3) returns approximately 1.833 (which is 1/1 + 1/2 + 1/3).

Define sumover(0) to be zero.

Do not use any local variables in your function.

0 0
Add a comment Improve this question Transcribed image text
Answer #1
#include <iostream>

using namespace std;

double sumover(unsigned int n) {
    if (n == 0) {
        return 0;
    } else {
        return sumover(n - 1) + (1.0 / n);
    }
}

int main() {
    unsigned int n;
    cout << "Enter a positive integer: ";
    cin >> n;
    cout << "sumover(" << n << ") = " << sumover(n) << endl;
    return 0;
}

Add a comment
Know the answer?
Add Answer to:
**C++** Write a recursive function called sumover that has one argument n, which is an unsigned...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT