Question

Any help in the compiler error //Driver Program //***************** /** * * CSCI 241 Assignment 8...

Any help in the compiler error

//Driver Program

//*****************

/**

*

* CSCI 241 Assignment 8, Part 3

*

* Author: your name

* z-ID: your z-ID

* Date: due date of assignment

*

* This program builds, sorts and prints lists using the quicksort and

* merge sort algorithms.

*/

#include <iostream>

#include <iomanip>

#include <vector>

#include <string>

#include "sorts.h"

#include "quicksort.h"

#include "mergesort.h"

using std::cout;

using std::fixed;

using std::left;

using std::setprecision;

using std::string;

using std::vector;

// Data files

#define D1 "/home/fagent/Desktop/assign8/Part3/data8a.txt"

#define D2 "/home/fagent/Desktop/assign8/Part3/data8b.txt"

#define D3 "/home/fagent/Desktop/assign8/Part3/data8c.txt"

// Output formatting constants

#define INT_SZ 4 // width of integer

#define FLT_SZ 7 // width of floating-pt number

#define STR_SZ 12 // width of string

#define INT_LN 15 // no of integers on single line

#define FLT_LN 9 // no of floating-pt nums on single line

#define STR_LN 5 // no of strings on single line

int main()

{

vector<int> v1; // vector of integers

vector<float> v2; // vector of floating-pt nums

vector<string> v3; // vector of strings

// Print header message

cout << "*** CSCI 241: Assignment 8 - Output ***\n\n";

// sort and print first list

cout << "First list - ascending order:\n\n";

buildList(v1, D1);

quickSort(v1, &lessThan);

printList(v1, INT_SZ, INT_LN);

v1.clear();

// Problem

cout << "\nFirst list - descending order:\n\n";

buildList(v1, D1);

mergeSort(v1, &greaterThan);

printList(v1, INT_SZ, INT_LN);

// Sort and print second list

cout << fixed << setprecision(2);

cout << "\nSecond list - descending order:\n\n";

buildList(v2, D2);

quickSort(v2, &greaterThan);

printList(v2, FLT_SZ, FLT_LN);

v2.clear();

//Problem

cout << "\nSecond list - ascending order:\n\n";

buildList(v2, D2);

mergeSort(v2, &lessThan);

printList(v2, FLT_SZ, FLT_LN);

// Sort and print third list

cout << left;

cout << "\nThird list - ascending order:\n\n";

buildList(v3, D3);

quickSort(v3, &lessThan);

printList(v3, STR_SZ, STR_LN);

v3.clear();

//Problem

cout << "\nThird list - descending order:\n\n";

buildList(v3, D3);

mergeSort(v3, &greaterThan);

printList(v3, STR_SZ, STR_LN);

// print termination message

cout << "\n*** End of program execution ***\n";

return 0;

}

//Sort.h

//**************

#ifndef SORTS_H

#define SORTS_H

#include<vector>

#include<iostream>

#include<fstream>

#include<iomanip>

#include<string>

using namespace std;

template <class T> void buildList(vector<T>& set, const char* fileName)

{

ifstream in;

//open file for reading

in.open(fileName);

//check if file is open

if (!in)

{

cout << "unable to open the file " << fileName << endl;

return;

}

//read till eof

T num;

while (!in.eof())

{

in >> num;

set.push_back(num);

}

}

//Second Templates

template <class T> void printList(const vector<T>& set, int itemWidth, int numPerLine)

{

int k=0;

for (int i = 0; i < static_cast<int> (set.size()-1); i++)

{

if (numPerLine == k)

{

cout << endl;

k=0;

}

    k++;

cout << setw(itemWidth) << set[i] << " ";

}

}

// Part two templates

template <class T> bool lessThan(const T& item1, const T& item2)

{

    if (item1 < item2)

        return true;

    else

        return false;

}

template <class T> bool greaterThan(const T& item1, const T& item2)

{

    if (item1 > item2)

return true;

else

return false;

}

#endif

//Quicksort.h

//*****************

#ifndef QUICKSORT_H

#define QUICKSORT_H

#include<vector>

#include<iostream>

#include<fstream>

#include<iomanip>

#include<string>

using namespace std;

template <class T> int partition(vector<T> &set, int start, int end, bool (*compare)(const T &, const T &))

{

    int pivotIndex, mid;

    T pivotValue;

    mid = (start + end) / 2;

    // Swap elements start and mid of the vector

    iter_swap(set.begin() + start, set.begin() + mid);

    pivotIndex = start;

    pivotValue = set[start];

    for (int scan = start + 1; scan <= end; scan++)

        {

        if (compare(set[scan], pivotValue))

            {

                ++pivotIndex;

                // Swap elements pivotIndex and scan of the vector

                iter_swap(set.begin() + pivotIndex, set.begin() + scan);

            }

        }

    // Swap elements start and pivotIndex of the vector

    iter_swap(set.begin() + pivotIndex, set.begin() + start);

    return pivotIndex;

}

template <class T> void quickSort(vector<T> &set, int start, int end, bool (*compare)(const T &, const T &))

{

    int pivotPoint;

    if (start < end)

        {

            pivotPoint = partition(set, start, end, compare); // Get the pivot point

            quickSort(set, start, pivotPoint - 1, compare); // Sort first sublist

            quickSort(set, pivotPoint + 1, end, compare); // Sort second sublist

        }

}

template <class T> void quickSort(vector<T> &set, bool (*compare)(const T &, const T &))

{

    quickSort(set, 0, set.size()-2 , compare);

}

#endif

//MergeSort.h

//**************

#ifndef MERGESORT_H

#define MERGESORT_H

#include<vector>

#include<iostream>

#include<fstream>

#include<iomanip>

#include<string>

using namespace std;





//Template 1

template <class T> void mergeSort(vector<T>& set, int (*compare)(const T&, const T&))

{

mergeSort(set, 0, set.size()-1, compare);

}

//Template 2

template <class T> void mergeSort(vector<T>& set, int start, int end, int (*compare)(const T&, const T&))

{

//friend template <class T> bool lessThan(const T& item1, const T& item2);

// friend template <class T> bool greaterThan(const T& item1, const T& item2);

    

int mid;

if (start < end)

    {

        mid = (start + end) / 2;

        // Divide and conquer

        mergeSort(set, start, mid, compare);

        mergeSort(set, mid+1, end, compare);

        // Combine

        merge(set, start, mid, end, compare);

    }

}

//Template 3

template <class T> void merge(vector<T>& set, int start, int mid, int end, int (*compare)(const T&, const T&))

{

// Create temporary vector to hold merged subvectors

    vector<T> temp(end - start + 1);

    int i = start; // Subscript for start of left sorted subvector

    int j = mid+1; // Subscript for start of right sorted subvector

    int k = 0; // Subscript for start of merged vector

    // While not at the end of either subvector

    while (i <= mid && j <= end)

        {

            if (compare(set[j], set[i]))

            {

            //Copy element j of set into element k of temp

            temp[k] = set[j];

            //Add one to j

            j = j+1;

            //Add one to k

            k = k + 1;

            }

            else

            {

            //Copy element i of set into element k of temp

            temp[k] = set[i];

            //Add one to i

            i = i + 1;

            //Add one to k

            k = k + 1;

            }

        }

    // Copy over any remaining elements of left subvector

    while (i <= mid)

        {

        //Copy element i of set into element k of temp

        temp[k] = set[i];

        //Add one to i

        i = i + 1;

        //Add one to k

        k = k + 1;

        }

    // Copy over any remaining elements of right subvector

    while (j <= end)

        {

        //Copy element j of set into element k of temp

        temp[k] = set[j];

        //Add one to j

        j = j+1;

        //Add one to k

        k = k + 1;

        }

    // Copy merged elements back into original vector

    for (i = 0, j = start; j <= end; i++, j++){

    //Copy element i of temp into element j of set

    set[j] = temp[i];

    }

}

#endif

1 0
Add a comment Improve this question Transcribed image text
Answer #1

If you have any doubts, please give me comment...

mergesort.h

#include <vector>

using namespace std;

template <class T>

void merge(vector<T> &set, int start, int mid, int end,

bool (*compare)(const T &, const T &))

{

// Create temporary vector to hold merged subvectors

vector<T> temp(end - start + 1);

int i = start; // Subscript for start of left sorted subvector

int j = mid + 1; // Subscript for start of right sorted subvector

int k = 0; // Subscript for start of merged vector

// While not at the end of either subvector

while (i <= mid && j <= end)

{

if (compare(set[i], set[j]))

{

// Copy element i of set into element k of temp

// Add one to i

// Add one to k

temp[k] = set[i];

i++;

k++;

}

else

{

// Copy element j of set into element k of temp Add one to j Add one to k

temp[k] = set[j];

j++;

k++;

}

}

// Copy over any remaining elements of left subvector

while (i <= mid)

{

// Copy element i of set into element k of temp Add one to i Add one to k

temp[k] = set[i];

k++;

i++;

}

// Copy over any remaining elements of right subvector

while (j <= end)

{

// Copy element j of set into element k of temp Add one to j Add one to k

temp[k] = set[j];

k++;

j++;

}

// Copy merged elements back into original vector

for (i = 0, j = start; j <= end; ++i, ++j)

{

// Copy element i of temp into element j of set

set[j] = temp[i];

}

}

template <class T>

void mergeSort(vector<T> &set, int start, int end,

bool (*compare)(const T &, const T &))

{

int mid;

if (start < end)

{

mid = (start + end) / 2;

// Divide and conquer

mergeSort(set, start, mid, compare);

mergeSort(set, mid + 1, end, compare);

// Combine

merge(set, start, mid, end, compare);

}

}

template <class T>

void mergeSort(vector<T> &set, bool (*compare)(const T &, const T &))

{

mergeSort(set, 0, set.size() - 1, compare);

}

Add a comment
Know the answer?
Add Answer to:
Any help in the compiler error //Driver Program //***************** /** * * CSCI 241 Assignment 8...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Merge Sort: Time Complexity: O(n log(n)) #include "stdafx.h" #include <iostream> #include <time.h> #include <stdlib.h> using namespace...

    Merge Sort: Time Complexity: O(n log(n)) #include "stdafx.h" #include <iostream> #include <time.h> #include <stdlib.h> using namespace std; void combine(int *a, int low, int high, int mid) {        int i, j, k, c[100000];        i = low;        k = low;        j = mid + 1;        while (i <= mid && j <= high)        {               if (a[i] < a[j])               {                      c[k] = a[i];                      k++;                      i++;               }               else               {                     ...

  • C++. Difficulty with quickSort function. Code will not run quickSort function. The code I'm having trouble...

    C++. Difficulty with quickSort function. Code will not run quickSort function. The code I'm having trouble with is in bold. -------------------------------------------------------------------------------------------------driverProgram.cpp #include #include #include #include #include "quickSort.cpp" using namespace std; int main() { const int MIN_SIZE = 4; //Array size const int SIZE = 25; int theArray[SIZE] = {11, 22, 33, 44, 55, 66, 77, 88, 99, 12, 13, 14, 15, 16, 17, 18, 19, 18, 19, 20, 21, 22, 23, 24, 25}; cout << "List of 25 items: ";...

  • Practical 5: Write a program that implements several sorting algorithms, and use it to demonstrate the comparative perfo...

    Practical 5: Write a program that implements several sorting algorithms, and use it to demonstrate the comparative performance of the algorithms for a variety of data sets. Need Help With this Sorting Algorithm task for C++ Base Code for sorting.cpp is given. The header file is not included in this. Help would be much appreciated as I have not started on this due to personal reasons #include <cstdlib> #include <iostream> #include <getopt.h> using namespace std; long compares; // for counting...

  • there show an error in sample.cpp file that more than one instance of overloaded function find...

    there show an error in sample.cpp file that more than one instance of overloaded function find matches the argument list. can you please fix it. and rewrite the program and debug. thanks. I also wrote error below on which line in sample.cpp. it shows on find. #include #include #include "node1.cpp" using namespace main_savitch_5; // node1.h #ifndef MAIN_SAVITCH_NODE1_H #define MAIN_SAVITCH_NODE1_H #include <string> namespace main_savitch_5 {    template<class item>    class node    {    public:        typedef item value_type;   ...

  • I want to compare the runtimes and swap operations times among quick Sort, selection Sort and...

    I want to compare the runtimes and swap operations times among quick Sort, selection Sort and shell Sort here is my code: But when I create a 1000,000 size array, I can't get the result of the operations times and runtime. what's wrong with my code? and I also want to copy the array. Because I want to use same array for three sort. And for the shell Sort, I haven't learn it in my class. Can anyone help me...

  • Please give a output Linux/Ubuntu and how to run it (compile) this program ,my assigment is below...

    Please give a output Linux/Ubuntu and how to run it (compile) this program ,my assigment is below : : Merge Sort algorithm using 2 processes a.) Define an integer array of 100 integers. Populate this array with random numbers. You can use int rand(void); function. Do not forget to initialize this function. You will sort the numbers in the array using merge-sort algorithm. In merge sort algorithm the half of the array will be sorted by one process and second...

  • I need to complete the C++ program for hotel reservation. In the main cpp you have...

    I need to complete the C++ program for hotel reservation. In the main cpp you have to display the hotel information from the hotel.txt based on customer preferences(which can be taken from the User.txt) For example, if the customer's budget is 200$, then only the hotels with prices below that amount should be displayed. Then the user has an option to choose which one to reserve for the particular time(which also can be found in the user.txt) The last two...

  • vector.h: #ifndef VECTOR_H #define VECTOR_H #include <algorithm> #include <iostream> #include <cassert> template <typename T> class Vector...

    vector.h: #ifndef VECTOR_H #define VECTOR_H #include <algorithm> #include <iostream> #include <cassert> template <typename T> class Vector {     public:         Vector( int initsize = 0 )         : theSize( initsize ),          theCapacity( initsize + SPARE_CAPACITY )         { objects = new T[ theCapacity ]; }         Vector( const Vector & rhs )         : theSize( rhs.theSize),          theCapacity( rhs.theCapacity ), objects( 0 )         {             objects = new T[ theCapacity ];             for( int k = 0; k < theSize; ++k)                 objects[ k ] = rhs.objects[ k...

  • This is an assignment for my algorithm class which I have written the code partially and...

    This is an assignment for my algorithm class which I have written the code partially and only need to complete it by adding a time function that calculates the average running time. We could use any programming language we want so I am using C++. I am including the instruction and my partial code below. Thank you! Implement linearSearch(a,key) and binarySearch( a,key)functions. Part A.In this part we will calculate theaverage-case running time of each function.1.Request the user to enter a...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT