Question: Please use C + + langague , Please do not motify the code, just fix totalsum make the result Correctly. #include #include pthread.h #include

Please use C++langague,Please do not motify the code, just fix totalsum
make the result Correctly.
#include
#include "pthread.h"
#include
#include
using namespace std;
// to protect the C.S
pthread_mutex_t Count_mutex = PTHREAD_MUTEX_INITIALIZER;
double total_sum =0.0;
// function to calculate
double factorail(int num){
double fact =1.0;
for(int i =1; i<= num; ++i){
fact *=i;
//cout << "Factorial("<< i <<")="end; ++i){
part_sum +=1.0/factorail(i);
}
pthread_mutex_lock(&Count_mutex);
total_sum += part_sum;
pthread_mutex_unlock(&Count_mutex);
return nullptr;
}
int main(){
// get the num from user
int n;
cout << "Enter the value of n (max number in the series): ";
cin >> n;
// divide it, so it's works between 2 threads
int mid = n /2;
// Create 2 threads
ThreadArgs* arg1= new ThreadArgs{1,mid};
ThreadArgs* arg2= new ThreadArgs{mid+1,n};
pthread_t tid1,tid2;
pthread_create(&tid1,nullptr,series,arg1);
pthread_create(&tid2,nullptr,series,arg2);
pthread_join(tid1,nullptr);
pthread_join(tid2,nullptr);
// Print the sequence and total result
cout << "Sequence: ";
for (int i =1; i <= n; ++i){
cout <<"1/"<< i <<"!";
if (i != n) cout <<"+";
}
cout << endl;
cout << "Total Result: "<< total_sum << endl;
return0;
}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!