Question & Answer: Using division method for a hash function h and a hash table of size m,…..

Using division method for a hash function h and a hash table of size m,

store your data of student ID’s (n= 20) in an array of records not sorted.

h (k) = k mod m

m : A prime number between   2**4   < m <   2**5   and not too close

to the power of 2 (e.g. 23)

Read 20 6-digit student ID’s from array of records

And mapped them to hash table using linear probing

Print out hash table

Do minimum two searches – one with ID found and the other one with ID not found

Expert Answer

 

#include<stdio.h>//include header files

#include<conio.h>

int a[30],m,elecount,size;//DECLARE GLOBAL VARIBALE

int main() //main program

{

int ele,i,ch; //declare local varibales

void insert(int); // declare insert function

void search(int); //declare search function

printf(“enter value of m it is in between 16 and 32 and it must be a prime numbern”);

scanf(“%d”,&m); //read m value

printf(“enter no of array elements it must be less than 30n”);

scanf(“%d”,&size); //read number of elements

do

{

printf(“nMENUn1. INSERT n2. SEARCHn3. DISPLAYn 4.EXIT n”); //display the menu

printf(“enter one choicen”);

scanf(“%d”,&ch); //select your option

switch(ch) //switch sataement to select your option

{

case 1: for(i=0;i<size;i++) //repeat loop for number of elements time

{

printf(“enter element to be insertn”); //read element

scanf(“%d”,&ele); //insert at appropriate position through function call

insert(ele);

}

break;

case 2: printf(“enter element to searchn”); // identfy search element

scanf(“%d”,&ele);

search(ele); //search element in the function

break;

case 3: printf(“Hash table data is n”); //display hash table data

for(i=0;i<30;i++)

{

if(a[i]!=0)

printf(“%d = %dn”,i,a[i]);

}

break;

}

}while(ch!=4);//exit from menu and main program

}

void search(int key) //search function defination

{

int pos,flag=0,count=0; //count is to count no of elements

if(elecount==0) //represents no of elements in the array

printf(“There is no elements in the hashtable”);

else

{

pos=key%m; //indentify the pos using division method

while((a[pos]!=key)&&(count!=size)) //repeat loop until identify the position and end of the table

{

pos=(pos+1)%m; //indentify the pos using division lenear probing method

count++;

}

if(a[pos]==key) //elment found case

{

printf(“element found at loc %d “,pos);

flag=1;

}

if(flag==0) //elment not found case

printf(“element not found”);

}

}

void insert(int key) //defination to insert element

{

int pos; //check hash table full condition

if(elecount==30)

{

printf(“array is full”);

}

else

{

pos = key % m; //indentify the pos using division method

while(a[pos]!=0)

pos=(pos+1)%m;//indentify the pos using division lenear probing method

a[pos]=key;

elecount++;

}

}

Still stressed from student homework?
Get quality assistance from academic writers!