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

QuickSort using threads in C/C++ Given a large array of integers use C (or C++)

ID: 3740517 • Letter: Q

Question

QuickSort using threads in C/C++
Given a large array of integers use C (or C++) AND Pthread APIs to write a parallel program to find the median.

The parallel program must meet the following requirements: - Assume that the entire array is stored initially in a text file.

Your program needs to read all integer values from the input text file and store them in one location.

Then, the entire array is distributed to the different threads for parallel processing.

The format of the input text file is as follows: o The very first line of the input file begins with # and followed by a space character and a number, which indicates how many integer values are stored in the input file.

Adjacent integer values in the input text file is separated by “ ”.

The following is the content of a sample input text file called inputFile.txt: # 5 412 324 2 2342 12 5 -

DO NOT assume the size of input data. The number of integer elements in the input data can be as large as 10,000,000.

You will need to allocate a memory space dynamically (e.g., using malloc() in C or new in C++) to store all integers read from the given input file.

The input file name should be provided as a command-line parameter of your program. For example, o %mt_median_finder inputFile.txt

Your program needs to create 4 threads and all four threads should use an exactly same thread function

Explanation / Answer

#include <iostream>

#define maxsize 1000000000000

using namespace std;

void elementsList(double,int);

double getMedian (double,int);

void elementsInList (double,int);

int

main()

{

double ptrArray[maxsize];

int i = 0;

string inFileName, getcontent;

cout << "Enter input file name: ";

cin >> inFileName;

fstream inFileStr(inFileName.c_str(), ios::in);

if(inFileStr.fail()){

cerr << "Unable to open " << inFileName << endl;

exit(-1);

}

if(inFileStr.is_open()){

while(!inFileStr.eof()){

if(inFileStr >> value)

{

ptrArray[i] = value;

i++;

}

}

}

elementsList(ptrArray, nElements);

double dMedian = getMedian(ptrArray, nElements);

elementsInList(ptrArray, nElements);

cout<<"The median of numbers in array list is ="

<<dMedian

<<endl;

delete [] ptrArray;

return 0;

}

}

void

elementsList(double ptr,

int iN )

{

double ptrI,ptrJ,dTemp ;

for(ptrI = ptr; ptrI < ptr+iN; ptrI++)

{

cout<<"Next element->"; cin>>dTemp;

for(ptrJ = ptrI-1;

ptrJ >= ptr && ptrJ > dTemp;

(ptrJ+1) = ptrJ ,ptrJ--);

(ptrJ+1)= dTemp;

}

}

double

getMedian(double ptr,

int iN )

{

double dResult;

int iHalf = iN/2;

if(iN%2==0)

{

dResult = ((ptr + iHalf-1)+ (ptr + iHalf))/double(2);

}

else

{

dResult = (ptr + iHalf);

}

return dResult;

}

void

elementsInList(double ptr,

int iN )

{

for(double d=ptr;

d < ptr+iN ;

cout<<d<<endl, d++);

}