Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

can anybody debug this program for me please #include <iostream> #include<fstrea

ID: 3731688 • Letter: C

Question

can anybody debug this program for me please

#include <iostream>

#include<fstream>

#include <cstdlib>

#define MIN_TABLE_SIZE 10

using namespace std;

struct hashNode{

string stateName;

hashNode *next;

};

/*

* Node Type Declaration

*/

enum EntryType {Legitimate, Empty, Deleted};

/*

* Node Declaration

*/

struct HashNode

{

int element;

enum EntryType info;

};

/*

* Table Declaration

*/

struct HashTable

{

int size1;

HashNode *table;

};

/*

* Returns whether n is prime or not

*/

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;

}

/*

* Finding next prime size of the table

*/

int nextPrime(int n)

{

if (n <= 0)

n == 3;

if (n % 2 == 0)

n++;

for (; !isPrime( n ); n += 2);

return n;

}

/*

* Function To Generate Hash

*/

int HashFunc(int key, int size1)

{

return key % size1;

}

/*

* Function to Initialize Table

*/

HashTable *initializeTable(int size1)

{

HashTable *htable;

if (size1 < 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->size1 = nextPrime(size1);

htable->table = new HashNode [htable->size1];

if (htable->table == NULL)

{

cout<<"Table Size Too Small"<<endl;

return NULL;

}

for (int i = 0; i < htable->size1; i++)

{

htable->table[i].info = Empty;

htable->table[i].element = NULL;

}

return htable;

}

/*

* Function to Find Element at a key

*/

int Find(int key, HashTable *htable)

{

int pos = HashFunc(key, htable->size1);

int collisions = 0;

while (htable->table[pos].info != Empty &&

htable->table[pos].element != key)

{

pos = pos + 2 * ++collisions -1;

if (pos >= htable->size1)

pos = pos - htable->size1;

}

return pos;

}

/*

* Function to Insert Element into a key

*/

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;

}

}

/*

* Function to Rehash the Table

*/

HashTable *Rehash(HashTable *htable)

{

int size1 = htable->size1;

HashNode *table = htable->table;

htable = initializeTable(2 * size1);

for (int i = 0; i < size1; i++)

{

if (table[i].info == Legitimate)

Insert(table[i].element, htable);

}

free(table);

return htable;

}

/*

* Function to Retrieve Hash Table

*/

void Retrieve(HashTable *htable)

{

for (int i = 0; i < htable->size1; i++)

{

int value = htable->table[i].element;

if (!value)

cout<<"Position: "<<i + 1<<" Element: Null"<<endl;

else

cout<<"Position: "<<i + 1<<" Element: "<<value<<endl;

}

}

/*

* Main Contains Menu

*/

int main()

{

hashNode *array[15];

for (int j=0; j<15; j++)

{

array[j]->stateName =" ";

array[j]->next=NULL;

}

int size=0;

string cName;

ifstream myfile;

myfile.open("a3.txt");

myfile>>cName;

while(!myfile.eof())

{

size=sizeof(cName);

if (array[size]->stateName != " ")

{

hashNode *temp=new hashNode();

temp->stateName=cName;

temp->next=NULL;

if(array[size]->next==NULL)

{

array[size]->next=temp;

}

else

{

hashNode *temp2;

temp2=array[size];

while(temp2->next!=NULL)

{

temp2=temp2->next;

}

temp2->next=temp;

}

}

else{

array[size]->stateName=cName;

array[size]->next=NULL;

}

myfile>>cName;

}

for(int i=0; i<15;i++)

{

cout<<i<<" ";

while(array[i]->next!=NULL)

{

cout<<array[i]->stateName<<" ";

}

cout<<endl;

}

int value, size1, pos, i = 1;

int choice;

HashTable *htable;

while(1)

{

cout<<" ----------------------"<<endl;

cout<<"Operations on Quadratic Probing"<<endl;

cout<<" ----------------------"<<endl;

cout<<"1.Initialize size of the table"<<endl;

cout<<"2.Insert element into the table"<<endl;

cout<<"3.Display Hash Table"<<endl;

cout<<"4.Rehash The Table"<<endl;

cout<<"5.Exit"<<endl;

cout<<"Enter your choice: ";

cin>>choice;

switch(choice)

{

case 1:

cout<<"Enter size of the Hash Table: ";

cin>>size1;

htable = initializeTable(size1);

cout<<"Size of Hash Table: "<<nextPrime(size1);

break;

case 2:

if (i > htable->size1)

{

cout<<"Table is Full, Rehash the table"<<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<<" Enter correct option ";

}

}

return 0;

}

Explanation / Answer

Hi, I executed your program and tried to find what's wrong with that so every time it telling that there is a segmentation fault.

now I will tell you what exactly segmentation fault means.

------>The segmentation fault is a common condition that occurs and then program crash unexpectedly in another way it stops.and the smooth execution of the program is not done.

------->The smooth execution of the program is very important

------->why these happen so when our program trying to read or write the illegal memory location actually the program's memory is divided into various segments.

---------->the text segments are defined for the program at compile time when we declare the variable.various arrays and variables are defined at compile time.the stack segment for temporary variables is defined at compile time.

-------->And the memory allocated at runtime is acquired by using malloc()

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Chat Now And Get Quote