NickAndNick

Циклический обход массива

May 27th, 2018
110
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <iostream>
  2. using namespace std;
  3. template<typename Type>
  4. int right(
  5.     const Type* box,
  6.     const size_t size,
  7.     const size_t start,
  8.     const size_t step
  9. ) {
  10.     return box[(start + step) % size];
  11. }
  12. template<typename Type>
  13. int left(
  14.     const Type* box,
  15.     const size_t size,
  16.     const size_t start,
  17.     size_t step
  18. ) {
  19.     step %= size;
  20.     return box[(size + start - step) % size];
  21. }
  22. int main() {
  23.     int box[] = { 1, 2, 9, 5, 3, 6, 8 };
  24.     const auto size = sizeof(box) / sizeof(box[0]);
  25.     const auto index = 6U;
  26.     const auto step = 10U;
  27.     cout
  28.         << right(box, size, index, step) << '\n'
  29.         << left(box, size, index, step)
  30.         << endl;
  31.     system("pause");
  32. }
RAW Paste Data