Please help with error

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;

// prototype of functions linreg & integrate
double integrate ( double, double, double, double );
void linreg (ifstream&, double&, double&, double&, double&, double&);


int main()
{
// numeric variable declarations
double m, b, firstx, lastx, r;

// file variable declarations and initialization
ifstream fin;
ofstream fout;

// 1) file variable initializations
fin.open("mp5.inp");
fout.open("mp5.out");

// 2) invoke functon linreg to calculate least squares line
linreg (fin, m, b, r, firstx, lastx);

// 3) display linear equation, correlation coefficient to the file
fout << fixed << setprecision(2)
<< setw(10) << "y = " << setw(6) << m
<< setw(10) << " * x + " << setw(6) << b << "\n";
fout << setw(26) << "Correlation Coefficient = "
<< setw(6) << r << endl;


// 4) invoke integrate and display result to the file

fout << setw(26) << "Integral = "
<< setw(6) << integrate (m,b,firstx,lastx) << endl;


// 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))
Last edited on
linreg ( fin, double m, double b, double r, double firstx, double lastx )

The code is missing a return type for the function and a data type for the first variable.
Can someone look over the code and help me with the linreg function and the subsequent steps?
Topic archived. No new replies allowed.