Advertisement
Toliak

lab5

Oct 26th, 2018
309
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.90 KB | None | 0 0
  1. #include <iostream>
  2. #include <iomanip>
  3. #include <string>
  4.  
  5. bool compare(double number)
  6. {
  7.     return std::to_string(number)[0] == '9';
  8. }
  9.  
  10. size_t count(double *array, size_t size, bool (*functor)(double))
  11. {
  12.     size_t result = 0;
  13.     for (size_t i=0;i<size;i++){
  14.         if (functor(array[i]))
  15.             result++;
  16.     }
  17.     return result;
  18. }
  19.  
  20. int main(int argc, char *argv[])
  21. {
  22.     if (argc < 2) {
  23.         std::cout << "Wrong amount of arguments" << std::endl;
  24.         return 0;
  25.     }
  26.     // First argv element - path to programm
  27.     size_t arraySize = atoi(argv[1]);
  28.     auto array = new double[arraySize];
  29.     for (int i= 0;i<arraySize;i++){
  30.         array[i] = (rand() % 1000) / 100.;
  31.         std::cout << "Element " << std::setw(3) << i << ": " << std::setw(10) << array[i] << std::endl;
  32.     }
  33.  
  34.     std::cout << "Result: " << count(array, arraySize, compare) << std::endl;
  35.     return 0;
  36. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement