Completely lost

Oct 28, 2021 at 8:59pm
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

Oct 28, 2021 at 9:04pm
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 Oct 28, 2021 at 9:53pm
Oct 29, 2021 at 3:34am
@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
Oct 29, 2021 at 7:18am
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';
}
Oct 29, 2021 at 1:53pm
this is an interesting series. it appears to approach N, always slightly under it.
Oct 29, 2021 at 11:35pm
limπ‘›β†’βˆž 𝑛/𝑛+1
= lim π‘›β†’βˆž 𝑛⋅1/𝑛 /( (𝑛+1)β‹…1/𝑛 )
= lim π‘›β†’βˆž 1/(1+1/𝑛)
= 1
Last edited on Oct 29, 2021 at 11:35pm
Topic archived. No new replies allowed.