Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Please explain your answer as well. I will include sample input and output for your reference. Please write in c++ . I will include code
Please explain your answer as well. I will include sample input and output for your reference. Please write in c++ . I will include code that already solves the problem, but I need you to rewrite it and solve it differently. This should not be difficult I just need to see someone answer it differently.
Here is a sample input:
and sample output:
Code that already solves the problem:
#include#include #include #include using namespace std; const int TABLE_SIZE = 128; /* * HashNode Class Declaration */ class HashNode { public: int key; int value; HashNode* next; HashNode(int key, int value) { this->key = key; this->value = value; this->next = NULL; } }; /* * HashMap Class Declaration */ class HashMap { private: HashNode** htable; public: HashMap() { htable = new HashNode*[TABLE_SIZE]; for (int i = 0; i next; delete prev; } } delete[] htable; } /* * Hash Function */ int HashFunc(int key) { return key % 9; } /* * Insert Element at a key */ void Insert(int value) { int hash_val = HashFunc(value); HashNode* prev = NULL; HashNode* entry = htable[hash_val]; while (entry != NULL) { prev = entry; entry = entry->next; } if (entry == NULL) { entry = new HashNode(HashFunc(value), value); if (prev == NULL) { htable[hash_val] = entry; } else { prev->next = entry; } } else { entry->value = value; } } static HashMap* buildHash(int * n, int size) { clock_t begin = clock(); HashMap *hm = new HashMap(); for (int i = 0; i Insert(n[i]); } clock_t end = clock(); cout value next; } cout >input; switch (input[0]) { case'S': { HashMap * hm = new HashMap(); cin >> name; ifstream fin(name + ".txt"); int length = 0; fin >> length; int * numbers = new int[length]; int c = 0; while (!fin.eof()) { fin >> numbers[c++]; } cout >input; switch (input[0]) { case 'B': build = true; hm = HashMap::buildHash(numbers, length); break; case 'H': if (build) { hm->Display(); } else { cout > input; if (input==name) { cout 3.2 Requirements In this question, you will write a serial program that executes a sequence of commands that operate on individual files. Valid commands include: Start Name, where Name is a character string of maximum length 20 alphabetic characters representing the name of the data file (Name.txt). The structure of Name.txt is an integer N indicating the total number of data entries contained in this file, followed by N additional lines of integers (the actual data entries). This command first reads N from the file Name.txt, dynamically allocates an array of integers of size N, and then reads the remaining N data entries into the array. The commands that follow until the End command are to be applied to the resulting data array. The output of the Start command is: Processing data from: Name.txt End Name, which indicates the end of the processing for the data in file Name.txt. The Start and End commands wil always come in pairs with matching names. Any memory dynamically allocated for Name must be freed on an End command. The output of the End command is: End of processing data from: Name.txt . BuildHash Table, which builds a hash table storing al the integers from the array of size N. Your atedly insert one integer at a time into the hash table using hash function implementation must repe h(k)k mod 9, resolving collision by chaining. When finished, output the following information: Time to build hash table (ms): t Hash TableSize, which outputs the number of elements (integers) stored at each slot of the hash table If no hash table has been built, output Build hash table first! Otherwise, go through each slot i of the hash table, and output the number of elements (integers) stored at that slot ni in the following format Number of elements in slot i: ni . Exit, which indicates that there are no more commands to execute, .e., the program terminates Test your program against the following com mand sequence. All the test files (the same as assignment #2) are provided in the folder named datafiles Start test.1 BuildHash Table Hash TableSize End test.1 3.2 Requirements In this question, you will write a serial program that executes a sequence of commands that operate on individual files. Valid commands include: Start Name, where Name is a character string of maximum length 20 alphabetic characters representing the name of the data file (Name.txt). The structure of Name.txt is an integer N indicating the total number of data entries contained in this file, followed by N additional lines of integers (the actual data entries). This command first reads N from the file Name.txt, dynamically allocates an array of integers of size N, and then reads the remaining N data entries into the array. The commands that follow until the End command are to be applied to the resulting data array. The output of the Start command is: Processing data from: Name.txt End Name, which indicates the end of the processing for the data in file Name.txt. The Start and End commands wil always come in pairs with matching names. Any memory dynamically allocated for Name must be freed on an End command. The output of the End command is: End of processing data from: Name.txt . BuildHash Table, which builds a hash table storing al the integers from the array of size N. Your atedly insert one integer at a time into the hash table using hash function implementation must repe h(k)k mod 9, resolving collision by chaining. When finished, output the following information: Time to build hash table (ms): t Hash TableSize, which outputs the number of elements (integers) stored at each slot of the hash table If no hash table has been built, output Build hash table first! Otherwise, go through each slot i of the hash table, and output the number of elements (integers) stored at that slot ni in the following format Number of elements in slot i: ni . Exit, which indicates that there are no more commands to execute, .e., the program terminates Test your program against the following com mand sequence. All the test files (the same as assignment #2) are provided in the folder named datafiles Start test.1 BuildHash Table Hash TableSize End test.1
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started