Advertisement
Guest User

Untitled

a guest
Nov 17th, 2018
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.18 KB | None | 0 0
  1. #include <iostream>
  2. #include <random>
  3. #include <iomanip>
  4. using namespace std;
  5. using box_t = pair<int*, const size_t>;
  6. class Array {
  7. public:
  8.     static void fill(box_t& box, const int a, const int b) {
  9.         auto[sequence, length] = box;
  10.         for (auto i = 0U; i < length; ++i) sequence[i] = generate(a, b);
  11.     }
  12.     static void show(const box_t& box, const streamsize width = 2U) {
  13.         auto[sequence, length] = box;
  14.         for (auto i = 0U; i < length; ++i) cout << setw(width) << sequence[i];
  15.         cout.put('\n');
  16.     }
  17.     static void replace(box_t& box, const int dest, const int src) {
  18.         auto[sequence, length] = box;
  19.         for (auto i = 0U; i < length; ++i) if (sequence[i] == dest) sequence[i] = src;
  20.     }
  21. private:
  22.     static int generate(int a, int b) {
  23.         if (a > b) swap(a, b);
  24.         const uniform_int_distribution<> uid(a, b);
  25.         random_device rd;
  26.         mt19937 gen(rd());
  27.         return uid(gen);
  28.     }
  29. };
  30. int main() {
  31.     int sequence[10U];
  32.     box_t box{ sequence, size(sequence) };
  33.     Array::fill(box, 7, 14);
  34.     Array::show(box, 4U);
  35.     Array::replace(box, 7, 0);
  36.     Array::show(box, 4U);
  37.     system("pause");
  38. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement