# Thread: Liner Regression bugs

1. ## Liner Regression bugs

Hey, I'm working with a buddy on making this Linear and quadratic regression program and we hit a bug that we both can't seem to figure out. I'm looking for any help I can get, it is greatly appreciated.
Code:
```// Gary Marosy
// CMPSC 201C- 001
// HW5
// Inputs: Points Red from File
// Outputs: Equation of the fitted line

#include <iostream>
#include <cmath>
#include <iomanip>
#include <fstream>

using namespace std;

const int SIZE = 20;

void getData (double x[], double y[], int& size)
{
ifstream input;

input.open("linear.txt");
size = 0;

while (!input.eof())
{
input >> x[size] >> y[size];
size++;
}

input.close();
}

void displayResults ( double x[], double y[], double z[], int size)
{
int i = 0;
while (i < SIZE)
{
cout << "Measured X" << fixed << 5 << "Measured Y" << fixed << 5 << right << endl;
cout << x[i] << left << y[i] << right << endl;
i++;

}
}

void linearRegress ( double x[], double y[], int size, double &m, double &b )
{
int i = 0;
while (i < size)
{
int m;
int b;
int sumX = 0;
int sumY = 0;
int sumXY = 0;
int sumX2 = 0;
int denom;

for ( i; i < size; i++)
{

sumX += x[i];
sumY += y[i];
sumXY += x[i] + y[i];
sumX2 += x[i] * x[i];

denom = (size * sumX2) - (sumX * sumX);

m = ((size * sumXY) - (sumX * sumY)) / denom;

b = ((sumX2 * sumY) - (sumXY * sumX)) / denom;

}
}
}

void quadRegress ( double x[], double y[], int size, double &m, double &b, double &c );

int main ()
{
double x[SIZE];
double y[SIZE];
double z[SIZE];
int numlines = 0;
double m = 0;
double b = 0;
double size = 0;

getData(x, y, size);

linearRegress (x, y, size, m, b);

displayResults (x, y, z, size);

return 0;
}```
Ah... almost forgot to list the error.... The error reads as..
1>c:\homeworkfiles.cpp\homework5\homework5.cpp(88 ) : error C2664: 'getData' : cannot convert parameter 3 from 'double' to 'int &'
I've never seen an error like this before.
Thanks again to anyone who can help.

2. > double size = 0;
Why would you want a double to store the number of elements in an array?

It's not like you can ever have 5.678 elements is it?

> while (!input.eof())
Read the FAQ on why using eof() to control a loop is bad.
Essentially, this loops ONE more time than you expect.

3. thanks... There are no bugs now... however.. nothing is being outputted and I cant quite seem to figure out why.

4. Originally Posted by bar5037
thanks... There are no bugs now... however.. nothing is being outputted and I cant quite seem to figure out why.
And that's not a bug? What is a bug, if "no output isn't"? To me, anything that doesn't conform to what you expect is a bug [unless of course you expect the impossible].

Aside from that, I can't see any good reason why you shouldn't get output. But perhaps your code gets stuck somewhere.
Code:
```void linearRegress ( double x[], double y[], int size, double &m, double &b )
{
int i = 0;
while (i < size)
{
int m;
int b;
int sumX = 0;
int sumY = 0;
int sumXY = 0;
int sumX2 = 0;
int denom;

for ( i; i < size; i++)
{

sumX += x[i];
sumY += y[i];
sumXY += x[i] + y[i];
sumX2 += x[i] * x[i];

denom = (size * sumX2) - (sumX * sumX);

m = ((size * sumXY) - (sumX * sumY)) / denom;

b = ((sumX2 * sumY) - (sumXY * sumX)) / denom;

}
}
}```
The while-loop is completely useless here. The first time, it will be less than size, then it will enter the for-loop and after the for-loop, i is equal to size, to the while-loop exits. No meaning to that at all - the exact same thing would happen if you remove the while-loop part.

The local variables m and b are hiding the parameters into the function with the same name - so you will not see "any result" from the function.

I also believe that you only want to calculate denom, m and b when the for-loop is done.

But none of this explains why you don't get any output. I take it it's not a case of "the output is printed and then the screen disappears", and you just need a "getchar()" at the end of the main function?

--
Mats

5. It might be getting stuck elsewhere, because i fooled around with taking the while loop and and moving denom , m and b out. However still no dice.... any other sugestions... BTW huge thanks to matsp....

6. Code:
```void getData (double x[], double y[], int& size)
{
ifstream input;

input.open("linear.txt");
size = 0;

while (!input.eof())
{
input >> x[size] >> y[size];
size++;
}

input.close();
}```
That is the wrong way to read your data from the file. EOF is only set after an attempt to read data has failed. After reading the last line of data in the file, EOF is still false so your loop would execute one more time trying to get data. In most cases, this would result in the size parameter reporting there being one more x and y value than there really are and that last row of data would screw up your calculations. You also make no checks that the number of elements is less than or equal to the size of your arrays. This would be better:
Code:
```void getData (double x[], double y[], int& size)
{
ifstream input;

input.open("linear.txt");
size = 0;

while( size < SIZE && input >> x[size] >> y[size] )
{
size++;
}

input.close();
}```
The size < SIZE part makes sure we only store what the x and y arrays are capable of storing and the other modification makes sure we only store as much as what's in the file (up to SIZE of course).

Popular pages Recent additions