The document provides code for a program that tests different sorting algorithms on arrays of random doubles. It contains code for SortProfiler.cpp, SortUtil.cpp, and ArrayUtil.cpp/h files. The document notes there are errors in red in the code and asks to fix them. It provides the code for each file but not SortUtil.h.
Please help me fix the following errors in red- I attached the code be.pdf
1. Please help me fix the following errors in red. I attached the code below for each file that
contains an error. I did not attach the SortUtil.h file because it did not contain any errors.
SortProfiler.cpp:
#include <iostream>
#include <cstdlib>
#include <chrono>
#include <iomanip>
#include "arrayutil.cpp"
#include "SortUtil.cpp"
using namespace std;
using namespace std::chrono;
int main(int argc, char **argv)
{
int *iArray1, *iArray2, *iArray3;
double *dArray1, *dArray2, *dArray3;
long bTime, sTime, qTime;
int size[] = {100, 500, 1000, 5000, 10000, 50000, 100000};
srand(time(NULL));
double *testArray = new double [50];
cout<<"***Testing Selection Sort***"<<endl;
cout<<"Original Array:"<<endl;
ArrayUtil::genRandArray(testArray,50);
ArrayUtil::printArray(testArray,0,49,5);
cout<<endl;
SortUtil::selectionSortDouble(testArray,50);
cout<<"Sorted Array:"<<endl;
ArrayUtil::printArray(testArray,0,49,5);
cout<<endl;
cout<<"***Testing Bubble Sort***"<<endl;
cout<<"Original Array:"<<endl;
ArrayUtil::genRandArray(testArray,50);
ArrayUtil::printArray(testArray,0,49,5);
cout<<endl;
SortUtil::bubbleSortDouble(testArray,50);
cout<<"Sorted Array:"<<endl;
ArrayUtil::printArray(testArray,0,49,5);
cout<<endl;
cout<<"***Testing Quick Sort***"<<endl;
cout<<"Original Array:"<<endl;
ArrayUtil::genRandArray(testArray,50);
2. ArrayUtil::printArray(testArray,0,49,5);
cout<<endl;
SortUtil::quickSortDouble(testArray,0,49);
cout<<"Sorted Array:"<<endl;
ArrayUtil::printArray(testArray,0,49,5);
cout<<endl;
cout<<"Using Arrays with Random Doubles"<<endl;
cout<<setw(8)<<left<<"n"<<setw(16)<<left<<"selectionSort"<<setw(16)<<left<<"bubbleSort"<
<setw(12)<<left<<"quickSort"<<endl;
for(int i = 0; i < 50; i++) cout<<"-";
cout<<endl;
for (int i = 0; i < 7; i++)
{
dArray1 = new double[size[i]];
dArray2 = new double[size[i]];
dArray3 = new double[size[i]];
ArrayUtil::genRandArray(dArray1,size[i]);
ArrayUtil::arrayCopyDouble(dArray1, 0, dArray2, 0, size[i]);
ArrayUtil::arrayCopyDouble(dArray1, 0, dArray3, 0, size[i]);
auto start = high_resolution_clock::now();
SortUtil::selectionSort(dArray1,size[i]);
auto elapsed = high_resolution_clock::now() - start;
sTime = duration_cast<microseconds>(elapsed).count();
start = high_resolution_clock::now();
SortUtil::bubbleSort(dArray2,size[i]);
elapsed = high_resolution_clock::now() - start;
bTime = duration_cast<microseconds>(elapsed).count();
start = high_resolution_clock::now();
SortUtil::quickSort(dArray3,0,size[i]-1);
elapsed = high_resolution_clock::now() - start;
qTime = duration_cast<microseconds>(elapsed).count();
cout<<setw(8)<<size[i]<<setw(16)<<sTime<<setw(16)<<bTime<<setw(12)<<qTime<<endl;
delete[] dArray1;
delete[] dArray2;
delete[] dArray3;
}
cout<<endl<<endl;
return 0;
}
SortUtil.cpp:
#include <iostream>
3. #include <algorithm>
#include "SortUtil.h"
using namespace std;
static void arrayCopy (int source[], int sourceStart, int dest[], int destStart, int size)
{
for (int i = 0 ; i < size; i++)
{
dest [destStart + i] = source [sourceStart + i];
}
}
static int partition(int data[], int start, int end)
{
//implement this function
return 0;
}
static void merge(int data[], int first[],int sizeFirst, int second[], int sizeSecond)
{
int iFirst = 0;
int iSecond = 0;
int j = 0;
while (iFirst < sizeFirst && iSecond < sizeSecond)
{
if (first[iFirst] < second[iSecond])
{
data[j] = first[iFirst];
iFirst++;
}
else
{
data[j] = second[iSecond];
iSecond++;
}
j++;
}
arrayCopy(first, iFirst, data, j, sizeFirst-iFirst);
arrayCopy(second, iSecond, data, j, sizeSecond-iSecond);
}
/* sorting functions */
void SortUtil::bubbleSort(int data[], int size)
{
//implement this function
}
void SortUtil::selectionSort(int data[], int size)
{
//implement this function
}
4. void SortUtil::insertionSort(int data[],int size)
{
for (int i = 1; i < size; i++)
{
int next = data[i];
int j = i;
while (j > 0 && data[j-1] > next)
{
data[j] = data[j-1];
j--;
}
data[j] = next;
}
}
void SortUtil::quickSort(int data[], int start, int end)
{
//implement this function
}
void SortUtil::mergeSort(int data[], int size)
{
if (size <= 1)
return;
int *first = new int[size/2];
int *second = new int[size -size/2];
arrayCopy(data,0,first,0,size/2);
arrayCopy(data,size/2,second,0,size-size/2);
mergeSort(first,size/2);
mergeSort(second,size-size/2);
merge(data, first,size/2, second, size-size/2);
delete[] first;
delete[] second;
}
arrayutil.cpp
#include <cstdlib>
#include<iostream>
#include<iomanip>
#include <cstring>
#include <climits>
#include "arrayutil.h"
using namespace std;
template <typename T>
void ArrayUtil::arrayCopy(T a[], int i, T b[],int j, int numElems)
{
5. /*
for (int m = i; m < (i+numElems); m++)
{
b[j] = a[m];
j++;
}
*/
memcpy(&b[j],&a[i],sizeof(T)*numElems);
}
template <typename T>
void ArrayUtil::genRandArray(T* data, int size)
{
srand(time(NULL));
T factor = 1;
for (int i=0; i<size;i++)
data[i]=rand()%INT_MAX*factor;
}
template <typename T>
void ArrayUtil::genSortedArray(T* data, int size, bool ascending)
{
int i;
T factor = 1;
if (ascending)
{
for (i=0; i<size;i++)
data[i] = i*factor;
}
else
{
for (i=0; i<size;i++)
data[i]=size-i*factor;
}
}
template <typename T>
void ArrayUtil::printArray(T data[],int first,int last, int numPerLine)
{
for (int i = first; i <= last; i++)
{
cout<<fixed<<setprecision(4);
cout<<setw(10)<<data[i]<<" ";
if ((i - first + 1) % numPerLine == 0)
cout<<endl;
}
}
6. arrayutil.h
#include <cstdlib>
#include <iostream>
#ifndef ARRAYUTIL_H
#define ARRAYUTIL_H
using namespace std;
namespace ArrayUtil
{
/** copies numElems elements from array a to array b
* starting at position i in array a and starting at
* position j in array b.
* @param a - the source array
* @param i - the starting index in the source array
* @param b - the destination array
* @param j - the starting index in the destination array
* @param numElems - the number of elements to copy from
* the source array into the destination array.
*/
template <typename T>
void arrayCopy (T a[], int i, T b[], int j, int numElems);
/**
* This function fills an array with random numbers
* @param data - array
* @param size - size of the array
*/
template <typename T>
void genRandArray (T* data, int size);
/**
* This function fills an array with numbers in ascending or descending order
* @param data - array
* @param size - size of the array
*/
template <typename T>
void genSortedArray (T *data, int size, bool ascending);
/**
* Prints elements whose subscripts are in the range F to L
* of an array, numPerLine elements per line.
* @param data - an array containing data to be printed.
* @param first the first index of the array.
* @param last the last index of the array.
*/
template <typename T>
void printArray (T data[], int first, int last, int numPerLine);
7. }
#endif
GDB online Debugger | Compiler - Code, Compile, Run, Debug online C, C++ C Post a new
question 4 Compilation failed due to following error(s).