/// Geoff Hagopian computing cos(x)

#include "..\std_lib_facilities.h"
#include <iomanip>

double cos(double);
void show_table(vector<double>);

int main() {
    /**double x;
    cout << "\nEnter a double and we'll compute its cosine. ";
    while(cin >> x) {
        cout << "\nThe cos(" << x << ") = " << cos(x);
    }*/
    vector<double> costable;
    for(double i = 0; i < 6.28; i += 0.01) {
        costable.push_back(cos(i));
    }
    show_table(costable);
}

double cos(double t) {
    double term = 1;
    double accumulator = 1;
    double n = 0;
    while(abs(term)> 0.0001) {  ///alternating series error
        term *= -t*t;
        ++n;
        term /= n;
        ++n;
        term /= n;
        accumulator += term;
        //cout << "\nn = " << n << "  term = " << term << " cos = " << accumulator;
    }
    return accumulator; ///cosine value
}

void show_table(vector<double> vd) {
    /// top row header
    int SP = 8;
    cout << "     " << left << setw(SP) << 0 << setw(SP) << 1
         << setw(SP) << 2 << setw(SP) << 3 << setw(SP) << 4 << setw(SP)
         << 5 << setw(SP) << 6 << setw(SP) << 7 << setw(SP) << 8
         <<  9 << endl;
    for(int i = 0; i < 628; ++i) {
        cout << right << setw(SP) << setprecision(3) << fixed << vd[i];
        //if((i+1)%10==0) cout << endl;
    }
}
