Question: Threads in C++ Code needs to be adjusted. Question is for Java but is implemented in C++. Code has been commented where it needs fixing
Threads in C++
Code needs to be adjusted. Question is for Java but is implemented in C++. Code has been commented where it needs fixing (inside main).
Question:
1. Part A) Partitioning array into fixed chunks.
Evaluate method for a polynomial has been provided here. Using the Polynomial class, you have to use java threads in the Polynomial class such that the polynomial evaluation calculations gets distributed among threads. Look at the vector addition demo using threads to get started. Use it as a template to get started with Multi-threaded Polynomial evaluation.
Answer for java is provided here:
https://www.chegg.com/homework-help/questions-and-answers/5-53-title-polynomial-evaluation-using-java-threads-group-assignment-allowed-submit-group--q39898568?trackid=9b3ea157b3da&strackid=c2eb7050c356
// CPP code to illustrate // Queue in Standard Template Library (STL) #include
#define MAX 50000 #define COEFFICIENT 1
using namespace std;
class Term { public: int coefficient; int degree; Term(int coef, int deg) { coefficient = coef; degree = deg; } };
double power(double x, int degree) { if(degree == 0) return 1; if(degree == 1) return x;
return x * power(x, degree - 1); }
double sequential(int coeffArr[], double x) { int maxDegree = MAX - 1; int i; double answer = 0; for( i = 0; i < maxDegree; i++) { double powerX = power(x, i);
//printf("%f ", powerX); answer = answer + coeffArr[i] * powerX; } return answer; }
// Print the queue void showq(queue void initialize(int coeffArr[]) { int maxDegree = MAX - 1; int i; for( i = 0; i < maxDegree; i++) { coeffArr[i] = COEFFICIENT; } } // Driver Code int main() { int coeffArr[MAX]; initialize(coeffArr); double x = 0.99; queue cout << " gquiz.size() : " << gquiz.size(); Term firstTermInQ = gquiz.front(); cout << " gquiz.front() : " << firstTermInQ.coefficient<<" "< //error is in this block int numThreads; cin>>numThreads; int chunkSize = ceil(MAX/numThreads); thread *threads = new thread[numThreads]; for(int i = 0; i // printf("Goodbye, World! "); // first.join(); // second.join(); for(int i = 0; i return 0; }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
