Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Some of the attributes of a state in the United States are its name, capital, ar

ID: 3683998 • Letter: S

Question

Some of the attributes of a state in the United States are its name, capital, area, year of admission to the union, and the order of admission to the union.

Task A: Implement (define the methods) the class stateData (inline with the class definition, stateData.h, to keep track of the information for a state. The stateData class includes appropriate functions to manipulate the state’s data, such as the functionssetStateInfo, getStateInfo, and so on. Also, overload the relational operators to compare two states by their name. For easy input and output, overload the stream operators, << and>>.

Task B: In the main driver we are using the class hashT as described in the section, ‘‘Hashing: Implementation Using Quadratic Probing,’’ which uses quadratic probing to resolve collision, to create a hash table to keep track of each state’s information. Use the state’s name as the key to determine the hash address. You may assume that a state’s name is a string of no more than 15 characters.

Complete the provided main driver to test your program that searches for and removes certain states from the hash table. Some function you will need to call. See the comments of the main driver file for specifics.

Add and use the following hash function, to the main driver, to determine the hash address of an item:

int hashFunc(string name)
{
int i, sum;
int len;
i = 0;
sum = 0;
len = name.length();
for (int k = 0; k < 15 - len; k++)
name = name + ' ';
for (int k = 0; k < 5; k++)
{
sum = sum + static_cast<int>(name[i]) * 128 * 128 +

static_cast<int>(name[i + 1]) * 128 +

static_cast<int>(name[i + 2]);
i = i + 3;
}
return sum % HTSize;
}

----------------------

Download and use the hashT, and the starter files for statedata and main Driver found in this

https://www.mediafire.com/?a4bdip7n7po1a3n

----------------------

Please help me answer the question above, and fill in the code in main test driver.

Explanation / Answer

main.cpp

#include <iostream>
#include <cassert>
#include <fstream>
#include "orderedArrayListType.h"
#include "hashT.h"
#include "stateData.h"


using namespace std;

void part2();

int main() {
   part2();
};


void part2() {
   cout << " ============================|Problem 2|======================================= " << endl;
  
   //create hash
   hashT<stateData> stateHash(101);

   cout << "Opening file..." << endl;
   //opening file
   ifstream infile;
   infile.open("state_data.txt");
   assert(infile.is_open());
  
   cout << "Creating hash from file..." << endl;
   //using file as input to supply our hash with states
   while (infile){
       stateData newState;

       infile >> newState;

       int stateIndex = stateHash.hashFunc(newState.getName()); //creating index using hash function

       stateHash.insert(stateIndex, newState); //add to hash
   }

   infile.close(); // close file

   //Searching for a state
   bool found1;
   int hashIndex;
   string stateName;
   stateData searchState;

   cout << "Search the hash, Type the name of a state: ";
   cin >> stateName;

   hashIndex = stateHash.hashFunc(stateName);
   searchState = stateData(stateName);
  
   stateHash.search(hashIndex, searchState, found1);
   if (found1 && stateHash.isItemAtEqual(hashIndex, searchState))
   {
       cout << "Success!" << endl;
       stateHash.retrieve(hashIndex, searchState);
       cout << searchState << endl;
   }
   else {
       cout << "Not Found!" << endl;
   }


   cout << "REMOVING STATE" << endl;
   stateHash.remove(hashIndex, searchState);
   bool found2;
   stateHash.search(hashIndex, searchState, found2);
   if (found2 && stateHash.isItemAtEqual(hashIndex,searchState))
   {
       cout << "Failed to remove state." << endl;
       cout << searchState << endl;
   }
   else {
       cout << "Successfully removed state." << endl;
       cout << "Printing States" << endl;
       cout << setw(15) << left << "Name: " << " | ";
       cout << setw(15) << left << "Capital: " << " | ";
       cout << setw(15) << left << "Joined Union: " << " | ";
       cout << setw(15) << left << "Join Number: " << " | ";
       cout << setw(15) << left << "Area: " << endl;
       stateHash.print();
   }
}

stateData.h


#include <string>
#include <iostream>
#include <iomanip>


using namespace std;

class stateData {
private:
   string name;
   string capital;
   int unionNum;
   int year;
   int area;

public:
   //accessor
   string getName() { return name; }
   void setName(string n) { name = n; }
   string getCapital() { return capital; }
   void setCapital(string c) { capital = c; }
   int getUnionNum() { return unionNum; }
   void setUnionNum(int n) { unionNum = n; }
   int getYear() { return year; }
   void setYear(int y) { year = y; }
   int getArea() { return area; }
   void setArea(int a) { area = a; }

   stateData(string iname = " ", string icap = " ", int inum = 0, int iyear = 0, int iarea = 0) {
       name = iname;
       capital = icap;
       unionNum = inum;
       year = iyear;
       area = iarea;
   } // constructor


   //output overload
   friend ostream& operator<<(ostream& out, stateData& state) {
       if (state.getArea() > 0) {
           out << setw(15) << right << state.getName() << " | ";
           out << setw(15) << right << state.getCapital() << " | ";
           out << setw(15) << right << state.getYear() << " | ";
           out << setw(15) << right << state.getUnionNum() << " | ";
           out << setw(15) << right << state.getArea();
       }
       return out;
   }
   //input overload
   friend istream& operator>>(istream& in, stateData& state) {  
       char ch;
       getline(in, state.name);
       getline(in, state.capital);
       in >> state.area >> state.year >> state.unionNum;
      
       in.get(ch);
      
       return in;
   }
   //overload assignment operator
   stateData& operator=(const stateData& other) {
       name = other.name;
       capital = other.capital;
       unionNum = other.unionNum;
       year = other.year;
       area = other.area;

       return *this;
   }

   //overload boolean operators
   bool operator==(const stateData& other) const { return name == other.name; }
   bool operator!=(const stateData& other) const { return name != other.name; }
   bool operator>=(const stateData& other) const { return name >= other.name; }
   bool operator>(const stateData& other) const { return name > other.name; }
   bool operator<=(const stateData& other) const { return name <= other.name; }
   bool operator<(const stateData& other) const { return name < other.name; }
};

orderedArrayListType.h
#ifndef H_OrderedListType
#define H_OrderedListType

#include <iostream>
#include "arrayListType.h"

using namespace std;

template<class elemType>
class orderedArrayListType: public arrayListType<elemType>
{
public:
   void insertOrd(const elemType&);
  
   void fill();

   int binarySearch(const elemType& item) const;
   int binaryWithSeqSearch(const elemType& item)const;

   orderedArrayListType(int size = 100);
};

template <class elemType>
void orderedArrayListType<elemType>::insertOrd(const elemType& item)
{
    int first = 0;
    int last = length - 1;
    int mid;

    bool found = false;

    if (length == 0)   //the list is empty
    {
        list[0] = item;
        length++;
    }
    else if (length == maxSize)
        cerr << "Cannot insert into a full list." << endl;
    else
    {
        while (first <= last && !found)
        {
            mid = (first + last) / 2;

            if (list[mid] == item)
                found = true;
            else if (list[mid] > item)
                last = mid - 1;
            else
                first = mid + 1;
        }//end while

        if (found)
            cerr << "The insert item is already in the list. "
                 << "Duplicates are not allowed." << endl;
        else
        {
            if (list[mid] < item)
                mid++;

            insertAt(mid, item);
        }
    }
}//end insertOrd

template<class elemType>
int orderedArrayListType<elemType>::binaryWithSeqSearch
                                       (const elemType& item) const
{
    int first = 0;
    int last = length - 1;
    int mid;

    bool found = false;

   int count = 0;


       while (first <= last && !found)
       {
           mid = (first + last) / 2;
           count++;
           if (list[mid] == item)
               found = true;
           else if (list[mid] > item)
               last = mid - 1;
           else
               first = mid + 1;
           //once the partition has reached 15 or below
           if (last - first <= 15) {
               for (int i = first; i <= last; i++) {
                   count++;
                   if (list[i] == item) {
                       found = true;
                       break;
                   }  
               }
               break;
           }
       }
  

   cout << "Binary search with sequential comparisons: " << count << endl;

    if (found)
        return mid;
    else
        return -1;
}//end binaryWithSeqSearch

template<class elemType>
int orderedArrayListType<elemType>::binarySearch
(const elemType& item) const
{
   int first = 0;
   int last = length - 1;
   int mid;

   bool found = false;

   int count = 0;


       count = 2;
       while (first <= last && !found)
       {
           mid = (first + last) / 2;
           count++;
           if (list[mid] == item)
               found = true;
           else if (list[mid] > item)
               last = mid - 1;
           else
               first = mid + 1;
       }
  

   cout << "Binary search comparisons: " << count << endl;

   if (found)
       return mid;
   else
       return -1;
}//end binarySearch


template<class elemType>
orderedArrayListType<elemType>::orderedArrayListType(int size)
   : arrayListType<elemType>(size)
{
}
template<class elemType>
void orderedArrayListType<elemType> :: fill()
{
   int seed = 47;
   int multiplier = 2743;
   int addOn = 5923;
   while (listSize() < maxListSize())
   {
       insertOrd(seed);
       seed = int((seed * multiplier + addOn) % 100000);
   }
}
#endif


arrayListType.h
#ifndef H_arrayListType
#define H_arrayListType


#include <iostream>
#include <cassert>

using namespace std;

template <class elemType>
class arrayListType
{
public:
    const arrayListType<elemType>& operator=
                         (const arrayListType<elemType>&);
      //Overloads the assignment operator
    bool isEmpty() const;
      //Function to determine whether the list is empty
      //Postcondition: Returns true if the list is empty;
      //    otherwise, returns false.
    bool isFull() const;
      //Function to determine whether the list is full.
      //Postcondition: Returns true if the list is full;
      //    otherwise, returns false.
    int listSize() const;
      //Function to determine the number of elements in the list
      //Postcondition: Returns the value of length.
    int maxListSize() const;
      //Function to determine the size of the list.
      //Postcondition: Returns the value of maxSize.
    void print() const;
      //Function to output the elements of the list
      //Postcondition: Elements of the list are output on the
       //   standard output device.
    bool isItemAtEqual(int location, const elemType& item) const;
      //Function to determine whether the item is the same
      //as the item in the list at the position specified by
      //Postcondition: Returns true if the list[location]
      //    is the same as the item; otherwise,
      //               returns false.
   void insertAt(int location, const elemType& insertItem);
      //Function to insert an item in the list at the
      //position specified by location. The item to be inserted
      //is passed as a parameter to the function.
      //Postcondition: Starting at location, the elements of the
      //    list are shifted down, list[location] = insertItem;,
      //    and length++;. If the list is full or location is
      //    out of range, an appropriate message is displayed.
   void insertEnd(const elemType& insertItem);
      //Function to insert an item at the end of the list.
      //The parameter insertItem specifies the item to be inserted.
      //Postcondition: list[length] = insertItem; and length++;
      //    If the list is full, an appropriate message is
      //    displayed.
    void removeAt(int location);
      //Function to remove the item from the list at the
      //position specified by location
      //Postcondition: The list element at list[location] is removed
      //    and length is decremented by 1. If location is out of
      //    range,an appropriate message is displayed.
    void retrieveAt(int location, elemType& retItem) const;
      //Function to retrieve the element from the list at the
      //position specified by location.
      //Postcondition: retItem = list[location]
      //    If location is out of range, an appropriate message is
      //    displayed.
    void replaceAt(int location, const elemType& repItem);
      //Function to replace the elements in the list at the
      //position specified by location. The item to be replaced
      //is specified by the parameter repItem.
      //Postcondition: list[location] = repItem
      //    If location is out of range, an appropriate message is
      //    displayed.
    void clearList();
      //Function to remove all the elements from the list.
      //After this operation, the size of the list is zero.
      //Postcondition: length = 0;
    int seqSearch(const elemType& item) const;
      //Function to search the list for a given item.
      //Postcondition: If the item is found, returns the location
      //    in the array where the item is found; otherwise,
      //    returns -1.
    void insert(const elemType& insertItem);
      //Function to insert the item specified by the parameter
      //insertItem at the end of the list. However, first the
      //list is searched to see whether the item to be inserted
      //is already in the list.
      //Postcondition: list[length] = insertItem and length++
      //     If the item is already in the list or the list
      //     is full, an appropriate message is displayed.
    void remove(const elemType& removeItem);
      //Function to remove an item from the list. The parameter
      //removeItem specifies the item to be removed.
      //Postcondition: If removeItem is found in the list,
      //      it is removed from the list and length is
      //      decremented by one.

    arrayListType(int size = 100);
      //constructor
      //Creates an array of the size specified by the
      //parameter size. The default array size is 100.
      //Postcondition: The list points to the array, length = 0,
      //    and maxSize = size
  
    arrayListType(const arrayListType<elemType>& otherList);
      //copy constructor

    ~arrayListType();
      //destructor
      //Deallocates the memory occupied by the array.

protected:
    elemType *list; //array to hold the list elements
    int length;      //to store the length of the list
    int maxSize;     //to store the maximum size of the list
};

template <class elemType>
bool arrayListType<elemType>::isEmpty() const
{
    return (length == 0);
}

template <class elemType>
bool arrayListType<elemType>::isFull() const
{
    return (length == maxSize);
}

template <class elemType>
int arrayListType<elemType>::listSize() const
{
    return length;
}

template <class elemType>
int arrayListType<elemType>::maxListSize() const
{
    return maxSize;
}

template <class elemType>
void arrayListType<elemType>::print() const
{
    for (int i = 0; i < length; i++)
        cout << list[i] << " ";

    cout << endl;
}

template <class elemType>
bool arrayListType<elemType>::isItemAtEqual
                            (int location, const elemType& item) const
{
    return(list[location] == item);
}

template <class elemType>
void arrayListType<elemType>::insertAt
                  (int location, const elemType& insertItem)
{
    if (location < 0 || location >= maxSize)
        cerr << "The position of the item to be inserted "
             << "is out of range" << endl;
    else if (length >= maxSize) //list is full
        cerr << "Cannot insert in a full list" << endl;
    else
    {
        for (int i = length; i > location; i--)
             list[i] = list[i - 1];   //move the elements down

        list[location] = insertItem; //insert the item at the
                                      //specified position

        length++;     //increment the length
    }
} //end insertAt

template <class elemType>
void arrayListType<elemType>::insertEnd(const elemType& insertItem)
{

    if (length >= maxSize) //the list is full
        cerr << "Cannot insert in a full list" << endl;
    else
    {
         list[length] = insertItem;   //insert the item at the end
         length++;   //increment the length
    }
} //end insertEnd

template <class elemType>
void arrayListType<elemType>::removeAt(int location)
{
    if (location < 0 || location >= length)
        cerr << "The location of the item to be removed "
             << "is out of range" << endl;
    else
    {
        for (int i = location; i < length - 1; i++)
            list[i] = list[i+1];

        length--;
    }
} //end removeAt

template <class elemType>
void arrayListType<elemType>::retrieveAt
                             (int location, elemType& retItem) const
{
    if (location < 0 || location >= length)
        cerr << "The location of the item to be retrieved is "
             << "out of range." << endl;
    else
        retItem = list[location];
} //end retrieveAt


template <class elemType>
void arrayListType<elemType>::replaceAt
                          (int location, const elemType& repItem)
{
    if (location < 0 || location >= length)
        cerr << "The location of the item to be replaced is "
             << "out of range." << endl;
    else
        list[location] = repItem;

} //end replaceAt

template <class elemType>
void arrayListType<elemType>::clearList()
{
    length = 0;
} //end clearList

template <class elemType>
int arrayListType<elemType>::seqSearch(const elemType& item) const
{
    int loc;
    bool found = false;
   int count = 0;
   for (loc = 0; loc < length; loc++) {
       count++;
       if (list[loc] == item)
       {
           found = true;
           break;
       }
   }

   cout << "Sequential search comparisons" << count << endl;

    if (found)
        return loc;
    else
        return -1;
} //end seqSearch

template <class elemType>
void arrayListType<elemType>::insert(const elemType& insertItem)
{
    int loc;

    if (length == 0)   //list is empty
        list[length++] = insertItem;    //insert the item and
                                //increment the length
    else if (length == maxSize)
        cerr << "Cannot insert in a full list." << endl;
    else
    {
        loc = seqSearch(insertItem);

        if (loc == -1)    //the item to be inserted
                          //does not exist in the list
            list[length++] = insertItem;
        else
            cerr << "the item to be inserted is already in "
                 << "the list. No duplicates are allowed." << endl;
    }
} //end insert

template<class elemType>
void arrayListType<elemType>::remove(const elemType& removeItem)
{
    int loc;

    if (length == 0)
        cerr << "Cannot delete from an empty list." << endl;
    else
    {
        loc = seqSearch(removeItem);

        if (loc != -1)
            removeAt(loc);
        else
            cout << "The item to be deleted is not in the list."
                 << endl;
    }
} //end remove

template <class elemType>
arrayListType<elemType>::arrayListType(int size)
{
    if (size < 0)
    {
        cerr << "The array size must be positive. Creating "
             << "an array of size 100. " << endl;

        maxSize = 100;
    }
    else
        maxSize = size;

    length = 0;

    list = new elemType[maxSize];
    assert(list != NULL);
}

template <class elemType>
arrayListType<elemType>::~arrayListType()
{
    delete [] list;
}


template <class elemType>
arrayListType<elemType>::arrayListType
                   (const arrayListType<elemType>& otherList)
{
    maxSize = otherList.maxSize;
    length = otherList.length;
    list = new elemType[maxSize]; //create the array
    assert(list != NULL);         //terminate if unable to allocate
                                  //memory space

    for (int j = 0; j < length; j++) //copy otherList
        list [j] = otherList.list[j];
} //end copy constructor

template <class elemType>
const arrayListType<elemType>& arrayListType<elemType>::operator=
                      (const arrayListType<elemType>& otherList)
{
    if (this != &otherList)   //avoid self-assignment
    {
        delete [] list;
        maxSize = otherList.maxSize;
        length = otherList.length;

        list = new elemType[maxSize]; //create the array
        assert(list != NULL);   //if unable to allocate memory
                                //space, terminate the program
        for (int i = 0; i < length; i++)
            list[i] = otherList.list[i];
    }

    return *this;
}

#endif

hashT.h

#ifndef H_Htable
#define H_Htable


#include <iostream>
#include <cassert>

using namespace std;

template <class elemType>
class hashT
{
public:
    void insert(int hashIndex, const elemType& rec);
      //Function to insert an item in the hash table. The first
      //parameter specifies the initial hash index of the item to
      //be inserted. The item to be inserted is specified by the
      //parameter rec.
      //Postcondition: If an empty position is found in the hash
      //   table, rec is inserted and the length is incremented by
      //   one; otherwise, an appropriate error message is
      //   displayed.

    void search(int& hashIndex, const elemType& rec, bool& found) const;
      //Function to determine whether the item specified by the
      //parameter rec is in the hash table. The parameter hashIndex
      //specifies the initial hash index of rec.
      //Postcondition: If rec is found, found is set to true and
      //   hashIndex specifies the position where rec is found;
      //   otherwise, found is set to false.

    bool isItemAtEqual(int hashIndex, const elemType& rec) const;
      //Function to determine whether the item specified by the
      //parameter rec is the same as the item in the hash table
      //at position hashIndex.
      //Postcondition: Returns true if HTable[hashIndex] == rec;
      //   otherwise, returns false.

    void retrieve(int hashIndex, elemType& rec) const;
      //Function to retrieve the item at position hashIndex.
      //Postcondition: If the table has an item at position
      //   hashIndex, it is copied into rec.

    void remove(int hashIndex, const elemType& rec);
      //Function to remove an item from the hash table.
      //Postcondition: Given the initial hashIndex, if rec is found
      //   in the table it is removed; otherwise, an appropriate
      //   error message is displayed.

    void print() const;
      //Function to output the data.

   int hashFunc(string name);
      //hash function for hashing algorithm

    hashT(int size = 101);
      //constructor
      //Postcondition: Create the arrays HTTable and indexStatusList;
      //   initialize the array indexStatusList to 0; length = 0;
      //   HTSize = size; and the default array size is 101.

    ~hashT();
      //destructor
      //Postcondition: Array HTable and indexStatusList are deleted.

private:
    elemType *HTable;   //pointer to the hash table
    int *indexStatusList; //pointer to the array indicating the
                           //status of a position in the hash table
    int length;    //number of items in the hash table
    int HTSize;    //maximum size of the hash table
};

template <class elemType>
int hashT<elemType>::hashFunc(string name)
{
   int i, sum;
   int len;
   i = 0;
   sum = 0;
   len = name.length();
   for (int k = 0; k < 15 - len; k++)
       name = name + ' '; //increase the length of name to 15 characters
   for (int k = 0; k < 5; k++)
   {
       sum = sum + static_cast<int>(name[i]) * 128 * 128
           + static_cast<int>(name[i + 1]) * 128
           + static_cast<int>(name[i + 2]);
       i = i + 3;
   }
   return sum % HTSize;
}

template <class elemType>
void hashT<elemType>::insert(int hashIndex, const elemType& rec)
{
   int pCount;
   int inc;

   pCount = 0;
   inc = 1;

   while(indexStatusList[hashIndex] == 1
          && HTable[hashIndex] != rec
          && pCount < HTSize / 2)
   {
       pCount++;
       hashIndex = (hashIndex + inc ) % HTSize;
       inc = inc + 2;
   }


   if(indexStatusList[hashIndex] != 1)
   {
       HTable[hashIndex] = rec;
       indexStatusList[hashIndex] = 1;
       length++;
   }
   else
       if(HTable[hashIndex] == rec)
           cerr<<"Error: No duplicates are allowed."<<endl;
       else
           cerr<<"Error: The table is full. "
                <<"Unable to resolve the collision."<<endl;
}

template <class elemType>
void hashT<elemType>::search(int& hashIndex, const elemType& rec, bool& found) const
{
   int pCount;
   int inc;

   pCount = 0;
   inc = 1;

   while (indexStatusList[hashIndex] != 0
       && HTable[hashIndex] != rec
       && pCount < HTSize / 2)
   {
       pCount++;
       hashIndex = (hashIndex + inc) % HTSize;
       inc = inc + 2;
   }

   if (HTable[hashIndex] == rec && indexStatusList[hashIndex] == 1) {
       found == true;
   }
   else {
       found == false;
   }
}

template <class elemType>
bool hashT<elemType>::isItemAtEqual(int hashIndex, const elemType& rec) const
{
   if (indexStatusList[hashIndex] != 1) {
       cout << "No Item at location!" << endl;
       return false;
   }
   else {
       return HTable[hashIndex] == rec;
   }
}

template <class elemType>
void hashT<elemType>::retrieve(int hashIndex, elemType& rec) const
{  
   if (indexStatusList[hashIndex] != 1) {
       cout << "No Item at location!" << endl;
   }
   else {
       rec = HTable[hashIndex];
   }
}

template <class elemType>
void hashT<elemType>::remove(int hashIndex, const elemType& rec)
{
   bool found;

   search(hashIndex, rec, found);

   if (found) {
       indexStatusList[hashIndex] = -1;
       length--;
   }
   else {
       cout << "Item is missing." << endl;
   }
}

template <class elemType>
void hashT<elemType>::print() const
{
   for (int i = 0; i < HTSize; i++) {
       if (indexStatusList[i] == 1) {
           cout << HTable[i] << endl;
       }
   }
}

template <class elemType>
hashT<elemType>::hashT(int size)
{
   HTable = new elemType[size];
   indexStatusList = new int[size];
   HTSize = size;
   length = 0;

   for (int i = 0; i < size; i++) {
       indexStatusList[i] = 0;
   }

}

template <class elemType>
hashT<elemType>::~hashT()
{
   delete[] HTable;
   delete[] indexStatusList;
}

#endif

state_data.txt

New Hampshire
Concord
9304 1788 9
Massachusetts
Boston
8257 1788 6
Vermont
Montpelier
9609 1791 14
Rhode Island
Providence
1214 1790 13
Connecticut
Hartford
5009 1788 5
Maine
Augusta
33215 1820 23
New York
Albany
49576 1788 11
Pennsylvania
Harrisburg
45333 1787 2
Delaware

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote