c please leave comments Write a thread program to sum a rand

c++; please leave comments

Write a thread program to sum a random array of n integers. Your program should split the array into k segments, and each of the k threads will sum over that segment of the array. The segment sums should be stored in a global array called a of type integer and size of k. From the command line the user will provide file (named: input.txt) where the numbers to sum 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.. about input.txt 5 In this case, the numbers will be read into from the file input.txt. Note that this file may contain a total number of integers that is not a multiple of k. Simply output the total sum. Name your program 0.cpp and be sure to place it in your pgm directory.

Solution

//code by ashishkr004
//for any query please contact me at ashishkr004atgmlcom
//feel free to mail me at above email address regarding this code.


#include <iostream>
#include <fstream>
#include <pthread.h>

using namespace std;

//define function to sum integers of array.
int sumRandomArray(int array[], int k, int r){
   int totalPartition=sizeof(*array)/k;
   int tSum=0;
   for (int i = r*(totalPartition); i < (r*totalPartition)+totalPartition; i++){
       tSum+=array[i];
   }
   pthread_exit(NULL);
   return tSum;
}

int main() {

   // reads text files.
   ifstream myReadFile;
   myReadFile.open(\"input.txt\");
   int inputLines[2];
   if (myReadFile.is_open()){
       int line=0;
       while (!myReadFile.eof()){
            myReadFile >> inputLines[line];
            line++;
      }
   }
    myReadFile.close();       //close the input.txt file.

    //initialize array of random integers of size n.
    int size=inputLines[1];       //second line of text file is for total size of array.
    int randomArray[size];
    for(int i=0; i<size; i++){
        randomArray[i] = (rand()%100)+1;       //generating random integers and append that integer to given array of size n.
    }

    int totalThreads=inputLines[0];       //first line of text file, total numbers of threads i.e K.
    pthread_t threads;

    int totalSum=0;


    //divide the work equally in all the multiple threads.
    for(int i=0; i < totalThreads; i++ ){
         totalSum +=pthread_create(&threads, NULL, sumRandomArray(randomArray,totalThreads,i), NULL);
    }
    pthread_exit(NULL);

    //sum remaining integers of array that didn\'t come in any section while dividing total integers of array to different threads.
    for (int i = (size-(size % totalThreads))-1; i < size; i++){
       totalSum+=randomArray[i];
    }
    
    cout<<totalSum;
   return 0;
}

c++; please leave comments Write a thread program to sum a random array of n integers. Your program should split the array into k segments, and each of the k th
c++; please leave comments Write a thread program to sum a random array of n integers. Your program should split the array into k segments, and each of the k th

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site