Загрузка данных


#include <iostream>
#include <cstdlib>
#include <ctime>

const int N = 12;
const int M = 15;
const int LOW = 23000;
const int HIGH = 45000;

void sv_add(int value, int m, int* H) {
    int i = value % m;
    while (H[i] != -1) {
        i = (i + 1) % m;
    }
    H[i] = value;
}

int sv_search(int value, int m, const int* H) {
    int i = value % m;
    int start = i;
    do {
        if (H[i] == value) return i;
        if (H[i] == -1) return -1;
        i = (i + 1) % m;
    } while (i != start);
    return -1;
}

int main() {
    std::srand(static_cast<unsigned>(std::time(nullptr)));
    int mas[N];
    for (int i = 0; i < N; ++i) {
        mas[i] = LOW + std::rand() % (HIGH - LOW + 1);
        std::cout << mas[i] << " ";
    }
    std::cout << std::endl;

    int H[M];
    for (int i = 0; i < M; ++i) H[i] = -1;

    for (int i = 0; i < N; ++i) sv_add(mas[i], M, H);

    std::cout << std::endl;
    for (int i = 0; i < M; ++i) std::cout << "H[" << i << "] = " << H[i] << std::endl;

    std::cout << std::endl << "Poisk elementa (dlya vyhoda vvedite -1):" << std::endl;
    int key;
    while (true) {
        std::cout << "Vvedite klyuch: ";
        if (!(std::cin >> key)) {
            std::cin.clear();
            std::cin.ignore(10000, '\n');
            std::cout << "Oshibka vvoda. Vvedite celoe chislo." << std::endl;
            continue;
        }
        if (key == -1) break;
        int idx = sv_search(key, M, H);
        if (idx == -1) std::cout << "Net elementa" << std::endl;
        else std::cout << "Element " << H[idx] << " naiden v pozicii " << idx << std::endl;
    }
    return 0;
}