Question

For this assignment you will create a stand-alone function, sort, that takes an UList<T> parameter and...

For this assignment you will create a stand-alone function, sort, that takes an UList<T> parameter and sorts it using the Bubble Sort algorithm. This sort function is to be placed in a file named sortBS.h and UList should be in a file named UList.h. The UML diagram for UList is given below:

UList<T>

#items: vector<T>

+UList(size_t=10)

+insert(const T&): void

+erase(const T&): bool

+find(const T&) const: bool

+size() const: size_t

+empty() const: bool

+friend operator << (ostream&, const UList<U>&): ostream&

+friend sort (UList<U>&): void

Submit sortBS.h and UList.hprior to the due date/time using the following form:

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

The required function sort() is as follows:

// Define the function to sort the list using

// bubble sort.

template <class U>

void sort (UList<U>& myList)

{

    // Start the loop to traverse the list.

    for (int i = 0; i < myList.size()-1; i++)

    {

        for (int j = 0; j < myList.size()-i-1; j++)

        {

            // Check if the values are in order or not.

            if (myList.items.at(j) > myList.items.at(j+1))

            {

                // Use a temporary variable to swap

                // the elements if they are not in order.

                U temp = myList.items.at(j);

                myList.items.at(j) = myList.items.at(j+1);

                myList.items.at(j+1) = temp;

            }

        }

    }

}

The complete executable code along with all the header files and main function is shown below:

Program screenshots:

UList.h

#ifndef ULIST_H #define ULIST_H // Include the required header files. #include <vector> %#3include <iostream> // Use the stan// Define the function to remove an element from // list. bool erase (const T& item) { // Call the function to check if the e// Define the function to check if the element // is present in the list or not. bool find (const T& item) const { // Start t// Define function to overload the << operator. template <class U> ostream& operator << (ostream& out, const UList <U>& myLis

sortBS.h

#ifndef SORTBS_H #define SORTBS_H // Include the required header files. %#3include UList.h // Define the function to sort t

main.cpp

// Include the required header files. %#3include <iostream> %#3include UList.h #include sortBS.h // Use the standard name

Sample output:

Initial list: 40 20 30 10 Sorted list: 10 20 30 40

Code to copy:

UList.h

#ifndef ULIST_H

#define ULIST_H

// Include the required header files.

#include <vector>

#include <iostream>

// Use the standard namespace.

using namespace std;

// Define the template.

template <class T>

class UList

{

    protected:

    vector<T> items;

    public:

    

    // Define the constructor to set the size of the vector.

    UList <T>(size_t size = 10)

    {

        items.reserve(size);

    }

    // Define the function to insert an element in the list.

    void insert (const T& item)

    {

        items.push_back(item);

    }

    // Define the function to remove an element from

    // list.

    bool erase (const T& item)

    {

        // Call the function to check if the element

        // is present in the list or not.

        if (find(item) == true)

        {

            int index = 0;

            // Start the loop to find the index of the

            // element to be removed.

            for (int i = 0; i < items.size(); i++)

            {

                if (items.at(i) == item)

                {

                    index = i;

                    break;

                }

            }

            // Call the function to remove the element.

            items.erase(index);

            // Return true.

            return true;

        }

        // Return false if the element is not deleted.

        else

        {

            return false;

        }

    }

    // Define the function to check if the element

    // is present in the list or not.

    bool find (const T& item) const

    {

        // Start the loop to traverse the list.

        for (int i = 0; i < items.size(); i++)

        {

            if (items.at(i) == item)

            {

                // Return true if the element is found.

                return true;

            }

        }

        // Otherwise, return false.

        return false;

    }

    // Define the function to return the size of the list.

    size_t size() const

    {

        return items.size();

    }

    // Define the function to check if the list is

    // empty or not.

    bool empty() const

    {

        return items.empty();

    }

    // Declare the friend functions with template.

    template <class U>

    friend ostream& operator << (ostream& out, const UList<U>& myList);

    template <class U>

    friend void sort (UList <U>&);

};

// Define function to overload the << operator.

template <class U>

ostream& operator << (ostream& out, const UList <U>& myList)

{

    // Start the loop to travese the list.

    for(int i = 0; i < myList.items.size(); i++)

    {

        // Add the value to the object.

        out << myList.items.at(i) << " ";

    }

    // Return the object.

    return out;

}

#endif

sortBS.h

#ifndef SORTBS_H

#define SORTBS_H

// Include the required header files.

#include "UList.h"

// Define the function to sort the list using

// bubble sort.

template <class U>

void sort (UList<U>& myList)

{

    // Start the loop to traverse the list.

    for (int i = 0; i < myList.size()-1; i++)

    {

        for (int j = 0; j < myList.size()-i-1; j++)

        {

            // Check if the values are in order or not.

            if (myList.items.at(j) > myList.items.at(j+1))

            {

                // Use a temporary variable to swap

                // the elements if they are not in order.

                U temp = myList.items.at(j);

                myList.items.at(j) = myList.items.at(j+1);

                myList.items.at(j+1) = temp;

            }

        }

    }

}

#endif

main.cpp

// Include the required header files.

#include <iostream>

#include "UList.h"

#include "sortBS.h"

// Use the standard namespace.

using namespace std;

// Define the main() function.

int main()

{

    // Create an object of the class UList.

    UList<int> myList;

    // Enter few values in the list.

    myList.insert(40);

    myList.insert(20);

    myList.insert(30);

    myList.insert(10);

    // Display the initial list using the << operator.

    cout << "Initial list: ";

    cout << myList << endl;

    // Call the function to sort the list using

    // bubble sort.

    sort(myList);

    // Display the sorted list.

    cout << "Sorted list: ";

    cout << myList << endl;

    // Return 0 and exit the code.

    return 0;

}

Add a comment
Know the answer?
Add Answer to:
For this assignment you will create a stand-alone function, sort, that takes an UList<T> parameter and...
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
  • 17. Write a non-member function called centroid(param) that takes a static array of points and the...

    17. Write a non-member function called centroid(param) that takes a static array of points and the size of the array and return the centroid of the array of points. If there is no center, return the origins private: double x, y, z public: /Constructors Point(); Point(double inX, double inY, double inZ = 0); Point(const Point& inPt); / Get Functions double getX() const; double getY) const; double getZ) const; Set Functions void setX(double inX); void setY(double inY); void setZ(double inZ); void...

  • In this assignment, you will implement a sort method on singly-linked and doubly-linked lists. Implement the...

    In this assignment, you will implement a sort method on singly-linked and doubly-linked lists. Implement the following sort member function on a singly-linked list: void sort(bool(*comp)(const T &, const T &) = defaultCompare); Implement the following sort member function on a doubly-linked list: void sort(bool(*comp)(const T &, const T &) = defaultCompare); The sort(…) methods take as a parameter a comparator function, having a default assignment of defaultCompare, a static function defined as follows: template <typename T> static bool defaultCompare(const...

  • Implement the friend function to overload the stream insertion operator (<<) so that the value contained...

    Implement the friend function to overload the stream insertion operator (<<) so that the value contained in the PlainBox object can be output using the << operator. The prototype is given in the header file. plainbox.h typedef double T; class PlainBox{ private: Titem; public: PlainBox(): PlainBox(const T& theltem): void setItem(const T& itemltem); T getitem() const; friend ostream& operator<<(ostream & out, const PlainBox & theBox); bool operator<(const PlainBox & theBox);

  • C++ Create a static implementation of a set. Add the efficiency of each function to the...

    C++ Create a static implementation of a set. Add the efficiency of each function to the documentation in the header file. Use test_set.cpp as your test program. Header: /************************************ This class models a mathematical set. */ #ifndef _SET_H #define _SET_H #include <cstdlib> #include <iostream> class set { public: typedef int value_type; typedef std::size_t size_type; static const size_type CAPACITY = 30; set(); // postcondition: empty set has been created void insert (const value_type& entry); // precondition: if entry is not in...

  • I need help with the code below. It is a C program, NOT C++. It can...

    I need help with the code below. It is a C program, NOT C++. It can only include '.h' libraries. I believe the program is in C++, but it must be a C program. Please help. // // main.c // float_stack_class_c_9_29 // // /* Given the API for a (fixed size), floating point stack class, write the code to create a stack class (in C). */ #include #include #include #include header file to read and print the output on console...

  • C++ Assignment Project 1 - NodeList Building upon the the ListNode/List code I would like you...

    C++ Assignment Project 1 - NodeList Building upon the the ListNode/List code I would like you to extend the interface of a list to have these member functions as well. struct ListNode { int element; ListNode *next; } Write a function to concatenate two linked lists. Given lists l1 = (2, 3, 1)and l2 = (4, 5), after return from l1.concatenate(l2)the list l1should be changed to be l1 = (2, 3, 1, 4, 5). Your function should not change l2and...

  • Objectives You will implement and test a class called MyString. Each MyString object keeps track ...

    Objectives You will implement and test a class called MyString. Each MyString object keeps track of a sequence of characters, similar to the standard C++ string class but with fewer operations. The objectives of this programming assignment are as follows. Ensure that you can write a class that uses dynamic memory to store a sequence whose length is unspecified. (Keep in mind that if you were actually writing a program that needs a string, you would use the C++ standard...

  • C++ Assignment Project 1 - NodeList Building upon the the ListNode/List code I would like you to extend the interface of...

    C++ Assignment Project 1 - NodeList Building upon the the ListNode/List code I would like you to extend the interface of a list to have these member functions as well. struct ListNode { int element; ListNode *next; } Write a function to concatenate two linked lists. Given lists l1 = (2, 3, 1)and l2 = (4, 5), after return from l1.concatenate(l2)the list l1should be changed to be l1 = (2, 3, 1, 4, 5). Your function should not change l2and...

  • BACKGROUND Movie Review sites collect reviews and will often provide some sort of average review to...

    BACKGROUND Movie Review sites collect reviews and will often provide some sort of average review to sort movies by their quality. In this assignment, you will collect a list of movies and then a list of reviews for each movie. You will then take a simple average (total of reviews divided by number of reviews) for each movie and output a sorted list of movies with their average review. Here you are provided the shell of a program and asked...

  • 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...

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