Advertisement
Guest User

Untitled

a guest
Apr 6th, 2020
175
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.30 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. inline double area(double a, double h) {
  6.     return (a * h) / 2;
  7. }
  8.  
  9. unsigned int sum(unsigned int *arr, size_t size) {
  10.     if (size > 0)
  11.         return arr[size - 1] + sum(arr, size - 1);
  12.     return 0;
  13. }
  14.  
  15. unsigned int sum(unsigned int first, unsigned int step, unsigned int count) {
  16.     if (count > 1) {
  17.         return first + sum(first + step, step, count - 1);
  18.     }
  19.     return first;
  20. }
  21.  
  22. float sum(float first, float step, unsigned int count) {
  23.     if (count > 1) {
  24.         return first + sum(first + step, step, count - 1);
  25.     }
  26.     return first;
  27. }
  28.  
  29. double sum(double first, double step, unsigned int count) {
  30.     if (count > 1) {
  31.         return first + sum(first + step, step, count - 1);
  32.     }
  33.     return first;
  34. }
  35.  
  36. template<class T, size_t SIZE = 0>
  37. void maximum(T arr[SIZE]) {
  38.     size_t maxi = 0;
  39.    
  40.     for (size_t i = 0; i < SIZE; ++i) {
  41.         if (arr[i] > arr[maxi])
  42.             maxi = i;
  43.     }
  44.  
  45.     T t = arr[0];
  46.     arr[0] = arr[maxi];
  47.     arr[maxi] = t;
  48. }
  49.  
  50. int main()
  51. {
  52.     unsigned int arr[] = { 1, 2, 3 };
  53.     std::cout << area(10., 5.) << std::endl;
  54.     std::cout << sum(arr, 3) << std::endl;
  55.     std::cout << sum(5U, 1U, 5U) << std::endl;
  56.     std::cout << sum(0.5f, 0.1f, 5) << std::endl;
  57.     std::cout << sum(0.5, 0.1, 5) << std::endl;
  58.     maximum<unsigned int, 3>(arr);
  59.     std::cout << arr[0] << std::endl;
  60.     return 0;
  61. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement