Advertisement
cd62131

Statistics

Jul 23rd, 2014
390
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.34 KB | None | 0 0
  1. #include <stdio.h>
  2. static double sum(double *x, int n);
  3. static double square_sum(double *x, int n);
  4. static double multiply_sum(double *x, double *y, int n);
  5. static void input_n(int *n);
  6. static void input_x_y(double *x, double *y, int n);
  7. int main(void);
  8. static double sum(double *x, int n) {
  9.   int i;
  10.   double s = 0.;
  11.   for (i = 0; i < n; i++)
  12.     s += x[i];
  13.   return s;
  14. }
  15. static double square_sum(double *x, int n) {
  16.   int i;
  17.   double s = 0.;
  18.   for (i = 0; i < n; i++)
  19.     s += x[i] * x[i];
  20.   return s;
  21. }
  22. static double multiply_sum(double *x, double *y, int n) {
  23.   int i;
  24.   double s = 0.;
  25.   for (i = 0; i < n; i++)
  26.     s += x[i] * y[i];
  27.   return s;
  28. }
  29. static void input_n(int *n) {
  30.   printf("n = ");
  31.   scanf("%d", n);
  32. }
  33. static void input_x_y(double *x, double *y, int n) {
  34.   int i;
  35.   printf("input x y:\n");
  36.   for (i = 0; i < n; i++) {
  37.     printf("%d ? ", i);
  38.     scanf("%lf%lf", &x[i], &y[i]);
  39.   }
  40. }
  41. int main(void) {
  42.   int n;
  43.   double x[BUFSIZ], y[BUFSIZ], sumx, sumy, sumxx, sumxy, aa, a0, a1;
  44.   input_n(&n);
  45.   input_x_y(x, y, n);
  46.   sumx = sum(x, n);
  47.   sumy = sum(y, n);
  48.   sumxx = square_sum(x, n);
  49.   sumxy = multiply_sum(x, y, n);
  50.   aa = n * sumxx - sumx * sumx;
  51.   a0 = (sumxx * sumy - sumx * sumxy) / aa;
  52.   a1 = (n * sumxy - sumx * sumy) / aa;
  53.   printf("a0 = %g\n", a0);
  54.   printf("a1 = %g\n", a1);
  55.   return 0;
  56. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement