Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- static double sum(double *x, int n);
- static double square_sum(double *x, int n);
- static double multiply_sum(double *x, double *y, int n);
- static void input_n(int *n);
- static void input_x_y(double *x, double *y, int n);
- int main(void);
- static double sum(double *x, int n) {
- int i;
- double s = 0.;
- for (i = 0; i < n; i++)
- s += x[i];
- return s;
- }
- static double square_sum(double *x, int n) {
- int i;
- double s = 0.;
- for (i = 0; i < n; i++)
- s += x[i] * x[i];
- return s;
- }
- static double multiply_sum(double *x, double *y, int n) {
- int i;
- double s = 0.;
- for (i = 0; i < n; i++)
- s += x[i] * y[i];
- return s;
- }
- static void input_n(int *n) {
- printf("n = ");
- scanf("%d", n);
- }
- static void input_x_y(double *x, double *y, int n) {
- int i;
- printf("input x y:\n");
- for (i = 0; i < n; i++) {
- printf("%d ? ", i);
- scanf("%lf%lf", &x[i], &y[i]);
- }
- }
- int main(void) {
- int n;
- double x[BUFSIZ], y[BUFSIZ], sumx, sumy, sumxx, sumxy, aa, a0, a1;
- input_n(&n);
- input_x_y(x, y, n);
- sumx = sum(x, n);
- sumy = sum(y, n);
- sumxx = square_sum(x, n);
- sumxy = multiply_sum(x, y, n);
- aa = n * sumxx - sumx * sumx;
- a0 = (sumxx * sumy - sumx * sumxy) / aa;
- a1 = (n * sumxy - sumx * sumy) / aa;
- printf("a0 = %g\n", a0);
- printf("a1 = %g\n", a1);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement