Hash Function

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • boolat
    New Member
    • Apr 2010
    • 5

    Hash Function

    hi,im doing my assignment,,but sadly i dnt knw how to do...help me please b4 19april2010
    Code a hash function, which computes the index number based on the MMU ID, which is
    a 10-digit key. The hashing algorithm used to compute the hash index is given as follows:
    • Partition the 10-digit key (yyyyyzzzzz) into two 5-digit numbers: yyyyy and
    zzzzz.
    • Add the two partitioned numbers, yyyyy and zzzzz.
    • Truncate the addition result, and keep only the last three digits.
    Use the function header given below. The function will accept an unsigned integer, ID
    which stores the MMU ID, compute accordingly and return the computed hash index.
    int hashfunc(unsign ed int ID);
    What is your MMU ID? _______________ _______
    What is the hash index based on your MMU ID? _______________ _______
  • newb16
    Contributor
    • Jul 2008
    • 687

    #2
    What does that 'b4' thingie mean? Then, how are you going to jam 10-digit number ( e.g. 9 bil. ) into unsigned int ( that is, I assume, 32-bit on most platforms)? To extract higher part, you can use modulo(%) operator.

    Comment

    • boolat
      New Member
      • Apr 2010
      • 5

      #3
      well,i have to submit by this monday..and im totally desprate..
      the 10 digit mean,if the id number entered is 1231112020.
      then divided into two parts of yyyyy and xxxxx.
      help me please..im bad in programming =((

      Comment

      • newb16
        Contributor
        • Jul 2008
        • 687

        #4
        I am somewhat aware what 10-digit number is. My question was about the choice of -- "int hashfunc(unsign ed int ID);" <- this prototype for the function - what if number entered it e.g. nine billion, that has 10 digits, but doesn't fit into unsigned int?
        >help me please..im bad in programming =((

        Try, for a beginning, to write the part of dividing them into two parts in a some math notation, without programming.

        Comment

        • boolat
          New Member
          • Apr 2010
          • 5

          #5
          well i got the hint to do formula part..
          here is my formula..but i dont knw if its correct or not..

          int hashfunc(unsign ed int ID){
          int a,b,c;
          float j,k;
          int e=0;
          j=ID/1000.0;
          a=j,".";
          b=j,"\n";
          c=a+b;
          k=c/1000.0;
          e=k,"\n";
          return e;

          for the maths part its like add yyyyy+zzzzz
          then truncate??i dont knw how..then keep the last digit..

          Comment

          • jkmyoung
            Recognized Expert Top Contributor
            • Mar 2006
            • 2057

            #6
            ?? what?
            Examples. if you have ID = aaaaaaaaab, and you want to get the value of b, we go:
            b = ID % 10;

            ID = aaaaaaaacc
            cc = ID % 100;

            ID = aaaaaaddee
            ee = ID % 100;
            dd = (ID % 10000) / 100 OR
            dd = (ID / 100) %100

            Hope this helps.

            Comment

            • newb16
              Contributor
              • Jul 2008
              • 687

              #7
              This doesn't do what it is supposed to do.
              a=j,".";
              It assigns j to a, then calculates value of pointer to "." string and discards it. It looks like that you once saw a printf() called and try to reproduce it.

              Then, there is no need to use floating point here, and divisor for the higher half should be 100000, not 1000.

              Comment

              • boolat
                New Member
                • Apr 2010
                • 5

                #8
                i dont knw how to partition the 10 digits of int 5 and 5..
                meaning for example if my id is 1234567890
                then i need to divide to (12345)yyyyy+ 67890 (zzzzz) while after that i have to add them up...

                Comment

                • newb16
                  Contributor
                  • Jul 2008
                  • 687

                  #9
                  It's result of dividing by 100000 and remainder of dividing by 100000. Then add them and apply modulo operator to get last 3 digits.

                  Comment

                  • boolat
                    New Member
                    • Apr 2010
                    • 5

                    #10
                    yes.i know.but still it did divide into two partition right??
                    correct me if im wrong

                    Comment

                    • newb16
                      Contributor
                      • Jul 2008
                      • 687

                      #11
                      No, the last code snippet you posted did not do it right.

                      Comment

                      Working...