Question

In: Computer Science

Use the provided BingoBall.h and Set.h files to implement the Set.cpp file. //File: BingoBall.h #ifndef BINGOBALL_H...

Use the provided BingoBall.h and Set.h files to implement the Set.cpp file.

//File: BingoBall.h

#ifndef BINGOBALL_H
#define   BINGOBALL_H

#include <iostream>

class BingoBall {
public:
   BingoBall():letter{'a'}, number{0} {}
   BingoBall(char let, int num) :
       letter{ let }, number{ num } {}
   char getChar() const { return letter; }
   int getNumber() const { return number; }
   //overload == operator
   bool operator==(BingoBall &b) const { return (number == b.getNumber() && letter == b.getChar()); }
private:
   char letter;
   int number;
};

inline std::ostream& operator<<(std::ostream& out, const BingoBall& b) {
   out << b.getChar() << "-" << b.getNumber();
   return out;
}

#endif   /* BINGOBALL_H */

// File: Set.h


#ifndef SET_H
#define SET_H

#include <string>

using namespace std;

template <typename T>
class Set{
public:
   //creates an initially empy set
   Set();

   //destroys the set, deallocating any freed memory space if appropriate
   ~Set();

   //adds the element to the set (if it is not already in there)
   //returns false if element is not in universe, true otherwise
   bool addElement(const T& elem);

   //removes the specified element from set and returns the removed element, returns
   //nullpointer if the element was not in the set  
   T removeElement(const T& elem);

   //removes a random element and returns it, returns nullpointer if the set was empty initially
   T removeRandom();

   //returns true if the given element is in the set, false otherwise
   bool isIn(const T& elem) const;

   //returns true if the set is empty, false otherwise
   bool empty() const;

   //returns number of elements in the set
   int size() const;
  
private:
   //class level variable for holding an array based set implementation
   T* setArray;


    //the number of elements in the set
   int numEntries;
  
   //used for array based implementations to hold the maximum size of  
   //the array (do we need this? why or why not?)
   int arraySize;
};

// displays the entire set, solely for debugging purposes
template <typename T>
std::ostream& operator<<(std::ostream& out, Set<T>& p);

#endif /* SET_H */

Solutions

Expert Solution

Summary :

Provided notes , code and output.

Notes :

Implemented the set assuming that the Template type is a pointer ( since it has to return NULL for remove element ).

Further array size is doubled when new element is inserted while the set size is full .

The "<<" operator is implemented by removing all the elements from the set and copying to temp set and re-adding them .

And last all functions are tested in the main method with examples.

######################## Code ######################

#include <iostream>
#include <stdlib.h>
#include <time.h> 
#include "Set.h"

using namespace std ;

template<typename T>
Set<T>::Set()
{
        // initialize the size to 10 
        arraySize = 10 ;
        numEntries = 0 ;
        setArray = new T[10];
        srand (time(NULL));
}

template<typename T>
Set<T>::~Set()
{
        // delete the allocated array using delete[]
        for( int i=0 ; i < numEntries ; i++ )
                delete setArray[i];
        
        delete[] setArray ;
}


template <typename T>
void Set<T>::_doubleSize()
{
        T* tmpArray = new T[arraySize] ;
        
        for( int i=0 ; i < this->arraySize ; i++ )
                tmpArray[i] = setArray[i];
        
        delete[] setArray;
        
        setArray = new T[2*arraySize] ;
        
        for( int j =0 ; j < arraySize ; j++ )
                setArray[j] = tmpArray[j];
        
        arraySize = 2 * arraySize;
        
}
template <typename T>
bool Set<T>::addElement(const T& elem)
{
        if ( isIn( elem) )
        {
                cout << " Element found hence not inserting \n";
                return false ;
        } else 
        {
                if (!( numEntries < arraySize ) )
                {
                        
                        cout << " Set is full \n";
                        this->_doubleSize() ;
                }
                //setArray[numEntries]  ;
                setArray[numEntries] = (T ) malloc( sizeof( elem ) ) ;
                
                *setArray[numEntries] = *elem;
                numEntries++ ;
                return true ;

        }
        
        return false;
}



