Hash Table String value

I am trying to make a hashstring function that:
sum the ordinal values of the characters of the variable multiplied by their position in the string (1-indexing), then taking the modulo by TABLESIZE.

ie. The variable ABC = (65 * 1 + 66 * 2 + 67 * 3) % TABLESIZE




The output I am trying to get is:


   VAR BORAMIR = 25	
   VAR LEGOLAS = 101	
   PRINT 	BORAMIR IS 25
   BORAMIR ++	
	
   PRINT LEGOLAS	LEGOLAS IS 101
   PRINT GANDALF	GANDALF IS UNDEFINED
   PRINT BORAMIR * 2	BOARAMIR * 2 IS 52




So far, my output is:

Start
    BORAMIR = 16
    LEGOLAS = 19
    GANDALF = 93
    BORAMIR * 2 = 80


My hashstring function is :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
unsigned int hashString( const string& s) 
	{
      
	  int sum = 0;
        for (int k = 0; k < s.length(); k++)
            sum = sum + int(s[k]);
        return  sum % 100; 
   }

    int main()
    {
        cout<<"Start"<<endl;

        
        cout<<"    BORAMIR = "<<hashString("Boramir")<<endl;
        cout<<"    LEGOLAS = "<<hashString("LEGOLAS")<<endl;
        cout<<"    GANDALF = "<<hashString("GANDALF")<<endl;
        cout<<"    BORAMIR * 2 = "<<hashString("BORAMIR * 2")<<endl;






I am assuming I am using the wrong table size? I am not sure how to determine the table size for this problem? Any suggestions would be greatly appreciate!
closed account (E3h7X9L8)
you forgot to add multiplication for each term of sum
sum = sum + int(s[k]) * (k+1);

i'm not familiar with hash tables so I don't know what tablesize means
closed account (48T7M4Gy)
You might also need a second ( const int ) parameter in the hashString function to accommodate the table size instead of hard-coding it as 100.
Topic archived. No new replies allowed.