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,unsignedint N)
and a function
double sum2(unsignedint 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.
@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.
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 ?
#include <iostream>
#include <cmath>
double sum2( unsignedint N )
{
constdouble 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( unsignedint i = 1 ; i <= N ; ++i ) sum += one/i ;
return sum ;
}
// we can reuse the implementation of sum2
void sum1( double& sum, unsignedint N ) { sum = sum2(N) ; }
int main()
{
unsignedint 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
constdouble gamma = 0.5772156649 ; // Euler–Mascheroni constant
// https://en.wikipedia.org/wiki/Harmonic_number#Calculationconstdouble hn_approx = gamma + std::log(N) + 0.5/N - 1.0/(12*N*N);
std::cout << " should be about: " << hn_approx << '\n' ;
}
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 :(