There is such a program. It should receive a string at the input and write to the array using the hashing method.
#include <iostream> using namespace std; class HashEntry { private: int key; char value[32]; public: HashEntry(int key, char value[32]) { this->key = key; strcpy_s(this->value,value); } int getKey() { return key; } char* getValue() { return value; } void setKey(int key) { this->key= key; } void setValue(char value[32]) { strcpy_s(this->value, value); } }; int hashFunc(char h[32]) { int l = strlen(h); int k = ((int)h[0] + (int)h[l-1])%100; return k; } int reHashFunc(int k) { return(k*2)%100; } int main() { setlocale(LC_ALL, "Russian"); char* arr[100]; char x[32]; int b = 2; int c = 0; for (int i = 99; i > 0; i--) { arr[i] = NULL; } do{ cin >> x; if (arr[b] != NULL) cout << "arr[" << b << "]= " << arr[b] << endl; b = hashFunc(x); cout << "Hash function = " << b << endl; if (arr[b] == NULL) { arr[b] = x; cout << "ΠΠ»Π΅ΠΌΠ΅Π½Ρ Π·Π°ΠΏΠΈΡΠ°Π½, ΠΊΠΎΠ΄ - " << b << endl; } else { if (strcmp(arr[b], x) == 0)cout << "Π’Π°ΠΊΠΎΠΉ ΡΠ»Π΅ΠΌΠ΅Π½Ρ ΡΠΆΠ΅ Π΅ΡΡΡ1" << endl; else{ c = reHashFunc(b); cout << "Rehash function = " << c << endl; if (arr[c] == NULL) { arr[c] = x; cout << "ΠΠ»Π΅ΠΌΠ΅Π½Ρ Π·Π°ΠΏΠΈΡΠ°Π½, ΠΊΠΎΠ΄ - " << c << endl; } else { if (strcmp(arr[c], x) == 0)cout << "Π’Π°ΠΊΠΎΠΉ ΡΠ»Π΅ΠΌΠ΅Π½Ρ ΡΠΆΠ΅ Π΅ΡΡΡ2" << endl; else{ while (c != b){ c = reHashFunc(c); cout << "Rehash function = " << c << endl; if (arr[c] == NULL){ arr[c] = x; cout << "ΠΠ»Π΅ΠΌΠ΅Π½Ρ Π·Π°ΠΏΠΈΡΠ°Π½, ΠΊΠΎΠ΄ - " << c << endl; break; } if (strcmp(arr[c], x) == 0){cout << "Π’Π°ΠΊΠΎΠΉ ΡΠ»Π΅ΠΌΠ΅Π½Ρ ΡΠΆΠ΅ Π΅ΡΡΡ3" << endl; break;} } if (c == b){ cout << "ΠΠ΅Π²ΠΎΠ·ΠΌΠΎΠΆΠ½ΠΎ ΠΈΠ΄Π΅Π½ΡΠΈΡΠΈΠΊΠ°ΡΠΎΡ"; } } } } } if (arr[b] != NULL) cout << "arr[" << b << "]= " << arr[b] << endl; } while (strcmp(x, "stop") != 0); do { int b = 0; int c = 0; cin >> x; b = hashFunc(x); if (arr[b] != NULL) cout << "arr[" << b << "]= " << arr[b] << endl; else { c = reHashFunc(b); if (arr[c] != NULL) cout << "arr[" << c << "]= " << arr[c] << endl; else cout << "ΠΠ΅Ρ ΡΠ°ΠΊΠΎΠΉ"; } } while (strcmp(x, "stop") != 0); system("pause"); return 0; } For some reason, in this loop, the value of arr [b] is assigned by the xth immediately after it is entered. I can not understand for what reason it happens.
do{ cin >> x; if (arr[b] != NULL) cout << "arr[" << b << "]= " << arr[b] << endl; b = hashFunc(x); cout << "Hash function = " << b << endl; if (arr[b] == NULL) { arr[b] = x; cout << "ΠΠ»Π΅ΠΌΠ΅Π½Ρ Π·Π°ΠΏΠΈΡΠ°Π½, ΠΊΠΎΠ΄ - " << b << endl; } else { if (strcmp(arr[b], x) == 0)cout << "Π’Π°ΠΊΠΎΠΉ ΡΠ»Π΅ΠΌΠ΅Π½Ρ ΡΠΆΠ΅ Π΅ΡΡΡ1" << endl; else{ c = reHashFunc(b); cout << "Rehash function = " << c << endl; if (arr[c] == NULL) { arr[c] = x; cout << "ΠΠ»Π΅ΠΌΠ΅Π½Ρ Π·Π°ΠΏΠΈΡΠ°Π½, ΠΊΠΎΠ΄ - " << c << endl; } else { if (strcmp(arr[c], x) == 0)cout << "Π’Π°ΠΊΠΎΠΉ ΡΠ»Π΅ΠΌΠ΅Π½Ρ ΡΠΆΠ΅ Π΅ΡΡΡ2" << endl; else{ while (c != b){ c = reHashFunc(c); cout << "Rehash function = " << c << endl; if (arr[c] == NULL){ arr[c] = x; cout << "ΠΠ»Π΅ΠΌΠ΅Π½Ρ Π·Π°ΠΏΠΈΡΠ°Π½, ΠΊΠΎΠ΄ - " << c << endl; break; } if (strcmp(arr[c], x) == 0){cout << "Π’Π°ΠΊΠΎΠΉ ΡΠ»Π΅ΠΌΠ΅Π½Ρ ΡΠΆΠ΅ Π΅ΡΡΡ3" << endl; break;} } if (c == b){ cout << "ΠΠ΅Π²ΠΎΠ·ΠΌΠΎΠΆΠ½ΠΎ ΡΠ°Π·ΠΌΠ΅ΡΡΠΈΡΡ ΠΈΠ΄Π΅Π½ΡΠΈΡΠΈΠΊΠ°ΡΠΎΡ"; } } } } } if (arr[b] != NULL) cout << "arr[" << b << "]= " << arr[b] << endl; } while (strcmp(x, "stop") != 0);