Thread: implement hash program

  1. #1
    Registered User
    Join Date
    Apr 2002
    Posts
    28

    Smile implement hash program

    Can any one suggest just to get me started, how I can implement
    a hash function that takes a string and returns a fixed length of that string ? just a big picture ....

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >just a big picture ....
    Big picture, huh? Well then, here's a decent hash function that works well for minimizing collisions.
    Code:
    int hash ( char *hashStr, int tableSize )
    {
      int hashValue = 0, index,
          x = 31415, y = 27183;
      for ( index = 0; hashStr[index] != '\0'; index++ ) {
        hashValue = ( x * hashValue + (int)hashStr[index] ) % tableSize;
        x = x * y % ( tableSize - 1 );
      }
      return hashValue;
    }
    -Prelude
    My best code is written with the delete key.

  3. #3
    Registered User stautze's Avatar
    Join Date
    Apr 2002
    Posts
    195
    Wouldn't it be x = 31416?
    'During my service in the United States Congress, I took the initiative in creating the Internet.' - Al Gore, March 9, 1999: On CNN's Late Edition

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. BOOKKEEPING PROGRAM, need help!
    By yabud in forum C Programming
    Replies: 3
    Last Post: 11-16-2006, 11:17 PM
  2. Can someome help me with a program please?
    By WinterInChicago in forum C++ Programming
    Replies: 3
    Last Post: 09-21-2006, 10:58 PM
  3. Not sure on hash table memory allocations
    By Thumper333 in forum C Programming
    Replies: 3
    Last Post: 09-27-2004, 09:00 PM
  4. data structures / hash functions
    By rickc77 in forum C Programming
    Replies: 5
    Last Post: 11-11-2001, 01:54 PM
  5. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM