[Show all top banners]

nepal_123
Replies to this thread:

More by nepal_123
What people are reading
Subscribers
:: Subscribe
Back to: Kurakani General Refresh page to view new replies
 Help Needed form C Programming experts
[VIEWED 5772 TIMES]
SAVE! for ease of future access.
Posted on 02-05-11 3:42 PM     Reply [Subscribe]
Login in to Rate this Post:     0       ?    
 

Hi Friends,

I am new to C Programming. I am working with Hash Functions. I am trying for

 using a simple hash function: that adda up the ascii value of each character in 'name' and returns  `sum'o modulo HASH_TABLE_SIZE

int symtbl_hash(char *name)
{
   
}

Can anyone halp me in creating this functions. Any sort of help appreciated.

Thanks in advance.

 
Posted on 02-05-11 4:21 PM     [Snapshot: 19]     Reply [Subscribe]
Login in to Rate this Post:     0       ?    
 

 I think you need to clarrify a bit. You are writing a hashfunction but you want the size of the hastable? 
Well i dont think, first of all, you should have same funtion for both hash function and hastable. 
Getting character ascii value should be easy and one googling away. So, I am trying to understand if there is more than what I understood.
 
Posted on 02-05-11 4:44 PM     [Snapshot: 38]     Reply [Subscribe]
Login in to Rate this Post:     0       ?    
 

Hi Maverick,

In order to hash for a string, I would like to add the ASCII values of all the characters in that string and call it a SUM. Then I would like to have SUM % Table_Size. Let Table size be 1024 for example.

The Function prototype is :

int hash(char *name)
{
   
}

where *name is the pointer to the address of string.



 
Posted on 02-05-11 8:17 PM     [Snapshot: 95]     Reply [Subscribe]
Login in to Rate this Post:     0       ?    
 

how abt something like below, havent compiled, dont have a compiler :)

This should  return the sum of the ascii values.

int hash(char *name)
{
    int sum = 0;
    int i;
    for (i = 0; i < strlen(name); i++)
    {
         sum  = sum + name[i];   
    }
    return sum;  
}

 
Posted on 02-05-11 8:19 PM     [Snapshot: 99]     Reply [Subscribe]
Login in to Rate this Post:     0       ?    
 

found an online compiler, dont know how long this link is valid
http://codepad.org/xNaHQtR5

This is what it returned me
ascii for A = 65


Link: http://codepad.org/xNaHQtR5    [ raw code | output | fork ] Save this paste 
Delete this paste

C, pasted 2 minutes ago: 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
int hash(char *name)
{
    int sum = 0;
    int i;
    for (i=0; i < strlen(name); i++)
    {
         sum  = sum + name[i];    
    }
    return sum;   
}

int main()
{
    int test = hash("AA");
    printf("%d", test);
    return 0;
}


Output:
1
130


happy coding..
 
Posted on 02-05-11 8:26 PM     [Snapshot: 100]     Reply [Subscribe]
Login in to Rate this Post:     0       ?    
 

I think he is trying to create a most simple version of a Hash Table, where sum of ascii values of a string is the hash code. And to get the index in the hash table, he needs to mod the sum by the table size:

prankster bro ko solution should be:

return sum%HASH_TABLE_SIZE; // Given that HASH_TABLE_SIZE is a const defined somewhere in the code

 
Posted on 02-05-11 8:30 PM     [Snapshot: 109]     Reply [Subscribe]
Login in to Rate this Post:     0       ?    
 

thats right nepalithito, just wanted to let him know how he can get the sum, the mod should be simple like you've given


 


Please Log in! to be able to reply! If you don't have a login, please register here.

YOU CAN ALSO



IN ORDER TO POST!




Within last 60 days
Recommended Popular Threads Controvertial Threads
TPS Re-registration case still pending ..
Toilet paper or water?
and it begins - on Day 1 Trump will begin operations to deport millions of undocumented immigrants
Tourist Visa - Seeking Suggestions and Guidance
From Trump “I will revoke TPS, and deport them back to their country.”
advanced parole
ढ्याउ गर्दा दसैँको खसी गनाउच
To Sajha admin
MAGA denaturalization proposal!!
How to Retrieve a Copy of Domestic Violence Complaint???
wanna be ruled by stupid or an Idiot ?
Travel Document for TPS (approved)
All the Qatar ailines from Nepal canceled to USA
NOTE: The opinions here represent the opinions of the individual posters, and not of Sajha.com. It is not possible for sajha.com to monitor all the postings, since sajha.com merely seeks to provide a cyber location for discussing ideas and concerns related to Nepal and the Nepalis. Please send an email to admin@sajha.com using a valid email address if you want any posting to be considered for deletion. Your request will be handled on a one to one basis. Sajha.com is a service please don't abuse it. - Thanks.

Sajha.com Privacy Policy

Like us in Facebook!

↑ Back to Top
free counters