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); 

    1 answer 1

    This is because you assign to the elements the value of a pointer to the same array allocated on the stack x instead of allocating a new buffer. Use better std::vector<std::string>> .

    • And is it possible to simply write to the element value from the array? - ENTERF34R
    • If only dynamically allocate a new array every time, copy the data into it and assign a pointer to this new array, remembering to free up memory at the end of the program. - VTT
    • Is it possible to free the memory from under the element declared in the loop at the end of the program? Simply, if you do this in the loop itself, the data is not saved, and if you declare an array outside the loop, then the initial error is returned. - ENTERF34R
    • @ ENTERF34R You can, but the main thing is not to forget to do it. - VTT