-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
77 lines (64 loc) · 1.87 KB
/
main.cpp
File metadata and controls
77 lines (64 loc) · 1.87 KB
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
// this is the library to print out and take in (input and output ) of user
#include <iostream>
// to store a dynamic array
#include <vector>
// this is used to remove this std::cout and use it like this cout
using namespace std;
class CalculateBeta {
private:
int N;
vector<double> X,Y;
//X=[], Y=[]
public:
// constructor
CalculateBeta(int n) {
N = n;
}
// method to add one pair of data
// addData(1,2) ==> X=[1],Y=[2]
// addData(4,6) ==> X=[1,4],Y=[2,6]
// addData(10,20) ==> X=[1,4,10],Y=[2,6,20]
void addData(double x, double y) {
X.push_back(x);
Y.push_back(y);
};
double computeBeta() {
double sumX = 0, sumY = 0, sumXY = 0, sumX2= 0;
// sumX --> (EXi)
// sumY --> (EYi)
// sumXY --> (EXiYi)
// sumX2 --> (EXi 2)
for (int i = 0; i < N; i++) {
sumX += X[i];
sumY += Y[i];
sumXY += X[i] * Y[i];
sumX2 += X[i] * X[i];
// x= [50,78,80,67] y=[7,98,200,65]
}
double numerator = sumXY - (sumX * sumY) /N;
double denominator = sumX2 - (sumX * sumX) /N;
return numerator / denominator;
}
};
int main() {
int n;
cout << "Please number of pairs (n) you would want to take in: ";
cin >> n;
CalculateBeta calculate_beta(n);
// Car ferrari()
// ferrari.speed()
// class is like creating a blueprint for an system, something a car can be a class.
// we can create a class that is called Car can create method
cout <<"Enter "<< n << " pairs (Xi Yi):"<< endl;
for (int i = 0; i < n; i++) {
double x, y;
cout << "X" << (i+1) << ": ";
cin >> x;
cout << "Y" << (i+1) << ": ";
cin >> y;
calculate_beta.addData(x, y);
}
// x= [50,78,80,67] y=[7,98,200,65]
cout << "Beta = " << calculate_beta.computeBeta() << endl;
return 0;
};