I'm writing this program to calculate the best fit line. It's not finished. I'm doing it step to step to make sure it runs so I don't get lots of errors and totally freak out at the end. Right now it runs fine, but something is really off with the calculations I have so far. The loop for the input works great, but the averages are really wrong, and the for the sum of squares calculation it is only calculating the square of the last value. Where am I going wrong?
#include <iostream>
#include <cmath>
using namespace std;
double avg(double[], int);
double sumsq(double[], int);
int main ()
{
int n;
cout << "How many points do you want to plot? ";
cin >> n;
double x[n];
double y[n];
for (int i=1; i<=n; i++)
{
cout << "Please enter the value for x " << i << ": ";
cin >> x[n];
cout << "Please enter the value for y " << i << ": ";
cin >> y[n];
}
cout << avg(x,n) << endl;
cout << avg(y,n) << endl;
cout << sumsq(x,n) << endl;
cout << sumsq(y,n) << endl;
return 0;
}
double avg(double z[], int n)
{
double sum=0;
for (int i=1; i<=n; i++)
{
sum+=z[i];
}
return sum/n;
}
double sumsq(double z[], int n)
{
double sum=0;
double sumsq=0;
for (int i=1; i<=n; i++)
{
sumsq=sum+(z[i]*z[i]);
}
return sumsq;
}