Analyzing a question

My instructor gave us a problem but i really cant seem to understand what it supposed to do or its output. Can someone please give me an idea or an example of its output.

So here's the problem :

Write a C++ program which calculates the sum

1/1 + 1/2 + 1/3 + 1/4 + ... + 1/N
where N is a positive integer. The program must contain two functions, a function


void sum1(double& sum,unsigned int N)
and a function

double sum2(unsigned int N)
which both calculate the sum. The unsigned integer number is provided in main. Thus we apply pass by value and pass by reference. Be careful with integer divison.

And can someone give me at least the most basic example of pass by value and pass by reference.
Last edited on
You need a loop where the results of the divisions are added. After the loop is done you return that value.

And can someone give me at least the most basic example of pass by value and pass by reference.
Pass by reference:

void sum1(double& sum,unsigned int N)

Pass by value:

void sum1(double& sum,unsigned int N)

@coder777 uhm thank you so much for answering my questions.
i still can't understand the pass by reference and value can you give me an example in a form of code please just the most basic example.

And btw the example (1/1) above is actually division ? so its not a fraction ?
Actually the & in double& sum tells that it is a reference. The variable passed this way is not copied and can be modified within the function passed to. That's the point The called function modifys the value which is then visible by the caller.

Passing by value is passing a copy to a function. Thus changing that parameter will have no affect outside of the function.

See:

https://www.learncpp.com/cpp-tutorial/73-passing-arguments-by-reference/


And btw the example (1/1) above is actually division ? so its not a fraction ?
Yes, it is a division. 1/2 + 1/4 will become 0.5 + 0.25
@coder777 thank you so much for your help :) brb i'll try to solve the problem
@coder777 I tried to make a solution and i didn't use loop cuz i didn't know where to start so this is what i came up with so far

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#include <iostream>
using namespace std;

void sum1 (double& sum, int N, int X) {
	double asd = 1;
	double asd2 = 1;
	double temp1;
	double temp2;
	
	temp1 = asd / N;
	temp2 = asd2 / X;
	
	sum = temp1 + temp2;
	
	
}

void sum2 (int N) {
	
}


int main () {
	
	int a_N;
	int b_N;
	double num1 = 1;
	double num2 = 1;
	double dsum;
	double dssum;
	int Nn;
	
	cout << "Assign a value: " << num1 << "/";
	cin >> a_N;
	cout << "Assign another value: " << num1 << "/" << a_N << " + " << num2 << "/";
	cin >> b_N;
	
	
	
	sum1 (dsum, a_N, b_N);
	
	cout << "Sum: " << dsum;
	
	
}


Im still on the first function and i added another parameter but i dont think that is acceptable because our prof already gave us the function as a given, so is there a way to solve the problem without adding another parameter ?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#include <iostream>
#include <cmath>

double sum2( unsigned int N )
{
    const double one = 1 ; // double avoid integer division
    double sum = 0 ;

    // 1/1 + 1/2 + 1/3 + 1/4 + ... + 1/N
    // loop: i goes from 1 to N
    // in each iteration through the loop, add one/i to the sum
    for( unsigned int i = 1 ; i <= N ; ++i ) sum += one/i ;

    return sum ;
}

// we can reuse the implementation of sum2
void sum1( double& sum, unsigned int N ) { sum = sum2(N) ; }

int main()
{
    unsigned int N = 0 ;
    std::cout << "enter N: " ;
    std::cin >> N ;
    std::cout << N ;

    std::cout << "\n1/1 + 1/2 + ... + 1/" << N << '\n' ;

    std::cout << "returned by sum2: " << sum2(N) << '\n' ;

    // pass by reference
    double sum ;
    sum1( sum, N ) ;
    std::cout << "computed by sum1: " << sum << '\n' ;

    // to verify that the answer is close to the the computed value
    // using the standard mathematical formula for the harmonic constant
    const double gamma = 0.5772156649 ; // Euler–Mascheroni constant
    // https://en.wikipedia.org/wiki/Harmonic_number#Calculation
    const double hn_approx = gamma + std::log(N) + 0.5/N - 1.0/(12*N*N);
    std::cout << " should be about: " << hn_approx << '\n' ;
}

http://coliru.stacked-crooked.com/a/14deb55b2a3b71f0
https://rextester.com/OGHS79457
I think your series converges to the same value after a few terms. If N is bigger than some cutoff, just return the answer will save looping forever.
jonnin wrote:
series converges to the same value after a few terms


It's actually one of the slowest-diverging series there is. (Although that will probably get lost in computer round-off).
For bonus points, add the numbers in the opposite order from what's give. for example, for N=5, do 1/5 + 1/4 + 1/3 + 1/2 + 1/1

This will improve accuracy when N is large.
Why does that improve accuracy? Because of floating-point math not being commutative?
Last edited on
Aha thanks, yep that's what was thinking; adding a really small number to a really big number loses more information than incrementally adding similarly sized numbers together.
yea it hits its limit MUCH slower than i initially thought. I was thinking it would stop changing quickly (adding insignificant amounts that had no effect on the the double's value). If you do it as above, where it minimizes the roundoff problems, its not useful to try to cut off the calculation at all :(
Last edited on
Topic archived. No new replies allowed.