In pseudocode show how to add the terms of the harmonic seri

In pseudocode, show how to add the terms of the harmonic series (1 + 1/2 + 1/3 + ...) using 4 threads. Stop adding terms when they become smaller than 10^(-5).

Solution

Pseudocode for the problem provided above is below:-


// This defines the number of threads to be used.
const int num = 4;
// Each thread will compute this function
// N is amount to terms to compute
double calculate(long long start, long long N)
{
for(; start <= n; start++)
{
sum += 1.0/start;
}
   return sum;
}

void execute(long long N)
{
thread th[num]; // We are using four threads
double sum[num] = {0.0}; // Array of sum for each thread
double final_Sum = 0.0;
clock_t start = clock();

// Assign threads their task.
l: for(int i = 0; i < num; i++)
{
// Procedure to call thread
th[i] = thread(calculate,(((n/4)*i)+1), ((n/4)*(i+1)));
}

// Join threads to terminate and calculate the final sum
for(int i = 0; i < num; i++)
{
th[i].join();
final_Sum += sum[i];
}
if (final_Sum > 0.000001) // Execute the loop again and over till the condition satisfies.
   goto l;

}

In pseudocode, show how to add the terms of the harmonic series (1 + 1/2 + 1/3 + ...) using 4 threads. Stop adding terms when they become smaller than 10^(-5).S

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site