Design a hash table to store phone #s. Your job is to write a hash function that has a parameter username, and generate a key. Username is unique, length 5 and can be A-Z, 0-9, space. Write a hash function that generate keys without collisions and use minimum memory.


let's try to understand problem:



code to generate hash function:
#include <stdio.h> 
#include <math.h>
char arr[100];
int i,j=4,total=0;
double x;
/* this function will generate hashcode */
void hashcode()
{
for(i=0;i<5;i++)
{
if(arr[i]==' ') // to caovert space to 0
x=0;

if(arr[i]>='0'&& arr[i]<='9') // to convert ascii of 0-9 to 1-10
x=(arr[i]-47);

if(arr[i]>='A'&& arr[i]<='Z') // to convert ascii of A-Z to 11-36
x=(arr[i]-54);

x=x*pow(37, j);
total=total+(int)x;
j--;
}
}
int main()
{
printf("enter the usename of length 5\n");
for(i=0;i<5;i++)
scanf("%c",&arr[i]);
hashcode();
printf("\n%d",total);
return 0;
}


output:-
enter the usename of length 5
Z10 A

67572482
----------------------------------------------
Z10 A=36*37^(4)+2*37^(3)+1*37^(2)+0*37^(1)+11*37^(0)
refer table givien below





0 comments: