Completely lost

My task is to define a function that accepts i as as an argument to compute the following summation, return the summation from the function

m(i) 1/2 + 2/3 + 3/4 + ... + i / (i + 1), where i >= 1

halp

Show us what you've tried.

You're going to need:
- a for loop to iterate a counter variable (e.g. n) from 1 to i, inclusive.
- a variable to accumulate the sum
- floating-point division, for example
sum += static_cast<double>(n) / (n + 1);

Tutorials:
http://cplusplus.com/doc/tutorial/control/
http://cplusplus.com/doc/tutorial/functions/
Last edited on
@OP
Here's a supplementary start, with an alternative hack to the static_cast - try it with 1 instead of 1
Your next challenge is to design the function. Perhaps even get user input for the limit and produce some user-friendly output.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>

int main()
{
    int LIMIT{3};
    double sum{0};
    
    for(int i = 1; i < LIMIT; i++)
    {
        sum += i / (1. * ( i + 1 ));
    }
    
    std::cout << sum << '\n';
    return 0;
}


1.16667
Program ended with exit code: 0
1
2
3
4
5
6
#include <iostream>
double sum( int i ){ return i ? i / ( i + 1.0 ) + sum( i - 1 ) : 0; }
int main()
{
   std::cout << sum( 10 ) << '\n';
}
this is an interesting series. it appears to approach N, always slightly under it.
limπ‘›β†’βˆž 𝑛/𝑛+1
= lim π‘›β†’βˆž 𝑛⋅1/𝑛 /( (𝑛+1)β‹…1/𝑛 )
= lim π‘›β†’βˆž 1/(1+1/𝑛)
= 1
Last edited on
Topic archived. No new replies allowed.