template <typename T>
T Set<T>::removeElement(const T& elem)
{
        int i =0;
        bool flag = false ;
        
        T ptr ;
        for( ; i < numEntries ; i++ )
        {
                if ( *elem == *setArray[i] )
                {  
                        flag = true ;
                        ptr = setArray[i];
                        break ;
                }
        }
        
        if ( flag ) 
        {
                for ( int j = i ; j < numEntries -1  ; j++ ) 
                {
                        setArray[j] = setArray[j+1] ;
                }
                numEntries = numEntries - 1;
                return ptr;
        }
        
        return NULL;
        
}


template <typename T>
T Set<T>::removeRandom()
{
        if ( ! this->empty() )
        {
                int rIdx = rand() % numEntries  ;
                //cout << " Random Indx : " << rIdx << "\n";
                T elm = setArray[rIdx];
                for ( int i= rIdx ; i < numEntries - 1 ; i++ )
                        setArray[i] = setArray[i + 1 ] ;
                
                numEntries = numEntries - 1;
                return elm;
                
        } else 
                return NULL;
                
}

template <typename T>
bool Set<T>::isIn(const T& elem) const
{
        
        int i =0;
        
        for( ; i < numEntries ; i++ )
        {
                if ( *elem == *setArray[i] )
                {  
                        return true ;
                        break ;
                }
        }
        
        return false;
}

template <typename T>
bool Set<T>::empty() const
{
        return numEntries == 0 ;
}

template<typename T>
void Set<T>::_print_() const
{
        cout << "\nSetArray : ";
        for ( int i=0 ; i < numEntries ; i++ )
                cout << *setArray[i] << ", " ;
        
        cout <<"\n";
        
}

template <typename T>
int Set<T>::size() const
{
        return numEntries;
}

template <typename T>
std::ostream& operator<<(std::ostream& out, Set<T>& p)
{
        Set<T> tmp ;
        T tmpElem;
        out << "\n Number of Entries : " << p.size() << "\n Entry List : " ;
        
        int sz = p.size();
        for(int i =0 ; i < sz ; i++ )
        {
                tmpElem = p.removeRandom();
                tmp.addElement(tmpElem);
                out << *tmpElem  << ", " ;
                delete tmpElem;
        }
        
        
        sz = tmp.size();
        for(int i =0 ; i < sz ; i++ )
        {
                tmpElem = tmp.removeRandom();
                p.addElement(tmpElem);
        }
        out << " \n";
        
        return out ;
}


int main()
{
        Set<int *> intList ;
        int x , x1 , x2 ;
        
        for( int i=0 ; i < 15 ; i++ ) 
        {
                x = rand() % 1000 + 1;
                cout << " Trying to Insert : " << x << " Successful : " << 
                intList.addElement(&x) << "\n";
                if ( i == 5 )
                        x1 = x ;
        }
        
        cout << intList << "\n";
        
        cout << " Trying to Insert duplicate : " << x1  <<  ", Successful - " << intList.addElement( &x1 ) << "\n";
        
        int* x4 = intList.removeElement(&x1) ;
        cout << " Remove the element  : " << x1 << ", Successful - " <<  *x4  << "\n";
        if ( x4 != NULL )
                delete x4 ;
        
        if ( intList.removeElement(&x1) != NULL )
        {
                cout << " Found " << x1 << "\n";
        }else 
                cout << "Got NULL \n";
        
        
        
        cout << "\n Set Empty : " << intList.empty() << "\n";
        cout << intList;
        
        
        cout << "Removing Random : " << intList.size() << "\n";;
        int j = 0 , sz = intList.size();
        for (  j = 0 ; j < sz  ; j++ )
        {       
                int * x5 = intList.removeRandom() ;
                cout << *x5 << "\n";
                delete x5;
        }

        cout << "\n Set Empty : " << intList.empty() << "\n";
        
}

############### Output ###############


Related Solutions

Please use C++. You will be provided with two files. The first file (accounts.txt) will contain...
Please use C++. You will be provided with two files. The first file (accounts.txt) will contain account numbers (10 digits) along with the account type (L:Loan and S:Savings) and their current balance. The second file (transactions.txt) will contain transactions on the accounts. It will specifically include the account number, an indication if it is a withdrawal/deposit and an amount. Both files will be pipe delimited (|) and their format will be the following: accounts.txt : File with bank account info...
Use the functions.h header file with your program (please write in C code): #ifndef FUNCTIONS_H #define...
Use the functions.h header file with your program (please write in C code): #ifndef FUNCTIONS_H #define FUNCTIONS_H typedef struct MyStruct { int value; char name[ 100 ]; } MyStruct; void sortArray( MyStruct*, int ); void printArray( MyStruct*, int ); #endif Create a source file named functions.c with the following: A sorting function named sortArray. It takes an array of MyStruct's and the length of that array. It returns nothing. You can use any of the sorting algorithms, you would like...
C programming A small company provided you three files. 1) emp.txt : this file contains list...
C programming A small company provided you three files. 1) emp.txt : this file contains list of employees where each line represents data of an employee. An employee has an id (String max length 20), last name (string max length 100), and salary (int). See the example emp.txt file. 2) dept.txt: This text file contains list of departments of the employees. Each line of the file contains an employee id (string max length 20) and department name for that employee...
This is the header file: #ifndef __HW2_H__ #define __HW2_H__ // Previous two lines are the start...
This is the header file: #ifndef __HW2_H__ #define __HW2_H__ // Previous two lines are the start of the marco guard // Try not to change this file #include <iostream> #include <cmath> using std::cout; using std::endl; using std::istream; using std::ostream; class Point { 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;   ...
Code needed in C++, make changes to the file provided (18-1, has 3 files) Chapter 18...
Code needed in C++, make changes to the file provided (18-1, has 3 files) Chapter 18 Stacks and Queues ----------------------------------------------------------------------------------------------------- capacity is just 5 1. push 6 numbers on the stack 2. catch the overflow error in a catch block 3. pop one element, which means your capacity is now down to 4 4. push the element that was rejected earlier 5. verify your entire stack by popping to show the new numbers. IntStack.h #include <memory> using namespace std; class...
Use the Heap class provided to implement a sort routine in a Main class where the...
Use the Heap class provided to implement a sort routine in a Main class where the user enters a series of values, each value is then pushed onto a heap, then the values are printed out in ascending order. public class Heap { public static final int SIZE = 1025; public Heap() { elt = new Element[SIZE]; lastLoc = 0; } public void push(String k, Object o) { if (!fullCheck()) { lastLoc++; elt[lastLoc] = new Element(k,o); int loc = lastLoc;...
Double Linked Lists: Implement the PutItem and DeleteItem function // ItemType.h #ifndef ITEMTYPE_H #define ITEMTYPE_H enum...
Double Linked Lists: Implement the PutItem and DeleteItem function // ItemType.h #ifndef ITEMTYPE_H #define ITEMTYPE_H enum RelationType { LESS, GREATER, EQUAL}; class ItemType { public:     ItemType();     void Initialize(int number);     int getValue() const;     RelationType ComparedTo(ItemType); private:     int value; }; #endif // ITEMTYPE_H // ItemType.cpp #include "ItemType.h" ItemType::ItemType() {     value = 0; } void ItemType::Initialize(int number) {     value = number; } RelationType ItemType::ComparedTo(ItemType otherItem) {         if(value < otherItem.value)             return LESS;         else if...
Which of the following files is a temporary file? a. transaction file b. master file c. reference file d. none of the above
Which of the following files is a temporary file? a. transaction fileb. master filec. reference filed. none of the above
JAVA: You're given 3 files. Demo.java, SampleInterace.java, and OverflowException.java. Use your Demo class to implement SampleInterface,...
JAVA: You're given 3 files. Demo.java, SampleInterace.java, and OverflowException.java. Use your Demo class to implement SampleInterface, and the OverflowException class should handle any errors that may come from the addNum method. Demo.java: public class Demo implements SampleInterface { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated constructor stub } @Override public void addNum(int value) throws OverflowException { // TODO Auto-generated method stub } @Override public void removeNum(int value) { // TODO Auto-generated method stub...
For this assignment, you will use the provided database in the Unit 5 script file. You...
For this assignment, you will use the provided database in the Unit 5 script file. You will add statements to this file to query the database tables. For your reference, below is a screenshot of the enhanced entity relationship diagram (ERD) as a reference when you write your queries. With the data types listed in the diagram, this will help you identify the types of operators that you can use for particular queries. Use paiza.io for MySQL to execute and...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT