Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //main.cpp for final exam #2
- #include <iostream>
- #include <string>
- using namespace std;
- //2-dimensional table representing records in your file.
- const int row = 7;
- const int col = 3;
- string data[row][col] =
- {
- //In order: name/key, email, balance
- {"Gary Smith", "GarySmith@neit.edu", "$15.00"},
- {"Laila Kerniech", "LailaKerniech@neit.edu", "$30.00"},
- {"Shawn Kemp", "ShawnKemp@neit.edu", "$23.50"},
- {"Sarah Palin", "SarahPalin@neit.edu", "$110.00"},
- {"Ray Connif", "RayConnif@neit.edu", "$0.00"},
- {"Edmund Cortis", "EdmundCortis@neit.edu", "$50.00"},
- {"John Matis", "John Matis@neit.edu", "$15.00"},
- };
- string dataHashed[1324][3]; //Hashed records will be put here.
- class Search
- {
- public:
- int hashRoutine(string keyToSearch)
- {
- int sum = 0;
- int len = keyToSearch.length();
- for (int ndx = 0; ndx < len; ndx++)
- {
- int decVal = keyToSearch.at(ndx);
- sum = sum +decVal;
- }
- return sum;
- }
- void displayRecordFromHashtable(int index)
- {
- cout << "Record = "
- + dataHashed[index][0]
- +", "
- + dataHashed[index][1]
- +", "
- + dataHashed[index][2]
- +"\n";
- }
- int hash(string keyToSearch)
- {
- cout << "Searching for " << keyToSearch << " using hash method.\n";
- int foundAtIndex = hashRoutine(keyToSearch);
- cout << "Key " << keyToSearch << " is found at index " << foundAtIndex;
- cout << " after 1 read.\n";
- return foundAtIndex;
- }
- };
- int main(int argc, char* argv[])
- {
- Search *search = new Search;
- for (int ndx = 0; ndx < row; ndx++)
- {
- int generatedHashedIndex = search->hashRoutine(data[ndx][0]);
- cout << "generatedHashedIndex = " <<generatedHashedIndex << "\n";
- dataHashed[generatedHashedIndex][0] = data[ndx][0];
- dataHashed[generatedHashedIndex][1] = data[ndx][1];
- dataHashed[generatedHashedIndex][2] = data[ndx][2];
- }
- int foundAt = search->hash("Sarah Palin");
- search->displayRecordFromHashtable(foundAt);
- cin.get();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement