\CPP
Linear Regression.cpp
```/*
Josh Santomieri
Santomieri Systems

Line of best fit - Linear Regression
Calculates the line of best fit given a group of (x,y) points.

Function Prototype:
void CreateLineOfBestFit(int *x, int *y, int points, TCHAR *eq, rsize_t maxLen);

Usage:
int x[] = {1, 2, 3, 4, 5};
int y[] = {11100, 10910, 11450, 11520, 11420};
TCHAR eq[100];

CreateLineOfBestFit(x, y, sizeof(x)/sizeof(int), (TCHAR*)&eq, sizeof(eq)/sizeof(TCHAR));
*/

void CreateLineOfBestFit(int *x, int *y, int points, TCHAR *eq, rsize_t maxLen)
{
int ix1;
double xAvg = 0, yAvg = 0;
double b1 = 0, b2 = 0;

if(points < 2)
{
wcscpy_s(eq, maxLen, L"2 or more points needed.");
return;
}

for(ix1 = 0; ix1 < points; ix1++)
{
xAvg += *(x+ix1);
yAvg += *(y+ix1);
}
xAvg /= points;
yAvg /= points;

for(ix1 = 0; ix1 < points; ix1++)
{
b1 += (*(x+ix1) - xAvg) * (*(y+ix1) - yAvg);
b2 += pow((*(x+ix1) - xAvg), 2);
}

if(b2 > 0)
{
b1 = b1/b2;
b2 = yAvg - (b1 * xAvg);
}
else
{
b1 = 0;
b2 = 0;
}

swprintf_s(eq, maxLen, L"y = %.3f * x + %.3f", b1, b2);
return;
}```