Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Please finish the whole code. //====================== // Name : HashTable.cpp // Author : John Watson // Version : 1.0 // Copyright : Copyright 2017 SNHU

Please finish the whole code.

//====================== // Name : HashTable.cpp // Author : John Watson // Version : 1.0 // Copyright : Copyright 2017 SNHU COCE // Description : Hello World in C++, Ansi-style //====================

#include #include #include #include // atoi #include

#include "CSVparser.hpp"

using namespace std;

//===================== // Global definitions visible to all methods and classes //=====================

const unsigned int DEFAULT_SIZE = 179;

// forward declarations double strToDouble(string str, char ch);

// define a structure to hold bid information struct Bid { string bidId; // unique identifier string title; string fund; double amount; Bid() { amount = 0.0; } };

//===================== // Hash Table class definition //=====================

/** * Define a class containing data members and methods to * implement a hash table with chaining. */ class HashTable {

private: // Define structures to hold bids struct Node { Bid bid; unsigned int key; Node *next;

// default constructor Node() { key = UINT_MAX; next = nullptr; }

// initialize with a bid Node(Bid aBid) : Node() { bid = aBid; }

// initialize with a bid and a key Node(Bid aBid, unsigned int aKey) : Node(aBid) { key = aKey; } };

vector nodes;

unsigned int tableSize = DEFAULT_SIZE;

unsigned int hash(int key);

public: HashTable(); HashTable(unsigned int size); virtual ~HashTable(); void Insert(Bid bid); void PrintAll(); void Remove(string bidId); Bid Search(string bidId); };

