include stdio include math int main int argc const char argv int nScan

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#include <stdio.h>
#include <math.h>
int main (int argc, const char * argv[]) {
int nScanned;
int n;
double sumX, sumY;
double sumX2, sumY2;
double sumXY;
n = 0;
sumX = sumY = sumX2 = sumY2 = sumXY = 0.0;
do {
double x, y;
nScanned = scanf("%lg %lg", &x, &y);
if (nScanned == 2) {
n++;
sumX += x;
sumX2 += x * x;
sumY += y;
sumY2 += y * y;
sumXY += x * y;
}
} while (nScanned == 2);
double slope, intercept;
slope = (n * sumXY - sumX * sumY)
/ (n * sumX2 - sumX * sumX);
intercept = (sumY - slope * sumX) / n;
double correlation;
correlation = slope * sqrt((n * sumX2 - sumX * sumX)
/ (n * sumY2 - sumY * sumY));
printf("%g\t%g\t%g\n", slope, intercept, correlation);
return 0;
}