Implementation of hashing algorithm with arrays and c++ strings - Segmentation fault (core dumped) -


i'm getting "segmentation fault (core dumped)" error, when hash-function gives similar hash-values. both "hash" , "hash" functions should stay way are. "keyvalue" part of assignment, not exact implementation, numbers should upper case (hence "toupper" function) , start @ 1 (for example a=1, b=2 etc.). guess "hashinsert" 1 making problems, unfortunately couldn't figure out on own. (and should use arrays)

#include <iostream> #include <string>  using namespace std;  /**********************/ int toupper( int ); int keyvalue( int ); int hash( int ); int hash( int ); int hashvalue( string, int ); void hashinsert( string[], string ); /**********************/  const int days = 7; string week[days];  int keyvalue( int ch ){     return toupper(ch) - 64; } int toupper( int ch ){     if( ch >= 92 && ch <= 122 )         return ch - 32;     return ch; } int hash( int ch ){     return keyvalue( ch ) % days; } int hash( int ch ){     return 1 + (keyvalue(ch) % (days-2)); } int hashvalue( string key, int ){     return (hash(key[i]) + i*hash(key[i]) % days); } void hashinsert( string table[], string key ){     int pos = 0;     for(int i=0; < key.length(); i++){         pos = hashvalue( key, );         if( (table[pos]).empty() ){             table[pos] = key;             break;         }     } }  /*=================== main ===================*/ int main( int argc, char* argv[] ){      hashinsert( week, "monday" ); //  hashinsert( week, "tuesday" ); //  hashinsert( week, "wednesday" );     hashinsert( week, "thursday" ); //  hashinsert( week, "friday" );     hashinsert( week, "saturday" );     hashinsert( week, "sunday" );      cout << "0: " << week[0] << endl;        cout << "1: " << week[1] << endl;     cout << "2: " << week[2] << endl;     cout << "3: " << week[3] << endl;     cout << "4: " << week[4] << endl;     cout << "5: " << week[5] << endl;     cout << "6: " << week[6] << endl;       return 0; } 

hash(key[i]) + i*hash(key[i]) % days should (hash(key[i]) + i*hash(key[i])) % days.

you're accessing elements of week way further week[6].


Comments

Popular posts from this blog

commonjs - How to write a typescript definition file for a node module that exports a function? -

openid - Okta: Failed to get authorization code through API call -

thorough guide for profiling racket code -