Thread: Liner Regression bugs

  1. #1
    Registered User
    Join Date
    Oct 2007
    Posts
    16

    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. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > 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.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Oct 2007
    Posts
    16
    thanks... There are no bugs now... however.. nothing is being outputted and I cant quite seem to figure out why.

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by bar5037 View Post
    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
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    Registered User
    Join Date
    Oct 2007
    Posts
    16
    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. #6
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    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).
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Bugs in Program Question
    By racerday182 in forum C++ Programming
    Replies: 14
    Last Post: 12-05-2008, 10:30 AM
  2. Cubic Regression - Math/Numerical Question
    By Davros in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 04-27-2005, 03:02 PM
  3. Need help finding bugs
    By Shakti in forum Game Programming
    Replies: 16
    Last Post: 02-13-2005, 01:42 AM
  4. Need help fixing bugs in data parsing program
    By daluu in forum C Programming
    Replies: 8
    Last Post: 03-27-2003, 06:02 PM
  5. Linear Regression
    By Govtcheez in forum A Brief History of Cprogramming.com
    Replies: 3
    Last Post: 09-17-2001, 10:04 PM