// E01: build a tiny polynomial design matrix A[i,j]=x_i^j. Standard C++17 only.
#include <cmath>
#include <iomanip>
#include <iostream>
#include <vector>
int main() {
    const std::vector<double> x{0.0, 1.0, 2.0};
    const std::vector<double> y{1.0, 2.0, 2.0};
    const int m = static_cast<int>(x.size()), n = 2; // linear fit
    std::vector<double> a(m*n);
    for (int i=0; i<m; ++i) {
        double power=1.0;
        for (int j=0; j<n; ++j) { a[i*n+j]=power; power*=x[i]; }
    }
    std::cout << "E01 | A (3 rows x 2 columns); row-major A[i*n+j]\n";
    for (int i=0; i<m; ++i) {
        for (int j=0; j<n; ++j)
            std::cout << std::setw(9) << a[i*n+j];
        std::cout << " | y=" << y[i] << '\n';
    }
    // c0=7/6, c1=1/2: evaluate the known teaching polynomial by Horner's rule.
    std::cout << std::setprecision(12) << "p(2)= " << (7.0/6.0 + 0.5*x[2]) << '\n';
}
