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?
I originally had it at i=0 but the answers were wrong and I thought since in main my loop starts at 1 maybe I should change it. I still doesn't make a difference.
Oh, I just noticed, you're technically not allowed to have variable length arrays like double x[n];
If you want an array with variable length, use std::vector<double>.
(you could also do something like double* x = newdouble[n], but why bother when we have std::vector<double>)
Also:
18 19 20 21 22 23 24
for (int i=1; i<=n; i++) // Should be for (int i = 0; i < n; i++)
{
cout << "Please enter the value for x " << i << ": ";
cin >> x[n]; // Should be cin >> x[i];
cout << "Please enter the value for y " << i << ": ";
cin >> y[n]; // Should be cin >> y[i];
}
Also, your "sumsq" function doesn't make any sense at all.
You're not summing anything in it....
Everything else works now except for the square sums formula. If my variable for the loop is z, I basically want it to say to square each z[i] and add them together. But I can't figure out how to code that.
You code it pretty much just like your "avg" function, except instead of adding z[i], you add z[i]*z[i].
(and you don't divide by n at the end, of course)