Advertisement
waelnassaf

PR1 HW

Jul 13th, 2019
209
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.81 KB | None | 0 0
  1. #include <iostream>
  2. using namespace std;
  3. template<class T>
  4. // Correction of fun1
  5. double fun1(T* arr, int size) {
  6.     double sum = 0;
  7.     for (int i = 0; i < size; ++i) {
  8.         cin >> arr[i];
  9.         sum += arr[i];
  10.     }
  11.     return sum/size;
  12. }
  13.  
  14.  
  15. template<class T>
  16. void fun2(T* arr, int size, T* max, int* maxIndex) {
  17.     *max = arr[0];
  18.     for (int i = 0; i < size; ++i) {
  19.         if (arr[i] > *max) {
  20.             *max = arr[i];
  21.             *maxIndex = i;
  22.         }
  23.     }
  24. }
  25.  
  26.  
  27. int main()
  28. {
  29.  
  30.     int n = 5;
  31.     double a[n];
  32.     double avg = fun1(a, n);
  33.     cout << "Average is " << avg << endl;
  34.  
  35.     double max = 0;
  36.     int maxIndex = 0;
  37.     fun2(a, n, &max, &maxIndex);
  38.  
  39.     cout << "The maximum is " << max;
  40.     cout << " with a maximum index of " << maxIndex << endl;
  41.  
  42.     return 0;
  43. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement