The world’s Largest Sharp Brain Virtual Experts Marketplace Just a click Away
Levels Tought:
Elementary,Middle School,High School,College,University,PHD
| Teaching Since: | Jul 2017 |
| Last Sign in: | 337 Weeks Ago, 2 Days Ago |
| Questions Answered: | 15833 |
| Tutorials Posted: | 15827 |
MBA,PHD, Juris Doctor
Strayer,Devery,Harvard University
Mar-1995 - Mar-2002
Manager Planning
WalMart
Mar-2001 - Feb-2009
Need help debugging my code (C++). Getting error "C4703 potentially uninitialized local pointer variable 'htable' used"
Please comment code where you fixed it; I can not get it to work as its supposed to.
code attached#include <iostream>
#include <cstdlib>
#define MIN_TABLE_SIZE 10
using namespace std;
enum EntryType { Legitimate, Empty, Deleted };
struct HashNode {
int element;
enum EntryType info;
};
struct HashTable {
int size;
HashNode *table;
};
bool isPrime(int n) {
if (n == 2 || n == 3)
return true;
if (n == 1 || n % 2 == 0)
return false;
for (int i = 3; i * i <= n; i += 2)
if (n % i == 0)
return false;
return true;
}
int nextPrime(int n) {
if (n <= 0)
n = 3;
if (n % 2 == 0)
n++;
for (; !isPrime(n); n += 2);
return n;
}
int HashFunc(int key, int size) {
return key % size;
}
HashTable *initializeTable(int size) {
HashTable *htable;
if (size < MIN_TABLE_SIZE) {
cout << "Table Size Too Small" << endl;
return NULL;
}
htable = new HashTable;
if (htable == NULL) {
cout << "Out of Space" << endl;
return NULL;
}
htable->size = nextPrime(size);
htable->table = new HashNode[htable->size];
if (htable->table == NULL) {
cout << "Table Size Too Small" << endl;
return NULL;
}
for (int i = 0; i < htable->size; i++) {
htable->table[i].info = Empty;
htable->table[i].element = NULL;
}
return htable;
}
int Find(int key, HashTable *htable) {
int pos = HashFunc(key, htable->size);
int collisions = 0;
while (htable->table[pos].info != Empty && htable->table[pos].element != key) {
pos = pos + 2 * ++collisions - 1;
if (pos >= htable->size)
pos = pos - htable->size;
}
return pos;
}
void Insert(int key, HashTable *htable) {
int pos = Find(key, htable);
if (htable->table[pos].info != Legitimate) {
htable->table[pos].info = Legitimate;
htable->table[pos].element = key;
}
}
HashTable *Rehash(HashTable *htable) {
int size = htable->size;
HashNode *table = htable->table;
htable = initializeTable(2 * size);
for (int i = 0; i < size; i++) {
if (table[i].info == Legitimate)
Insert(table[i].element, htable);
}
free(table);
return htable;
}
void Retrieve(HashTable *htable) {
for (int i = 0; i < htable->size; i++) {
int value = htable->table[i].element;
if (!value)
cout << "Position: " << i + 1 << " Element: Null" << endl;
else
cout << "Position: " << i + 1 << " Element: " << value << endl;
}
}
int main() {
int value, size, i = 1;
int choice;
HashTable *htable;
while (1) {
cout << "Quadratic Probing\n" << endl;
cout << "1.Size Of Table" << endl;
cout << "2.Insert Element" << endl;
cout << "3.Display Hash Table" << endl;
cout << "4.Rehash The Table" << endl;
cout << "5.Exit\n" << endl;
cout << "Enter your choice: ";
cin >> choice;
switch (choice) {
case 1:
cout << "Enter size of the Hash Table: ";
cin >> size;
htable = initializeTable(size);
cout << "Size of Hash Table is " << nextPrime(size);
break;
case 2:
if (i > htable->size) {
cout << "Table is Full" << endl;
continue;
}
cout << "Enter element to be inserted: ";
cin >> value;
Insert(value, htable);
i++;
break;
case 3:
Retrieve(htable);
break;
case 4:
htable = Rehash(htable);
break;
case 5:
exit(1);
default:
cout << "\nChoice NOT valid\n";
}
}
return 0;
}
----------- ----------- H-----------ell-----------o S-----------ir/-----------Mad-----------am ----------- Th-----------ank----------- yo-----------u f-----------or -----------you-----------r i-----------nte-----------res-----------t a-----------nd -----------buy-----------ing----------- my----------- po-----------ste-----------d s-----------olu-----------tio-----------n. -----------Ple-----------ase----------- pi-----------ng -----------me -----------on -----------cha-----------t I----------- am----------- on-----------lin-----------e o-----------r i-----------nbo-----------x m-----------e a----------- me-----------ssa-----------ge -----------I w-----------ill----------- be----------- qu-----------ick-----------ly