1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
|
int read_from_file( double x[], double y[], int n_max );
double average( double x[], int n );
double sum_product( double x[], double x_average, double y[], double y_average, int n );
int main()
{
double x[50];
double y[50];
int n = read_from_file( x, y, 50 );
cout << "There are " << n << " samples.\n";
double x_average = average( x, n );
double y_average = average( y, n );
double m_numerator = sum_product( x, x_average, y, y_average, n );
double m_denominator = sum_product( x, x_average, x, x_average, n );
double m = m_numerator / m_denominator;
double b = y_average - m * x_average;
cout << "y = " << m << "x + " << b << "\n";
}
|