[ create a new paste ] login | about

Link: http://codepad.org/IBkLOVky    [ raw code | output | fork ]

C++, pasted on Jun 5:
#include <ctime>
#include <cstdlib>
#include <algorithm>
#include <iterator>
#include <iostream>

class Random {
public:
  Random(int min, int max)
  : min(min)
  , max(max) {
    std::srand(std::time(NULL));
  }

  int operator()() {
    return min + std::rand() % (max - min);
  }
private:
  int min;
  int max;
};

int main() {
  const size_t size  = 200;
        int    arr[size];
  const int    min   =-50;
  const int    max   = 50;

  std::generate(arr, arr + size, Random(min, max + 1));
  
  int pair_count = 0;
  for (size_t i = 1; i < size; ++i)
    if (arr[i - 1] == arr[i])
      ++pair_count;

  std::cout << "Array: " << std::endl;
  std::copy(arr, arr + size, std::ostream_iterator<int>(std::cout, " "));
  std::cout << std::endl;

  std::cout << "Pair count: " << pair_count << std::endl;
  
  return 0;
}


Output:
1
2
3
Array: 
26 18 -16 -31 4 -33 -3 -18 21 15 -31 41 -18 7 -46 -10 45 -40 47 4 29 23 13 -44 -44 -18 -10 35 20 -29 -32 -5 5 -31 15 -42 -14 28 -10 23 -42 -25 -20 -10 33 -16 31 -7 -6 -7 47 -28 -35 -25 45 38 7 -16 -12 -24 5 6 -13 -41 -9 18 35 -6 -5 41 33 -31 33 -38 -24 15 -4 23 -43 6 -35 20 -6 47 11 -11 34 -33 23 38 -41 45 -7 -4 20 1 -36 -30 45 -25 -39 27 -5 44 40 -13 -26 -49 -41 48 8 41 -17 -49 37 44 -10 -14 -40 30 40 -31 24 -18 -19 -41 33 -5 -20 -7 21 -43 -30 -35 17 26 2 7 27 -23 4 -16 -33 3 -48 20 -4 -8 22 -28 38 11 -43 -23 9 -12 -13 -42 34 -17 1 4 -44 -13 -15 23 -38 3 -21 6 30 -1 6 13 -49 8 -18 -3 17 20 -15 4 -20 -8 32 5 30 35 13 13 -33 31 33 40 -33 -33 12 47 20 7 
Pair count: 3


Create a new paste based on this one


Comments: