Advertisement
cd62131

SD and Min

Feb 20th, 2014
327
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.91 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <math.h>
  4. #define N 10
  5. static double *input(void){
  6.   char buf[BUFSIZ];
  7.   double *dat = (double *) malloc(N * sizeof(double));
  8.   int i = 0;
  9.   double d;
  10.   while (i < N) {
  11.     printf("Input %d: ", i + 1);
  12.     fgets(buf, BUFSIZ, stdin);
  13.     d = atof(buf);
  14.     if (d <= 0) continue;
  15.     dat[i++] = d;
  16.   }
  17.   return dat;
  18. }
  19. static double com1(double *dat) {
  20.   double sum = 0., average, diff = 0.;
  21.   int i;
  22.   for (i = 0; i < N; i++) sum += dat[i];
  23.   average = sum / N;
  24.   for (i = 0; i < N; i++) diff += (dat[i] - average) * (dat[i] - average);
  25.   return sqrt(diff / N);
  26. }
  27. static double com2(double *dat) {
  28.   double min = dat[0];
  29.   int i;
  30.   for (i = 1; i < N; i++) if (min > dat[i]) min = dat[i];
  31.   return min;
  32. }
  33. int main(void) {
  34.   double *dat = input();
  35.   printf("Standard Diviation: %lf\n", com1(dat));
  36.   printf("Minimum: %lf\n", com2(dat));
  37.   return 0;
  38. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement