hi all do anyone understand hash function?
how to use it in order i can count a digit number of size 10? then i separated it half half (into 5 digits each)
:)
hi all do anyone understand hash function?
how to use it in order i can count a digit number of size 10? then i separated it half half (into 5 digits each)
:)
Hosam, why don't you read up on that topic at Wikipedia or in your text or class notes, and apply it into a program.
What you want to do is find out when you have it working right, and have something you can copy, and keep it as a reference if needed, later.
I'm having some trouble understanding what you mean by "half half", etc. also.
hey adak thanks for ur replied, i really dont have idea what soever of hash table even i read my online notes and kept searching in wikipedia so will u please help me to explain to me whats the hash table for? and how to use it in c?
here's a simple answer:
a "hash function" takes a string of an arbitrary and probably variable length (called the key) , and transforms it into a single integer (called the hash value) which is a smaller, fixed width value.
hash functions are widely used in cryptography and database management, for two examples.
there's an endless number of methods to write a hash function, so we can't give you an example in C without knowing the requirements. and we wouldn't anyhow, because writing the C code is your job.
so now you need to start writing some code according you your assignment, and once youve got some code written, and have a specific coherent question about the code or how to implement a particular aspect of your assignment, you can come here and maybe get an answer.
.
hi this is so far i have done, the problem now the function hashfunc doesnt work at all,,how to pass the input from user into the function?
and i googling for hash i just found the concept. what i need is the c code for it,,coz i wanna see hows the code if hash function is work into that
this is si far that ive done
#include<stdio.h>
#include<stdlib.h>
#define MHASH 10
int hashfunc(unsigned int ID);
int main(){
unsigned long int id;
int result;
float j;
printf("enter your id number:");
scanf("%ld",&id);
result=hashfunc(result);
printf("your ID : %ld and your result:%d\n",id,result);
return 0;
}
int hashfunc(unsigned int ID){
int a,b,c;
float j,k;
int e=0;
j=ID/1000;
a=strtok(j,".");
b=strtok(j,"\n");
c=a+b;
k=c/1000;
e=strtok(k,"\n");
return e;
}
and my requirement for hash functions are:
1. i have to do partition as i will get 10 digit key from user and i have to split it into half so itll became 5 digits numbers
2. i have to add the two partitioned numbers
3. last one i have to truncate the addition result so i just kept the last three digits
so far i just thought it will b normal arithmetic function.
should i use linked list? or declare or struct for it? coz im nt sure at all with my code above
okay, look at your hashfunc. i've added indentations, for readability. you should always do the same thing.
int hashfunc(unsigned int ID) { int a,b,c; float j,k; int e=0; j=ID/1000; a=strtok(j,"."); b=strtok(j,"\n"); c=a+b; k=c/1000; e=strtok(k,"\n"); return e; }
now this function, it's quite a mess. you have an integer for an ID. you try to assign it to a floating point value by dividing it by 1000. j=ID/1000;
but this doesnt work
doesnt work because ID/1000
is an integer divided by an integer, which gives an integer. that resulting integer will then be put into the float variable 'j' but you will have lost the 3 bottom digits. example ID = 123456 .... ID/1000 = 123 .... j = 123.000
. if you really need to do this, then you would do it like so: j=ID/1000.0;
, using 1000.0 forces the program to treat the result as a floating point value so example ID = 123456 .... ID/1000.0 = 123.456 .... j = 123.456
.
another problem, looking at your requirements below, is that you have a 10-digit integer as a key. an unsigned int is typically 32 bits, for a maximum value of 4 billion and change. a 10 digit value can be over 9 billion. exceeding the range causes an overflow error.
then you're trying to tokenize a floating point value? well, that's just wrong. strtok is for strings. not values.
it's pointless to carry on. this code is pretty much unrecoverable. you just need to throw this function away.
look at your requirements.
1. i have to do partition as i will get 10 digit key from user and i have to split it into half so itll became 5 digits numbers
2. i have to add the two partitioned numbers
3. last one i have to truncate the addition result so i just kept the last three digits
its a very simple hash function. let me describe it in plain language:
(1) partition (or divide) your 10-digit input key into two 5-digit halves.
(2) add the two halves together into a sum. ... note that this sum may be 5 or 6 digits, but it doesnt matter.
(3) take the last three digits of the sum, return this 3-digit number as the result.
here's the prototype i suggest
int hashFunction( char keyString[] )
this means you take a character array (the 10 character key) as a string input, passing by value, and you return a pointer to an integer (which will be the value between 0 and 999) as the result. there are other valid ways to do it, this is just how i recommend, as it's simple and to the point.
here's a partial solution in psuedo code
int hashFunction( char keyString[] )
{
declare: char part1[6], part2[6] // 5 digits plus terminating NULL
declare: int part1_val, part2_val, sumParts, result
check: length (keyString) == 10
if false
print error message
return -1
if true
char part1 = keyString[first 5 chars]
char part2 = keyString[last 5 chars]
convert: char part1 -> int part1_val
convert: char part2 -> int part2_val
sumParts = part1_val + part2_val
... get the last three digits of sumParts
... put it into result
... this up to you to figure out
return result
}
your job is to code it in C. let me give you a hint. get familiar with the "sprintf()" function and the "atol()" function: sprintf(myString, "%d", myInteger);
myInteger = atol(myString);
ive never learnt about sprintf before besides my lecture told me this should be really easy less than 10 lines. and i think i will just use my first function prototype to define the hashfunction,,will u give me a hint how to work on this function protytpe:
int hashfunc(unsigned int ID)
should i realy use array for this?
ive never learnt about sprintf before
well, now's your chance to learn another basic fundamental function in the C library, isn't it? It's tremendously easier to use and more widely applicable than that "strtok()" function you were abusing.
besides my lecture told me this should be really easy less than 10 lines.
yeah, that sounds about right. the pseudo-code i posted is overly verbose, only to explain what is going on; it doesn't not make a 1:1 mapping of lines of actual code.
and i think i will just use my first function prototype to define the hashfunction,,will u give me a hint how to work on this function protytpe:
int hashfunc(unsigned int ID)
hey man, you can do whatever the hell you want, but there's no way you're going fit the full range of a 10-digit number into an unsigned int. well over half of the possible values will cause an overflow.
so, what part of this don't you understand?
should i realy use array for this?
no, you should not.
i've already given you a solution. now you can do it the way i've suggested, or you can come up with your own. in any event, you need to start coding.
.
alright ive read about sprintf seems it will make my code lots of easier :) thanks alot