Can someone please help with this program? The more i work on it, the more confused I'm becoming. Can someone guide me in the right direction?
* Statement: Find the least squares line for the data in mp5.inp and
* integrate it from the first x value to the last x value.
* Specifications:
* Input - a sequence of (x, y) values from a sequential file
* Invokes- linreg to determine linear regression line and correlation coeff
* - integrate to determine the area under the curve
* Output - to a sequential file
* - the least squares line y = m*x + b
* - the correlation coefficient
* - the integral of m*x+b over the first x to the last
************************************************************************/
#include <fstream>
#include <iomanip>
#include <cmath>
using namespace std;
// 5) disconnect from files
fin.close();
fout.close();
}
/*Function integrate
*
*receives - Slope m of least squares line
* - y intercept of least squares line
* - limits of integration
*returns - the antiderivative m*x^2/2 + b*x evaluated at x
*************************************************************************/
// Define function integrate below
double integrate (double m, double b, double firstx, double lastx)
// 1) integrate a linear function
{
return ((m / 2) * (lastx * lastx) + b * lastx) -((m / 2) * (firstx * firstx) + b * firstx);
}
/*Function linreg
*
*receives - input file object fin
*returns - Slope m of least squares line
* - y Intercept m of least squares line
* - Correlation coefficient r of least squares line
*************************************************************************/
// Define function linreg below
void linreg (double fin, double m, double b, double r, double firstx, double lastx )
{
double sum, sum x, sum y, sum x*x, sum x*y, sum y*y;
double x, y;
// 1) reduction variable initialization
sum x = 0;
sum y = 0;
sum x*x = 0;
sum x*y = 0;
sum y*y = 0;
int n = 0;
// 2) loop forever
while ( fin.eof())
{
// 3) attempt to input an ordered pair
fin >> x >> y;
// 4) test for end of file
if(!fin.eof( ))
{
// 5) leave when true
}
else
{
break;
}
// 6) test for first iteration
if ( n == 0 )
{
// 7) save lower limit of integration
x = firstx;
// 8) save upper limit of integration
}
else
{
x = lastx;
}
// 9) update reduction variables
for (int i = 0; i < n; i++)
{
sum x += x;
sum y += y;
sum x*x += x*x;
sum x*y += x*y;
sum y*y += y*y;
int n += 1;
}
}
}
// 10) calculate slope, y intercept and correlation coefficient
m = ( n *( sum x*y ) - (sum x) * (sum y)) / (n * sum x*x - sum x * sum x)
b =( sum y - m * sum x )/ n
p = (n * sum x * y - sum x *sum y) / (sqrt ( n* sum x*x - pow(sum x, 2))* (sqrt (n * sum y *y - pow(sum y, 2))