AddO1f

Lab9_1

Mar 3rd, 2016
144
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.66 KB | None | 0 0
  1. #include "stdafx.h"
  2. #include <iostream>
  3. #include <math.h>
  4.  
  5. double sr(int n, int* a);
  6. double srr(int n, int* a);
  7.  
  8. using namespace std;
  9.  
  10. int _tmain(int argc, _TCHAR* argv[])
  11. {
  12.    
  13.     int n,i;
  14.     cout << "vvedite razmer: ";
  15.     cin >> n;
  16.     int* a = new int[n];
  17.     cout << "vvedite massiv: ";
  18.     for (i=0; i<n; i++)
  19.     cin >> a[i];
  20.     cout << "sredn (ne rekurs) = " << sr(n,a) << endl;
  21.     cout << "sredn (rekurs) = " << srr(n,a)/n << endl;
  22.     system("pause");
  23.     return 0;
  24. }
  25.  
  26. double sr(int n, int* a)
  27. {
  28.     int sum = 0;
  29.     for (int i=0; i<n; i++)
  30.     sum+=a[i];
  31.     return sum/n;
  32. }
  33.  
  34. double srr(int n, int* a)
  35. {
  36.     if (n==1) return a[0];
  37.     else return srr(n-1,a)+a[n-1];
  38. }
Advertisement
Add Comment
Please, Sign In to add comment