// E09: independent toy function CSV writer.
#include <cmath>
#include <filesystem>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <string>
int main(int argc,char **argv) {
    if (argc!=2) { std::cerr << "Usage: " << argv[0] << " OUTPUT.csv\n"; return 2; }
    const std::filesystem::path output(argv[1]);
    if (output.extension()!=".csv" || output.empty() || std::filesystem::exists(output) ||
        !std::filesystem::exists(output.parent_path().empty()?".":output.parent_path())) {
        std::cerr << "Invalid location/extension or output already exists: " << output << '\n'; return 3;
    }
    std::ofstream file(output,std::ios::out);
    if (!file) { std::cerr << "Unable to write output\n"; return 4; }
    file << "x,y\n" << std::setprecision(17);
    for (int i=0;i<=8;++i) {
        const double x=i/4.0,y=1+x-x*x; // polynomial toy; unrelated to QHO
        if (!std::isfinite(y)) return 5;
        file << x << ',' << y << '\n';
    }
    file.close();
    if (!file) return 6;
    std::cout << "wrote_rows=9 file=" << output << "\nPASS E09\n";
}
