avr39ripe

cppCountEvenOddArr

Aug 4th, 2021
702
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <iostream>
  2.  
  3. void fillArr(int arr[], int arrSize, int begin, int end)
  4. {
  5.     for (int i{ 0 }; i < arrSize; ++i)
  6.     {
  7.         arr[i] = rand() % (end - begin) + begin;
  8.     }
  9. }
  10.  
  11. void printArr(int arr[], int arrSize)
  12. {
  13.     for (int i{ 0 }; i < arrSize; ++i)
  14.     {
  15.         std::cout << arr[i] << ' ';
  16.     }
  17.  
  18.     std::cout << '\n';
  19. }
  20.  
  21. bool isEven(int num)
  22. {
  23.     return num % 2 == 0;
  24. }
  25.  
  26. bool isOdd(int num)
  27. {
  28.     return num % 2 != 0;
  29. }
  30.  
  31. int countElements(int arr[], int arrSize, bool even = false)
  32. {
  33.     int count{ 0 };
  34.     for (int i{ 0 }; i < arrSize; ++i)
  35.     {
  36.         if ((even and isEven(arr[i])) or (!even and isOdd(arr[i])) ) { ++count; }
  37.     }
  38.     return count;
  39. }
  40.  
  41. int main()
  42. {
  43.     const int arrSize{ 10 };
  44.     int arr[arrSize]{};
  45.  
  46.     std::cout << "Original arr content:\n";
  47.     printArr(arr, arrSize);
  48.     fillArr(arr, arrSize, 0, 10);
  49.     std::cout << "Arr filled by random numbers:\n";
  50.     printArr(arr, arrSize);
  51.     std::cout << "There are " << countElements(arr, arrSize) << " elements\n";
  52.  
  53.     return 0;
  54. }
  55.  
RAW Paste Data