• + 0 comments

    C++

    #include <cmath>
    #include <cstdio>
    #include <vector>
    #include <iostream>
    #include <algorithm>
    #include <iomanip>
    using namespace std;
    
    double dot_product(vector<double>x, vector<double> y)
    {
        double sum=0;
        for(int i=0; i<x.size(); ++i)
        {
            sum+=x[i]*y[i];
        }
        return sum;
    }
    
    double sum(vector<double> x, int p=1)
    {   double sum=0;
        for(int i=0; i<x.size(); i++)
            sum+=pow(x[i], p);
        return sum;
    }
    double mean(vector<double> x)
    {   double total=sum(x);
        return total/x.size();
    }
    
    double linear_regression(vector<double> x, vector<double>y, double t)
    {   double n=double(x.size());
        double sumX=sum(x,1);
        double sumX2=sum(x,2);
        double sumY=sum(y,1);
        double meanX=mean(x);
        double meanY=mean(y);
        
        double b=(n*dot_product(x, y)-sumX*sumY)/(n*sumX2-sumX*sumX);
        double a=meanY-b*meanX;
        
        return a+b*t;
        
    }
    
    int main() {
        /* Enter your code here. Read input from STDIN. Print output to STDOUT */   
        int a=5;
        vector<double> math(a);
        vector<double> stat(a);
        double m,s;    
        for(int i=0; i<a; ++i)
        {
            cin>>m>>s;
            math.push_back(m);
            stat.push_back(s);
        }
        double math_grade=80;
        double stat_grade=linear_regression(math,stat,math_grade);
        
        cout<<fixed<<setprecision(3);
        cout<<stat_grade<<endl;
        return 0;
    }
    
    }