Question

Create a hash table class/struct.in C++ Define an array that holds 27 elements. Define a function...

Create a hash table class/struct.in C++

Define an array that holds 27 elements.

Define a function called Hash(int)

-This function returns the modulo of that int by the size of the table (array).

Define an add function that takes an integer.

-This function takes the integer, determines the hash of that number by calling the above hash function, then adds it to the table using linear probing for collision resolution.

Define a function that looks up a value, it takes an integer, return -1 if the value is not in the table.

Create a main that allows the user to add and lookup items in the table.

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

ANSWER:

GIVEN THAT:

Create a hash table class/struct.in

PROGRAM:-

#include <iostream>
#include <stdlib.h>
#define tableSize 27
using namespace std;

int hashTable[tableSize];

int Hash(int n)
{
return n%tableSize;
}


void add(int n)
{
int index = Hash(n);
  
int count = 0;
while(count < tableSize)
{
if (hashTable[index] == -1)
{
hashTable[index] = n;
break;
}
else
{
index = (index + 1) % tableSize;
count++;
}
}
}

void printArray()
{
for(int i = 0; i < tableSize; i++)
{
cout << hashTable[i] << " ";
}
cout << endl;
}

int lookup(int n)
{
int index = Hash(n);
  
int count = 0;
while(count < tableSize)
{
if (hashTable[index] == n)
{
return n;
}
else
{
index = (index + 1) % tableSize;
count++;
}
}
return -1;
}

int main()
{
for(int i = 0; i < tableSize; i++)
{
hashTable[i] = -1;
}

while(true)
{
cout << "Choose from given menu: " << endl;
cout << "1. Add element to hash." << endl;
cout << "2. Lookup value in hash" << endl;
cout << "Any other key to quit" << endl;
int choice;
cin >> choice;
if(choice == 1)
{
cout << "Enter number to be added: ";
int n;
cin >> n;
add(n);
}
else if (choice == 2)
{
cout << "Enter number to be searched: ";
int n;
cin >> n;
if(lookup(n) != -1)
{
cout << " Found" << endl;
}
else
{
cout << " Not found." << endl;
}
}
else
{
break;
}
}
return 0;
}

The above program output:-

Add a comment
Know the answer?
Add Answer to:
Create a hash table class/struct.in C++ Define an array that holds 27 elements. Define a function...
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
  • This should be in Java Create a simple hash table You should use an array for...

    This should be in Java Create a simple hash table You should use an array for the hash table, start with a size of 5 and when you get to 80% capacity double the size of your array each time. You should create a class to hold the data, which will be a key, value pair You should use an integer for you key, and a String for your value. For this lab assignment, we will keep it simple Use...

  • Name: Hash Tables CS 2020 Hw 14 edefine MAX CAPACITY 11 #define R struct HashTable f...

    Name: Hash Tables CS 2020 Hw 14 edefine MAX CAPACITY 11 #define R struct HashTable f int hashtable[MAX CAPACITY]: 1/ table to store values int countj // count of values currently stored int hash1(int value) ( return (value % MAX CAPACITY); int hash2(int value) ( return (R- (value % R)); 1) Use hash1 function and linear probing as a collision resolution strategy, insert the following elements into the correct index of the hash table: 1, 5, 4, 11, 21, 15,...

  • Java using data structures The objective is to create your own Hash Table class to hold...

    Java using data structures The objective is to create your own Hash Table class to hold a list of employees and their ID numbers. I've provided the TableEntry class which will be each data object in the hash table. The list of employees will be provided as a .txt file and must be read with the code. please create a .txt file called Employees.txt with the info provided so that the java code can read it in. Employees.txt: (No WhiteSpace...

  • Insert elements into a hash table implemented using chain hashing, as an array of linked list...

    Insert elements into a hash table implemented using chain hashing, as an array of linked list in which each entry slot is as a linked list of key/value pairings that have the same hash (outcome value computed using certain hash function). You are allowed to use “Hash Function”, h(k) = k % x where, x is the value you will need to decide to use, that you find appropriate for this implementation. The main requirements are the following: 1. Input:...

  • IN JAVA LANGUAGE: For this lab you are required for implement the following methods: 1. public...

    IN JAVA LANGUAGE: For this lab you are required for implement the following methods: 1. public QuadraticProbingHashTable( int size ): As the signature of this method suggests, this is a constructor for this class. This constructor will initialize status of an object from this class based on the input parameter (i.e., size). So, this function will create the array HashTable with the specified size and initialize all of its elements to be null. 2. public int hash(int value, int tableSize...

  • IN JAVA USING ECLIPSE The objective of this assignment is to create your own hash table...

    IN JAVA USING ECLIPSE The objective of this assignment is to create your own hash table class to hold employees and their ID numbers. You'll create an employee class to hold each person's key (id number) and value (name). Flow of the main program: Create an instance of your hash table class in your main method. Read in the Employees.txt file and store the names and ID numbers into Employee objects and store those in your hash table using the...

  • C++: Create a class called "HashTable" This class will implement hashing on an array of integers...

    C++: Create a class called "HashTable" This class will implement hashing on an array of integers Make the table size 11 Implement with linear probing. The hash function should be: h(x) = x % 11 Create search(), insert() and delete() member functions. Search should return the index of where the target is located, insert should allow the user to insert a value, and delete removes the desired value from the table. In main perform the following: 1. Insert: 17 11...

  • Write a c++ program that does the following Create an integer array of size 30. Assign...

    Write a c++ program that does the following Create an integer array of size 30. Assign -1 to each location in the array indicating that the array is empty. Populate half of the array with random integer values between 100 and 500 inclusive. Use the following formula in order to hash and store each number in its proper position/location. Generated Number Table Size: Should a collision occurs, use linear probing to find next available position location. Use the following probing...

  • C programming Problem 3 [Set via Hashing] As mentioned in the lecture, a hash table can...

    C programming Problem 3 [Set via Hashing] As mentioned in the lecture, a hash table can be used to implement a Set ADT. Let's try to use the template below to implement a Set with double hashing. Here we assume the Set contains names with 3 characters long. Since it is a Set, we do not need to have an explicit value for each key. We will use a token value of 1 if a name belongs to the Set....

  • Please complete the following task: Create a C++ hash table program for generating a hash from a string. Create a hash f...

    Please complete the following task: Create a C++ hash table program for generating a hash from a string. Create a hash function then test and evaluate the results. The set of strings are from a "words.txt" file (there are 45,402 words in the file, no repeating words. Map the words to 45,402 buckets using a hash function). Requirements: Create an array of 45,402 integers and initialize each to zero. These will be used to store how many times each index...

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