avr39ripe

cppFunctionRepetition

Aug 9th, 2021
660
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <iostream>
  2.  
  3.  
  4.  
  5.  
  6. void exFun(bool dieRealy)
  7. {
  8.    
  9.     std::cout << "exFun start\n";
  10.     std::cout << "exFun main work\n";
  11.     if (dieRealy) { return; }
  12.     std::cout << "exFun stop\n";
  13. }
  14.  
  15.  
  16. int checkNumber(int num)
  17. {
  18.     if (num > 0) { return 1; }
  19.     if (num < 0) { return -1; }
  20.     return 0;
  21. }
  22.  
  23. //const int arrSize{ 10 };
  24. //int arr[arrSize]{ 1,3,-1,7,0,2,-5,0,4,-2 };
  25. //int arrStatistics[Res::resSize]{};
  26.  
  27. //std::cout << "Original array:\n";
  28. //printArr(arr, arrSize);
  29. //std::cout << "Result array:\n";
  30. //printArr(arrStatistics,Res::resSize);
  31. //arrStat(arr, arrSize, arrStatistics);
  32. //std::cout << "Result array after function call:\n";
  33. //printArr(arrStatistics, Res::resSize);
  34.  
  35. enum Res {Zero, Pos, Neg, resSize};
  36.  
  37. void arrStat(int arr[], int arrSize, int res[])
  38. {
  39.     for (int i{ 0 }; i < arrSize; ++i)
  40.     {
  41.         if (arr[i] > 0) { ++res[Res::Pos]; }
  42.         else if (arr[i] < 0) { ++res[Res::Neg]; }
  43.         else { ++res[Res::Zero]; }
  44.     }
  45. }
  46.  
  47.  
  48. int arrSumToLim(int arr[], int arrSize, int begin, int end, int maxSum)
  49. {
  50.     int sum{0};
  51.  
  52.     for (int i{ begin }; i < end; ++i)
  53.     {
  54.         if (sum + arr[i] <= maxSum)
  55.         {
  56.             sum += arr[i];
  57.         }
  58.         else
  59.         {
  60.             break;
  61.         }
  62.     }
  63.  
  64.     return sum;
  65.  }
  66.  
  67.  
  68.  
  69. double max(double a, double b)
  70. {
  71.     return a > b ? a : b;
  72. }
  73.  
  74. int max(int a, int b)
  75. {
  76.     return a > b ? a : b;
  77. }
  78.  
  79. int max(int a, int b, int c)
  80. {
  81.     return max(a, max(b, c));
  82. }
  83.  
  84. int max(int a, int b, int c, int d)
  85. {
  86.     return max(max(a, b), max(c, d));
  87.     //return max(a, max(b, c, d));
  88. }
  89.  
  90. int max(int arr[], int arrSize)
  91. {
  92.     int maxV{ arr[0] };
  93.     for (int i{ 0 }; i < arrSize; ++i)
  94.     {
  95.         //if (arr[i] > max) { max = arr[i]; }
  96.         maxV = max(maxV, arr[i]);
  97.     }
  98.     return maxV;
  99. }
  100.  
  101. void printArr(int arr[], int arrSize)
  102. {
  103.     for (int i{ 0 }; i < arrSize; ++i)
  104.     {
  105.         std::cout << arr[i] << ' ';
  106.     }
  107.  
  108.     std::cout << '\n';
  109. }
  110.  
  111.  
  112.  
  113. int main()
  114. {
  115.  
  116.     //const int arrSize{ 10 };
  117.     //int arr[arrSize]{ 1,3,-1,7,0,2,-5,0,4,-2 };
  118.     //int arrStatistics[Res::resSize]{};
  119.  
  120.     //std::cout << "Original array:\n";
  121.     //printArr(arr, arrSize);
  122.     //std::cout << "Result array:\n";
  123.     //printArr(arrStatistics,Res::resSize);
  124.     //arrStat(arr, arrSize, arrStatistics);
  125.     //std::cout << "Result array after function call:\n";
  126.     //printArr(arrStatistics, Res::resSize);
  127.  
  128.     const int arrSize{ 10 };
  129.     int arr[arrSize]{ 1,2,3,4,5,6,7,8,9,10 };
  130.  
  131.     std::cout << "Limited sum is:" << arrSumToLim(arr, arrSize, 2, 6, 23) << '\n';
  132.  
  133.     //std::cout << "Max from int " << max(5, 3) << '\n';
  134.     //std::cout << "Max from float " << max(3.1, 3.34) << '\n';
  135.  
  136.     ///*const int arrSize{ 10 };
  137.     //int arr[arrSize]{};
  138.  
  139.     //std::cout << "Original arr content:\n";
  140.     //printArr(arr, arrSize);*/
  141.  
  142.     //return 0;
  143.  
  144.     //int binNum{ 10101 };
  145.  
  146. }
  147.  
RAW Paste Data