Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Operating Systems. Write a pthread program to find the maximum number in a rando

ID: 3683163 • Letter: O

Question

Operating Systems.

Write a pthread program to find the maximum number in a random array of n integers. Your program should split the array into k segments, and each of the k threads will find the maximum number over that segment of the array. The segment max should be stored in a global array called a of type integer and size of k. Input: From the command line the user will provide file (named: input) where the numbers to find the max will be read into an array and the number of threads to create. The first number in the file will be the size of the array you will need to create. Output: Simply output the max number. Name your program O.cpp and be sure to place it in your pgm directory.

Explanation / Answer

#include<iostream>
#include<cstdlib>
#include<ctime>
using namespace std;
int main()
{
const int size=50;
// array with size of 50
int array[size];
// for random numbers
srand(time(0));
    for(int i=0;i<size;i++){
    // Initializing Array number is less than 100
    array[i]=rand()%100;
    // Displaying array value
    cout<<array[i]<<endl;
    }
            // initializing max, min
                int max=array[0];
                int min=array[0];
/* scanning array to find
   minimum and maximum
   number  */
for(int i=0;i<size;i++){
    // finding minimum number in array
      if(min>array[i]){
         min=array[i];
      }
      //finding maximum number in array
      if(max<array[i]){
      max=array[i];
      }
}
       // displaying output
        cout<<"Maximum Number is :"<<max<<endl;
        cout<<"Minimum Number  is:"<<min<<endl;

return 0;
}