Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- #include <utility>
- using namespace std;
- pair <double, double> Get_y(vector <double> & x, vector <double> & y){
- int N = x.size();
- double S_x, S_y, S_xy, S_x2;
- for(int i = 0; i < N; ++i){
- S_x += x[i];
- S_y += y[i];
- S_xy += x[i] * y[i];
- S_x2 += x[i] * x[i];
- }
- double a = (N * S_xy - S_x * S_y) / (N * S_x2 - S_x * S_x);
- double b = (S_x2 * S_y - S_x * S_xy) / (N * S_x2 - S_x * S_x);
- return make_pair(a, b);
- }
- int main(){
- cout << "Enter the number of points:\n";
- int N;
- cin >> N;
- vector <double> x(N);
- vector <double> y(N);
- cout << "Enter " << N << " pairs of coordinates (x, y):\n";
- for(int i = 0; i < N; ++i){
- cin >> x[i] >> y[i];
- }
- pair <double, double> line = Get_y(x, y);
- cout << "a = " << line.first << ", b = " << line.second;
- system("PAUSE");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement