Problem holding values for X and Y - Linear Regression Problem

I'm working on a program to calculate linear regression of N inputs. I need to hold values for X and Y when I prompt the user to input values for X and Y. The problem is, after some debugging, I discovered that towards the end of my code X and Y are both 0. I don't understand where I'm going wrong and was hoping someone could point the problem out to me.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
  #include <iostream>
#include <string>
#include <cmath>

using namespace std;

int n;
int m;
int i;
int j;
int x;
int x2;
int y;
int beta1;
float sumX;
float sumY;
float sumXY;
float prodXY;
float avgX;
float avgY;

class LinearRegression {
	public: 
		float calc_sumX();
		float calc_sumY();
		float calc_avgX();
		float calc_avgY();
		float calc_prodXY();
		float calc_beta1();
};

float LinearRegression::calc_sumX() {
	// get sample size
	cout << "Enter amount of X items: ";
    	cin >> n;

        // get data
    	cout << "Enter the Data: ";
	//float x[n];

        // calculate sum
   	for (i = 1; i <= n; ++i){
        	cin >> x[i];
		sumX = sumX + x[i];
    	}
}

float LinearRegression::calc_sumY() {
	// get sample size
	cout << "Enter amount of Y items: ";
    	cin >> m;

        // get data
    	cout << "Enter the Data: ";
	float y[m];

        // calculate sum
   	for (i = 1; i <= m; ++i){
        	cin >> y[i];
		sumY = sumY + y[i];
    	}
}

// calculate the X average
float LinearRegression::calc_avgX() {
	avgX = sumX / n;
}

// calculate the Y average
float LinearRegression::calc_avgY() {
	avgY = sumY / m;
}

// calculate sumX * sumY
float LinearRegression::calc_prodXY() {
	prodXY = sumX * sumY;
}

float LinearRegression::calc_beta1() {
	for (i = 0; i < 10; i++) {
		sumXY = (sumXY + (x * y));
	}
	/* THIS IS NOT WORKING YET
	for (j = 0; j < 10; j++) {
		x2 = x2 + (x * x);
	}

	beta1 = (sumXY - (10 * (avgX * avgY)) / (x2 - 10 * (avgX * avgX)));
	*/
}

int main() {
	// Create object test1
	LinearRegression test1;

	// Run tests
	test1.calc_sumX();
	test1.calc_sumY();
	test1.calc_avgX();
	test1.calc_avgY();
	test1.calc_prodXY();
	test1.calc_beta1();

	cout << "Value of X" << x << endl;
	cout << "Value of Y" << y << endl;
}
Yes, because the variable in line 39 and 55 , which is a local variables of member functions of the class , stores all the user input.

i believe that you accidentally commented out line 39

and then you tried to print out the value of X and Y in line 104 and 105 .



where in the program did you try to modify the X and Y ?
Last edited on
Starting from the top:

Globals are bad enough, but globals named n, m, i, etc? That's a shooting offence. Why do you even have a class?

line 44: you don't have an array named x

None of your globals are initialised, so when you do stuff like
sumY = sumY + y[i];

You end up with nonsense values. Ask yourself, what is the value in sumY to start with? You never gave it a value (0 would have been nice), so it contains nonsense. You can't possibly get any meaningful values from calculations based on these.

for (i = 1; i <= m; ++i){

Imagine m == 4, you would have an array like y[0], y[1], y[2], y[3] - now, by starting with i = 1, you've skipped the first element. By proceeding until i=m, you've accessed outside the bounds of your array. This is undefined behaviour, and the best you can hope for is wrong data, but you're likely to experience a segmentation fault
Topic archived. No new replies allowed.