Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Can you fix these errors in these files? Also, do not modify the code in the ArrayUtil.cpp and the ArrayUtil.h file. Errors main.cpp:4:10: fatal error:
Can you fix these errors in these files? Also, do not modify the code in the ArrayUtil.cpp and the ArrayUtil.h file.
Errors
main.cpp:4:10: fatal error: ArrayUtil.h: No such file or directory 4 | #include "ArrayUtil.h" | ^~~~~~~~~~~~~ compilation terminated. SortProfiler.cpp:19:10: fatal error: ArrayUtil.cpp: No such file or directory#include 19 | #include "ArrayUtil.cpp" | ^~~~~~~~~~~~~~~ compilation terminated. arrayutil.cpp:19:10: fatal error: ArrayUtil.h: No such file or directory 19 | #include "ArrayUtil.h" | ^~~~~~~~~~~~~ compilation terminated.
main.cpp / SortUtil.cpp
#include #include #include "SortUtil.h" #include "ArrayUtil.h" using namespace std; /* auxilliary function (for internal use only) First, implement the functions so that they work with integer arrays. After you verify that they do, then templatize them so that they work with arrays of any numeric type. The starter code in the main function (SortProfiler.cpp) is written to test many of these functions on integer arrays. */ /** * Partitions an array for quicksort and returns the pivot index. * @param data - the array containing the data. * @param start - the first index. * @param end - the last index. * @return the index of the element about which the array is partitioned. */ static int partition(int data[], int start, int end) { int pivot = data[end]; int i = (start - 1); for (int j = start; j <= end - 1; j++) { if (data[j] < pivot) { i++; swap(data[i], data[j]); } } swap(data[i + 1], data[end]); return (i + 1); } /** This function merges two subarrays into the original array. * @param data - the original array * @param first - a subarray * @param sizeFirst - the size of the first subarray * @param second - a subarray * @param sizeSecond - the size of the second subarray. */ 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 */ inline void SortUtil::bubbleSort(int data[], int size) { int i, j; for (i = 0; i < size-1; i++) for (j = 0; j < size-i-1; j++) if (data[j] > data[j+1]) swap(data[j], data[j+1]); } inline void SortUtil::selectionSort(int data[], int size) { int i, j, min_idx; for (i = 0; i < size-1; i++) { min_idx = i; for (j = i+1; j < size; j++) if (data[j] < data[min_idx]) min_idx = j; swap(data[min_idx], data[i]); } } inline 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; } } inline void SortUtil::quickSort(int data[], int start, int end) { if (start < end) { int pi = partition(data, start, end); quickSort(data, start, pi - 1); quickSort(data, pi + 1, end); } } static void arrayCopy(int src[], int srcPos, int dest[], int destPos, int length); static void merge(int data[], int first[],int sizeFirst, int second[], int sizeSecond); inline 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; }
SortUtil.h
#ifndef SORTUTIL_H #define SORTUTIL_H #include #include using namespace std; namespace SortUtil { /* Your Task: You will need to templatize these function prototypes so that they take arrays of any type. For now, they will work with int data. Do not templatize them until you get the program to work with int data. */ /** * This function sorts the items in an array into ascending order * using the bubble sort algorithm. * @param data - the array containing the data. * @param size - the number of elements in the array. */ void bubbleSort(int data[], int size); /** * This function sorts the items in an array into ascending order * using the selection sort algorithm. * @param data - the array containing the data. * @param size - the number of elements in the array. */ static void SortUtil::selectionSort(int data[], int size); /** * This function sorts the items in an array into ascending order * using the insertion sort algorithm. * @param data - the array containing the data. * @param size - the number of elements in the array. */ void insertionSort(int data[], int size); /** * This function sorts the items in an array into ascending order * using the merge sort algorithm. * @param data - the array containing the data. * @param start - the starting index. * @param end - the ending index. */ void quickSort(int data[], int start, int end); /** * This function sorts the items in an array into ascending order * using the merge sort algorithm. * @param array the array containing the data. * @param size the number of elements in the array. */ void mergeSort(int array[], int size); } #endif
SortProfiler,cpp
#include #include #include #include #include "ArrayUtil.cpp" #include "SortUtil.cpp" #include "ArrayUtil.h" 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)); /******************************************************** The code below is intended for debugging. Uncomment it to verify that all the code for the various sorting algorithms are working correctly. Comment out this section of the code again once you have verified that they work correctly. In each case a random array of size 50 is generated and the orignal and sorted arrays are printed. ********************************************************/ /* int *testArray = new int[50]; cout<<"***Testing Selection Sort***"<(elapsed).count(); start = high_resolution_clock::now(); SortUtil::bubbleSort(iArray2,size[i]); elapsed = high_resolution_clock::now() - start; bTime = duration_cast(elapsed).count(); start = high_resolution_clock::now(); SortUtil::quickSort(iArray3,0,size[i]-1); elapsed = high_resolution_clock::now() - start; qTime = duration_cast(elapsed).count(); cout<
ArrayUtil.cpp
#include #include #include #include #include #include "ArrayUtil.h" using namespace std; template void ArrayUtil::arrayCopy(T a[], int i, T b[],int j, int numElems) { /* for (int m = i; m < (i+numElems); m++) { b[j] = a[m]; j++; } */ memcpy(&b[j],&a[i],sizeof(T)*numElems); } template void ArrayUtil::genRandArray(T* data, int size) { srand(time(NULL)); T factor = 1; for (int i=0; i void ArrayUtil::genSortedArray(T* data, int size, bool ascending) { int i; T factor = 1; if (ascending) { for (i=0; i void ArrayUtil::printArray(T data[],int first,int last, int numPerLine) { for (int i = first; i <= last; i++) { cout<
ArrayUtil.h
#include #include #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 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 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 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 void printArray (T data[], int first, int last, int numPerLine); } #endif
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started