In C++, please do not include all that information on all the states, only name
ID: 3910538 • Letter: I
Question
In C++, please do not include all that information on all the states, only name and capital and only 15 states. Test the retrieve time 10,000 retrieves if HTSize = 20 and if HTSize = 60. Generate the 10000 retrieves using a random number generator
a. 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. Design the class stateData to keep track of the information for
a state. Your class must include appropriate functions to manipulate the
state’s data, such as the functions setStateInfo, 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.
b. Use 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.
Test your program by searching for and removing certain states from the
hash table.
You may use the following hash function 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 + ' '; //increase the length of the name
//to 15 characters
for (int k = 0; k < 5; k++)
{
sum = sum + static_cast(name[i]) * 128 * 128
+ static_cast(name[i + 1]) * 128
+ static_cast(name[i + 2]);
i = i + 3;
}
return sum % HTSize;
}
Explanation / Answer
#include<iostream>
using namespace std;
class statedata{
public:
string name,capital;
int yearofadm;
void setstateinfo(statedata d){
cin>>d;
}
void getstatedata(statedata d){
cout<<d;
}
friend ostream & operator << (ostream &out, const statedata &c);
friend istream & operator >> (istream &in, statedata &c);
};
ostream & operator << (ostream &out, const statedata &c)
{
out <<"name "<< c.name<<endl;
out << "capital " << c.capital << endl;
out << "year of admission " << c.yearofadm << endl;
return out;
}
istream & operator >> (istream &in, Complex &c)
{
cout << " Enter Real Part ";
in >> c.name;
cout << " Enter Imagenory Part ";
in >> c.capital;
cout << " Enteryear of admission";
in >> c.yearofadmi;
return in;
}
void del(statedata arr[],string s,int size){
statedata d;
for(int i=0;i<size;i++){
d=arr[i];
if(d.name==s){
arr[i]=NULL;
}
}
}
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(name[i]) * 128 * 128
+ static_cast(name[i + 1]) * 128
+ static_cast(name[i + 2]);
i = i + 3;
}
return sum % HTSize;
}
int main(){
int HTsize;
cout<<"enter hash tablesize ";
cin>>HTSize;
statedata arr[HTSize];
statedata s;
s.getstatedata(s);
string name1 ;
cin>>name1;
int h=hashFunc(name1);
if(arr[h]==NULL){
statedata s1;
s1.getstatedata(s1);
arr[h]=s1;
}
else{
cout<<s1<<endl;
}
del(arr,name1,HTSize);//delete function//
return 0;
}