Linear Least Square method implementation in C++

Hi all am actually new to C++ and programming in general and I need ur help. Am trying to find the slope(m) and intercept(b) of the linear equation of the form y = mx + b but i dont know why the code is not working. Kindly help me take a look at :
#include <cstdlib>
#include <iostream>
#include <math.h>
#include <stdio.h>
#include <iomanip>
using namespace std;

double sumY1(double y[], int n)
{

double temp=0;
for (int i=1; i<=n; i++)
{
temp= i*y[i-1]+ temp;
}
return temp;
}
double sumY2(double y[], int n)
{
double temp = 0;
for (int i=1; i<=n; i++)
{
temp = y[i-1] + temp;
}
return temp;
}

double getM(double z, double p, int n)
{
double M =(1/(n*n*n-n))*(12*z - 6*(n+1)*p);
return (M);
}
double getB(double z, double p, int n)
{
double B= (1/(n*n-n))*(((4*n)+2)*p- 6*z);
return (B);
}

int main(int arg, char *argv[])

{
double z=0;
double p=0;
long double m;
long double b;
double x[100], y[100];
int n;
std::cout <<"what is n where n is the number of x\n";
std::cin >> n;
std::cout << "supply the values of x\n";
for ( int i = 0; i < n; i++)
{
std::cin >>x[i];
}
std::cout << "supply the values of y\n";
for (int i = 0; i < n; i++)
{
std::cin >>y[i];
}

for (int i=0; i < n; i++)
{
cout<<"x"<<i<<" = "<<x[i]<<"\n";
}

for (int i= 0 ; i < n; i++)
{
cout<<"y"<<i<<" = "<<y[i]<<"\n";
}

z = sumY1(y,n);

p = sumY2(y,n);

m =getM(z,p,n);

b = getB(z,p,n);

cout<<fixed<< setprecision(4)<<z<<" "<<p<<" "<<m<<" "<<b<<endl;

system ("pause");

}
Topic archived. No new replies allowed.