C++ PROBLEM WITH SOLUTION:
SOLUTION:
1 #include <iostream>
2 using namespace std;
3
4 double average_x;
5 double mean_y;
6 double m;
7 double b ;
8 int main()
9 {
10 int p;
11 double x, y, sum_x, sum_y, sum_xx, sum_xy;
12 sum_x=0;
13 sum_y=0;
14 sum_xx=0;
15 sum_xy=0;
16
17
18 cout<< "Enter the
number of paired values: ";
19
20 cin >>p;
21
22 cout <<p << " pairs are
required:\n ";
23
24 for (int a = 1; a <= p;
a++)
25 {
26 cout << "First
pair " <<a<<" ? ";
27
28 cin >> x
>>y;
29
30
31 sum_x = sum_x + x;
32 sum_y = sum_y + y;
33 sum_xx = sum_xx + x*x;
34 sum_xy=sum_xy + x*y;
35
36 }
37
38 average_x = sum_x/p;
39 mean_y = sum_y/p;
40 m = (sum_xy - mean_y*sum_x)/(sum_xx -average_x*sum_x);
41 b = mean_y
- average_x*m;
42
43 cout << "The least squares regression
line is:\n";
44
45 cout<<"Y =
"<< m <<" X + "<<"("<<
b<<")" <<endl;
46 }

1 Comments
Talent is not a subtitute for hardwork ....
ReplyDeleteWork harder.
Dear reader, if you think of a different or an alternative way to achieve this solution, kindly share you idea in the comment section. Thank you.