/** * Default constructor */ HashTable::HashTable() { // FIXME (1): Initialize the structures used to hold bids // Initalize node structure by resizing tableSize }

/** * Constructor for specifying size of the table * Use to improve efficiency of hashing algorithm * by reducing collisions without wasting memory. */ HashTable::HashTable(unsigned int size) { // invoke local tableSize to size with this-> // resize nodes size }

/** * Destructor */ HashTable::~HashTable() { // FIXME (2): Implement logic to free storage when class is destroyed // erase nodes beginning }

/** * Calculate the hash value of a given key. * Note that key is specifically defined as * unsigned int to prevent undefined results * of a negative list index. * * @param key The key to hash * @return The calculated hash */ unsigned int HashTable::hash(int key) { // FIXME (3): Implement logic to calculate a hash value // return key tableSize }

/** * Insert a bid * * @param bid The bid to insert */ void HashTable::Insert(Bid bid) { // FIXME (5): Implement logic to insert a bid // create the key for the given bid // retrieve node using key // if no entry found for the key // assign this node to the key position // else if node is not used // assing old node key to UNIT_MAX, set to key, set old node to bid and old node next to null pointer // else find the next open node // add new newNode to end }

/** * Print all bids */ void HashTable::PrintAll() { // FIXME (6): Implement logic to print all bids // for node begin to end iterate // if key not equal to UINT_MAx // output key, bidID, title, amount and fund // node is equal to next iter // while node not equal to nullptr // output key, bidID, title, amount and fund // node is equal to next node

}

/** * Remove a bid * * @param bidId The bid id to search for */ void HashTable::Remove(string bidId) { // FIXME (7): Implement logic to remove a bid // set key equal to hash atoi bidID cstring // erase node begin and key }

/** * Search for the specified bidId * * @param bidId The bid id to search for */ Bid HashTable::Search(string bidId) { Bid bid;

// FIXME (8): Implement logic to search for and return a bid

// create the key for the given bid // if entry found for the key //return node bid

// if no entry found for the key // return bid // while node not equal to nullptr // if the current node matches, return it /ode is equal to next node

return bid; }

//==================== // Static methods used for testing //====================

/** * Display the bid information to the console (std::out) * * @param bid struct containing the bid info */ void displayBid(Bid bid) { cout

/** * Load a CSV file containing bids into a container * * @param csvPath the path to the CSV file to load * @return a container holding all the bids read */ void loadBids(string csvPath, HashTable* hashTable) { cout

// initialize the CSV Parser using the given path csv::Parser file = csv::Parser(csvPath);

// read and display header row - optional vector header = file.getHeader(); for (auto const& c : header) { cout

try { // loop to read rows of a CSV file for (unsigned int i = 0; i

// Create a data structure and add to the collection of bids Bid bid; bid.bidId = file[i][1]; bid.title = file[i][0]; bid.fund = file[i][8]; bid.amount = strToDouble(file[i][4], '$');

//cout

// push this bid to the end hashTable->Insert(bid); } } catch (csv::Error &e) { std::cerr

/** * Simple C function to convert a string to a double * after stripping out unwanted char * * credit: http://stackoverflow.com/a/24875936 * * @param ch The character to strip out */ double strToDouble(string str, char ch) { str.erase(remove(str.begin(), str.end(), ch), str.end()); return atof(str.c_str()); }

/** * The one and only main() method */ int main(int argc, char* argv[]) {

// process command line arguments string csvPath, bidKey; switch (argc) { case 2: csvPath = argv[1]; bidKey = "98109"; break; case 3: csvPath = argv[1]; bidKey = argv[2]; break; default: csvPath = "eBid_Monthly_Sales_Dec_2016.csv"; bidKey = "98109"; }

// Define a timer variable clock_t ticks;

// Define a hash table to hold all the bids HashTable* bidTable;

Bid bid; bidTable = new HashTable(); int choice = 0; while (choice != 9) { cout > choice;

switch (choice) {

case 1: // Initialize a timer variable before loading bids ticks = clock();

// Complete the method call to load the bids loadBids(csvPath, bidTable);

// Calculate elapsed time and display result ticks = clock() - ticks; // current clock ticks minus starting clock ticks cout

case 2: bidTable->PrintAll(); break;

case 3: ticks = clock();

bid = bidTable->Search(bidKey);

ticks = clock() - ticks; // current clock ticks minus starting clock ticks

if (!bid.bidId.empty()) { displayBid(bid); } else { cout

cout

case 4: bidTable->Remove(bidKey); break; } }

cout

return 0; }

image text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribed

overview The focus of these problems will be working with information extracted from a municipal government data feed containing bids submitted for auction of property. All materials for this lab assignment can be found in the Supporting Materials section below. The data set is provided in two comma-separated files: 1. eBid_Monthly_Sales.csv (larger set of 12,023 bids) 2. eBid_Monthly_Sales_Dec_2016.csv (smaller set of 76 bids) This assignment is designed to explore linked lists, so you will implement a singly linked list to hold a collection of bids loaded from a CSV file. We provide a starter console program that uses a menu to enable testing of the hash table logic you will complete. It also allows you to pass in the path to the bids CSV file to be loaded, enabling you to try both files. In this version, the following menu is presented when the program is run: Menu: 1. Load Bids 2. Display All Bids 3. Find Bid 4. Remove Bid 9. Exit Enter choice: The HashTable.cpp program is partially completed. It contains empty methods representing the programming interface used to interact with a hash table. You will need to add logic to the methods to implement the necessary behavior. Here is the public API for HashTable.cpp that you have to complete: public: HashTable(); virtual HashTable(); void Insert(Bid bid); void Printall(); void Remove(string bidId); Bid Search(string bidId); Prompt You will need to perform the following steps to complete this activity: a. Name the project "HashTable". Remember to pick the correct compiler in Toolchains and click Finish. This will create a simple HashTable.cpp source file under the /src directory. b. Download the starter program files and copy them to the project's /src directory, replacing the existing auto-generated ones. Remember to right-click on the project in the Project Explorer pane on the left and Refresh the project so it adds all the new files to the src folder underneath. c. Because this activity uses C++11 features, you may need to add the -std=c++11 compiler switch to the miscellaneous settings. Task 1: Define structures to hold bids. Hint: You may choose either an array or a vector for storage. Note that you may be able to reuse portions of your code from previous assignments to save you time. Look for places you have implemented vectors for storage or a Node structure for a linked list. Reusing code from these labs may save you time. Task 2: Initialize the structures used to hold bids. Task 3: Implement code to free storage when a class is destroyed. Task 4: Implement code to calculate a hash value using the bid ID as the source for calculating the key. Task 5: Implement code to insert a bid. Be sure to check for key collisions and use the chaining technique with a linked list to store the additional bids. Task 6: Implement code to print all bids. Task 7: Implement code to remove a bid. Task 8: Implement code to search for and return a bid. Here is sample output from running the completed program: >./HashTable /Downloads/eBid_Monthly_Sales_Dec_2016.csv > HashTable. exe Downloads \eBid Monthly Sales Dec 2016.csv Note that Keys 2, 12, and 13 highlighted above indicate that key collisions occurred. Finding and Removing an existing bid: rilluilig d viu uldi IU ivilger exisls: Your submission must ad Your submission must address the following rubric criteria: - Code Reflection: A brief explanation of the code and its purpose, and a brief discussion of your experience in developing it, including any issues that you encountered while completing the exercise and what approaches you took to solve them - Pseudocode or Flowchart: A pseudocode or flowchart description of the code that is clear and understandable and captures accurate logic to translate to the programming language - Specifications and Correctness: Source code must meet its specifications and behave as desired. Correct code produces the correct output as defined by the data and problem; however, you should also produce fully functioning code (with no errors) that aligns with as many of the specifications as possible. You should write your code in such a way that the submitted file executes, even if it does not produce the correct output. You will be given credit for partially correct output that can be viewed and seen to be partially correct. - Annotation / Documentation: All code should also be well-commented. This is a practiced art that requires striking a balance between commenting everything, which adds a great deal of unneeded noise to the code, and commenting nothing. Well-annotated code requires you to: - Explain the purpose of lines or sections of your code, detailing the approach and method you took to achieve a specific task in the code. - Document any section of code that is producing errors or incorrect results. - Modular and Reusable: Programmers should develop code that is modular and reusable. If it contains functionality and responsibility in distinct methods, code is more flexible and maintainable. Your code should adhere to the single responsibility principle-classes and methods should do only one job. If you can replace a method with another that uses a different technique or implementation without impacting (having to refactor or rewrite) other parts of your code, then you have succeeded in creating modular methods. - Readability: Code needs to be readable to a knowledgeable programmer. In this course, readable code requires: - Consistent, appropriate whitespace (blank lines, spaces) and indentation to separate distinct parts of the code and operations - Explicit, consistent variable names, which should clearly indicate the data they hold and be formatted consistently: for example, numOrders (camelCase) or item_cost (underscored) - Organized structure and clear design that separates components with different responsibilities or grouping-related code into blocks Guidelines for Submission To complete this lab assignment, submit the CPP code files and a code reflection and associated pseudocode or flowchart. Your written portion should be 1-2 paragraphs in length. overview The focus of these problems will be working with information extracted from a municipal government data feed containing bids submitted for auction of property. All materials for this lab assignment can be found in the Supporting Materials section below. The data set is provided in two comma-separated files: 1. eBid_Monthly_Sales.csv (larger set of 12,023 bids) 2. eBid_Monthly_Sales_Dec_2016.csv (smaller set of 76 bids) This assignment is designed to explore linked lists, so you will implement a singly linked list to hold a collection of bids loaded from a CSV file. We provide a starter console program that uses a menu to enable testing of the hash table logic you will complete. It also allows you to pass in the path to the bids CSV file to be loaded, enabling you to try both files. In this version, the following menu is presented when the program is run: Menu: 1. Load Bids 2. Display All Bids 3. Find Bid 4. Remove Bid 9. Exit Enter choice: The HashTable.cpp program is partially completed. It contains empty methods representing the programming interface used to interact with a hash table. You will need to add logic to the methods to implement the necessary behavior. Here is the public API for HashTable.cpp that you have to complete: public: HashTable(); virtual HashTable(); void Insert(Bid bid); void Printall(); void Remove(string bidId); Bid Search(string bidId); Prompt You will need to perform the following steps to complete this activity: a. Name the project "HashTable". Remember to pick the correct compiler in Toolchains and click Finish. This will create a simple HashTable.cpp source file under the /src directory. b. Download the starter program files and copy them to the project's /src directory, replacing the existing auto-generated ones. Remember to right-click on the project in the Project Explorer pane on the left and Refresh the project so it adds all the new files to the src folder underneath. c. Because this activity uses C++11 features, you may need to add the -std=c++11 compiler switch to the miscellaneous settings. Task 1: Define structures to hold bids. Hint: You may choose either an array or a vector for storage. Note that you may be able to reuse portions of your code from previous assignments to save you time. Look for places you have implemented vectors for storage or a Node structure for a linked list. Reusing code from these labs may save you time. Task 2: Initialize the structures used to hold bids. Task 3: Implement code to free storage when a class is destroyed. Task 4: Implement code to calculate a hash value using the bid ID as the source for calculating the key. Task 5: Implement code to insert a bid. Be sure to check for key collisions and use the chaining technique with a linked list to store the additional bids. Task 6: Implement code to print all bids. Task 7: Implement code to remove a bid. Task 8: Implement code to search for and return a bid. Here is sample output from running the completed program: >./HashTable /Downloads/eBid_Monthly_Sales_Dec_2016.csv > HashTable. exe Downloads \eBid Monthly Sales Dec 2016.csv Note that Keys 2, 12, and 13 highlighted above indicate that key collisions occurred. Finding and Removing an existing bid: rilluilig d viu uldi IU ivilger exisls: Your submission must ad Your submission must address the following rubric criteria: - Code Reflection: A brief explanation of the code and its purpose, and a brief discussion of your experience in developing it, including any issues that you encountered while completing the exercise and what approaches you took to solve them - Pseudocode or Flowchart: A pseudocode or flowchart description of the code that is clear and understandable and captures accurate logic to translate to the programming language - Specifications and Correctness: Source code must meet its specifications and behave as desired. Correct code produces the correct output as defined by the data and problem; however, you should also produce fully functioning code (with no errors) that aligns with as many of the specifications as possible. You should write your code in such a way that the submitted file executes, even if it does not produce the correct output. You will be given credit for partially correct output that can be viewed and seen to be partially correct. - Annotation / Documentation: All code should also be well-commented. This is a practiced art that requires striking a balance between commenting everything, which adds a great deal of unneeded noise to the code, and commenting nothing. Well-annotated code requires you to: - Explain the purpose of lines or sections of your code, detailing the approach and method you took to achieve a specific task in the code. - Document any section of code that is producing errors or incorrect results. - Modular and Reusable: Programmers should develop code that is modular and reusable. If it contains functionality and responsibility in distinct methods, code is more flexible and maintainable. Your code should adhere to the single responsibility principle-classes and methods should do only one job. If you can replace a method with another that uses a different technique or implementation without impacting (having to refactor or rewrite) other parts of your code, then you have succeeded in creating modular methods. - Readability: Code needs to be readable to a knowledgeable programmer. In this course, readable code requires: - Consistent, appropriate whitespace (blank lines, spaces) and indentation to separate distinct parts of the code and operations - Explicit, consistent variable names, which should clearly indicate the data they hold and be formatted consistently: for example, numOrders (camelCase) or item_cost (underscored) - Organized structure and clear design that separates components with different responsibilities or grouping-related code into blocks Guidelines for Submission To complete this lab assignment, submit the CPP code files and a code reflection and associated pseudocode or flowchart. Your written portion should be 1-2 paragraphs in length

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions

Question

What are the objectives of job evaluation ?

Answered: 1 week ago

Question

Write a note on job design.

Answered: 1 week ago

Question

Compute the derivative of f(x)cos(-4/5x)

Answered: 1 week ago

Question

Discuss the process involved in selection.

Answered: 1 week ago

Question

Evaluate employees readiness for training. page 275

Answered: 1 week ago