loulett

HW1_deviations

Oct 27th, 2014
172
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.87 KB | None | 0 0
  1. #include <cstdio>
  2. #include <cstdlib>
  3. #include <cmath>
  4.  
  5. int min(const int *arr, int n)
  6. {
  7.     int min = 10000;
  8.     for (int i = 0; i < n; ++i)
  9.     {
  10.         if (*(arr + i) < min)
  11.         {
  12.             min = arr[i];
  13.         }
  14.     }
  15.     return min;
  16. }
  17.  
  18. double sm(const int *arr, int n)
  19. {
  20.     int sum = 0;
  21.     for (int i = 0; i < n; ++i)
  22.     {
  23.         sum += *(arr + i);
  24.     }
  25.     return sum / n;
  26. }
  27.  
  28. double sd(const int *arr, int n)
  29. {
  30.     double s = sm(arr, n);
  31.     double ans = 0;
  32.     for (int i = 0; i < n; ++i)
  33.     {
  34.         ans += pow(arr[i] - s, 2.0);
  35.     }
  36.     return sqrt(ans / (n - 1));
  37. }
  38.  
  39.  
  40.  
  41. int main()
  42. {
  43.     int n = 0;
  44.     scanf("%d", &n);
  45.  
  46.     int *arr = (int *) malloc(n * sizeof(int));
  47.    
  48.     if (!arr)
  49.         return 1;
  50.    
  51.     for (int i = 0; i < n; i++)
  52.     {
  53.         scanf("%d", arr + i);
  54.     }
  55.    
  56.     printf("min is %d\n", min(arr, n));
  57.     printf("simple mean is %f\n", sm(arr, n));
  58.     printf("standart devation is %f\n", sd(arr, n));
  59.  
  60.     return 0;
  61. }
Advertisement
Add Comment
Please, Sign In to add comment