The world’s Largest Sharp Brain Virtual Experts Marketplace Just a click Away
Levels Tought:
Elementary,Middle School,High School,College,University,PHD
| Teaching Since: | Apr 2017 |
| Last Sign in: | 103 Weeks Ago, 3 Days Ago |
| Questions Answered: | 4870 |
| Tutorials Posted: | 4863 |
MBA IT, Mater in Science and Technology
Devry
Jul-1996 - Jul-2000
Professor
Devry University
Mar-2010 - Oct-2016
Urgent.. Due in a few hours. I need help implementing three functions (I don't know what parameters to use) and creating pthreads in main. Attached more details and code in file.
1. Modify the main function to implement a loop that reads 10 integers
from the console (user input) and stores these numbers in a onedimensional (1D) array (this code will go right after the comment that
says “Add code to perform any needed initialization or to process user
input”). You should use a global array for this.
2. Implement a separate pthread function function for each one of the
following operations:
a. Count and print out how many of the entered numbers are negative.
This function must be named countNegatives
b. Calculate and print the average value of all the numbers entered. This
function must be named average
c. Print the numbers in reverse order from the order in which they were
entered. This function must be named reverse
3. Modify the main function to create one pthread for each one of
the functions that you implemented in (3) above (this code will go
between the comment that says “TODO: Modify according to
assignment requirements” and the “if (rc)” check).
4. Compile your program and run it several times. If the output of
your program is garbled, you may need to add a small delay in
between creating the next thread. One simple way to do this is to add
a loop that performs a count, such as the one below:
for (int count = 0; count < 100000; count++);
Expected Output:
Given the following input: 0 -10 -10 10 10 10 10 10 0 10
Your program should produce the following output: #include <pthread.h> #include <iostream>
using namespace std;
//
//
//
//
//
// This function shows the skeleton that pthread
functions must adhere to.
Copy this skeleton for any pthread function
you need to define.
In the copy, modify the name of the function
and the function body as needed. //int input[10];
void *routineName(void *arg)
{
// TODO: Add code that implements
//
the thread's functionality
cout << "Thread is running..." << endl;
return 0;
}
void *countNegatives(){
}
void *average(){
}
void *reverse(){
}
int main()
{
// id is used to store a unique thread identifier,
// returned by the call to create a new POSIX thread
pthread_t id;
// rc is used to store the code returned by the
// call to create a new POSIX thread. The value is
// zero (0) if the call succeeds.
int rc;
// TODO: Add code to perform any needed initialization
//
or to process user input /*
cout << "Enter 10 numbers: ";
//For Loop to take in 10 numbers
for(int count = 0; count < 10; count++)
{
cin >> input[count];
}
*/
// Create thread(s)
// TODO: Modify according to assignment requirements
rc = pthread_create(&id, NULL, routineName, NULL);
if (rc){
cout << "ERROR; return code from pthread_create() is " << rc << endl;
return -1;
}
// NOTE: Using exit here will immediately end execution of all threads
pthread_exit(0);
}