Advertisement
al3taibi

Untitled

May 30th, 2022
752
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.29 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. template<typename Type>
  6. Type sum(Type array[], int N) {
  7.     Type sum = 0;
  8.     for (int i = 0; i < N; i++)
  9.     {
  10.         if (array[i] > 0)
  11.         {
  12.             sum += array[i];
  13.         }
  14.     }
  15.     return sum;
  16. }
  17.  
  18. template<typename Type>
  19. Type multiple(Type array[], int N) {
  20.     Type max = -1, min = abs(array[0]), product = 1;
  21.     int max_i, min_i = 0, i, a;
  22.     for (i = 0; i < N; i++) {
  23.         a = abs(array[i]);
  24.         if (a > max) {
  25.             max_i = i;
  26.             max = a;
  27.         } else if (a < min) {
  28.             min_i = i;
  29.             min = a;
  30.         }
  31.     }
  32.  
  33.     if (max_i < min_i) swap(max_i, min_i);
  34.  
  35.     for (i = min_i + 1; i < max_i; i++) product *= array[i];
  36.     return product;
  37. }
  38.  
  39. template<typename Type>
  40. void sort(Type array[], int N){
  41.     int i, k;
  42.     for (i = 0; i < N; i++)
  43.         for (k = i + 1; k < N; k++)
  44.             if (array[k] > array[i]) swap (array[i], array[k]);
  45.  
  46.     for(i = 0; i < N; i++){
  47.         cout << array[i];
  48.         if(i != N - 1) cout << ", ";
  49.     }
  50. }
  51.  
  52. template<typename Type>
  53. void process(Type array[], int N)
  54. {
  55.     for(int i = 0; i < N; i++)
  56.     {
  57.         cin >> array[i];
  58.     }
  59.  
  60.     cout << sum(array, N) << ' ' << multiple(array, N) << endl;
  61.     sort(array, N);
  62. }
  63.  
  64.  
  65. int main() {
  66.     int N;
  67.     cin >> N;
  68.  
  69.     int array[N];
  70.     double arrayDouble[N];
  71.     float arrayFloat[N];
  72.  
  73.     process(array, N);
  74.     process(arrayDouble, N);
  75.     process(arrayFloat, N);
  76. }
  77.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement