Question

Modify the sum_thread.cpp program to compute sum = 1 + 1/2 + 1/3 + 1/4 +...

Modify the sum_thread.cpp program to compute sum = 1 + 1/2 + 1/3 + 1/4 + … 1/n.

Let’s estimate natural using n = 20.

sum_thread.cpp

#include <chrono>
#include <iostream>
#include <mutex>
#include <random>
#include <utility>
#include <vector>
#include <thread>

using namespace std;

constexpr long long size= 1000000;   
mutex myMutex;

void sumUp(unsigned long long& sum, const vector<int>& val, 
   unsigned long long beg, unsigned long long end){
   long long localSum = 0;
    for (auto it= beg; it < end; ++it){
        localSum+= val[it];
    }
    lock_guard<mutex> myLock(myMutex);
    sum += localSum;
}

int main(){

  cout << endl;

  vector<int> randValues;
  randValues.reserve(size);

  mt19937 engine (0);
  uniform_int_distribution<> uniformDist(1,10);
  for ( long long i=0 ; i< size ; ++i)
     randValues.push_back(uniformDist(engine));
 
  unsigned long long sum= 0;
  auto start = chrono::system_clock::now();

  int threads = 4;
  thread t[threads];
  long long slice = size / threads;
  int startIdx=0;
  for (int i = 0; i < threads; ++i) {
    cout << "Thread[" << i << "] - slice ["
         << startIdx << ":" << startIdx+slice-1 << "]" << endl;
    t[i] = thread(sumUp, ref(sum), ref(randValues), startIdx, startIdx+slice-1);
    startIdx += slice;
  }

  for (int i = 0; i < threads; ++i)
     t[i].join();

  chrono::duration<double> dur= chrono::system_clock::now() - start;
  cout << "Time for addition " << dur.count() << " seconds" << endl;
  cout << "Result: " << sum << endl;

  cout << endl;

}

Homework Answers

Answer #1

#include <iostream>

using namespace std;

// Function to return the sum of

// 1/1 + 1/2 + 1/3 + ..+ 1/n

class gfg

{

     

public : double sum(int n)

{

    double i, s = 0.0;

    for (i = 1; i <= n; i++)

    s = s + 1/i;

    return s;

}

};

// Driver code

int main()

{

    gfg g;

    int n = 5;

    cout << "Sum is " << g.sum(n);

    return 0;

}

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
Modify the sum_thread.cpp program to compute sum = 1 + 1/2 + 1/3 + 1/4 +...
Modify the sum_thread.cpp program to compute sum = 1 + 1/2 + 1/3 + 1/4 + … 1/n. Let’s estimate natural using n = 20. sum_thread.cpp #include <chrono> #include <iostream> #include <mutex> #include <random> #include <utility> #include <vector> #include <thread> using namespace std; constexpr long long size= 1000000; mutex myMutex; void sumUp(unsigned long long& sum, const vector<int>& val, unsigned long long beg, unsigned long long end){ long long localSum = 0; for (auto it= beg; it < end; ++it){ localSum+=...
Modify the sum_thread.cpp program to compute harmonic sum = 1 + 1/2 + 1/3 + 1/4...
Modify the sum_thread.cpp program to compute harmonic sum = 1 + 1/2 + 1/3 + 1/4 + … 1/n. Let’s estimate natural using n = 20. sum_thread.cpp #include <chrono> #include <iostream> #include <mutex> #include <random> #include <utility> #include <vector> #include <thread> using namespace std; constexpr long long size= 1000000; mutex myMutex; void sumUp(unsigned long long& sum, const vector<int>& val, unsigned long long beg, unsigned long long end){ long long localSum = 0; for (auto it= beg; it < end; ++it){...
I'm trying to figure out why my program has an infinite loop. I'm pretty sure it...
I'm trying to figure out why my program has an infinite loop. I'm pretty sure it has something to do with the catch(bad_alloc) function or maybe the while (true) loop above it but I'm not sure. Can you help me figure out why i have an infinite loop and help me fix it? Thanks ---------------------------------- main.cc ---------------------------- #include #include #include #include "numbers.h" using namespace std; int main() { Numbers N1, N2;       for(size_t i = 2; i < 16;...
Write up to three lines to explain the logic used behind those codes.        1) #include <iostream>...
Write up to three lines to explain the logic used behind those codes.        1) #include <iostream> #include <string> #include <fstream> #include <vector> #include <sstream> using namespace std; int main() {         ifstream infile("worldpop.txt");         vector<pair<string, int>> population_directory;         string line;         while(getline(infile, line)){                 if(line.size()>0){                         stringstream ss(line);                         string country;                         int population;                         ss>>country;                         ss>>population;                         population_directory.push_back(make_pair(country, population));                 }         }         cout<<"Task 1"<<endl;         cout<<"Names of countries with population>=1000,000,000"<<endl;         for(int i=0;i<population_directory.size();i++){                 if(population_directory[i].second>=1000000000){                        ...
1) Create a flowchart for the program below and Trace the program below with input of...
1) Create a flowchart for the program below and Trace the program below with input of 3 and then again with input of 5. #include <iostream> using namespace std; int Fall(int x, int m) { int Xm = 1, i=x; while(i>=x-m+1) { Xm = Xm*i; i=i-1; } return Xm; } int Delta(int x, int m) { int ans = 0; ans = Fall(x+1,m); ans = ans - Fall(x,m); return ans; } void main() { int x = 0, m =...
#include<iostream> using namespase std; int main() {     int i=1;     int j=-5;     int k=80;...
#include<iostream> using namespase std; int main() {     int i=1;     int j=-5;     int k=80;     float f=2.22;     float h=-7.5;     char a='S';     char b='M';     cout<<i<<"\t"<<j<<"\t"<<k<<endl;     Cout<<f<<"\t"<<h<<endl;     cout<<a<<"\t"<<b<<endl;     return 0;     }
No matter what I do I cannot get this code to compile. I am using Visual...
No matter what I do I cannot get this code to compile. I am using Visual Studio 2015. Please help me because I must be doing something wrong. Here is the code just get it to compile please. Please provide a screenshot of the compiled code because I keep getting responses with just code and it still has errors when I copy it into VS 2015: #include <iostream> #include <conio.h> #include <stdio.h> #include <vector> using namespace std; class addressbook {...
For this assignment you need to write a parallel program in C++ using OpenMP for vector...
For this assignment you need to write a parallel program in C++ using OpenMP for vector addition. Assume A, B, C are three vectors of equal length. The program will add the corresponding elements of vectors A and B and will store the sum in the corresponding elements in vector C (in other words C[i] = A[i] + B[i]). Every thread should execute approximately equal number of loop iterations. The only OpenMP directive you are allowed to use is: #pragma...
For this assignment, you need to write a parallel program in C++ using OpenMP for vector...
For this assignment, you need to write a parallel program in C++ using OpenMP for vector addition. Assume A, B, C are three vectors of equal length. The program will add the corresponding elements of vectors A and B and will store the sum in the corresponding elements in vector C (in other words C[i] = A[i] + B[i]). Every thread should execute an approximately equal number of loop iterations. The only OpenMP directive you are allowed to use is:...
Take the following program and translate it into PEP/9 assembly language: #include <iostream> using namespace std;...
Take the following program and translate it into PEP/9 assembly language: #include <iostream> using namespace std; int theArray[] = { 5, 11, -29, 45, 9, -1}; void sumPos(int ary[], int len, int &sum) {    sum = 0;    for (int i = 0; i < len; i++)            if (ary[i] > 0)                sum = sum + ary[i]; } int main() {    int total;    sumPos(theArray, 6, total);    for (int k=0; k < 6; k++)      cout...