diff --git a/Homework2/src/compute_arithmetic.cc b/Homework2/src/compute_arithmetic.cc index 4ea8992..5c6cb4d 100644 --- a/Homework2/src/compute_arithmetic.cc +++ b/Homework2/src/compute_arithmetic.cc @@ -1,13 +1,13 @@ #include "compute_arithmetic.hh" double compute_arithmetic::compute(unsigned int N) { double S = 0.0; - for (unsigned int i = 0; i <= N ; ++i) + for (int i = 0; i <= N ; i++) { S += double(i); } return S; } \ No newline at end of file diff --git a/Homework2/src/compute_pi.cc b/Homework2/src/compute_pi.cc index ee2d597..29eb798 100644 --- a/Homework2/src/compute_pi.cc +++ b/Homework2/src/compute_pi.cc @@ -1,23 +1,23 @@ #include #include "compute_pi.hh" double compute_pi::compute(unsigned int N) { double S = 0.0; if (N != 0) { - for (int i = 1; i <= N ; i++) + for (int i = 1; i <= N; i++) { S += 1.0/pow(double(i),2); } } S=sqrt(6.0*S); return S; } double compute_pi::getAnalyticPrediction(){ return M_PI; } \ No newline at end of file diff --git a/Homework2/src/main.cc b/Homework2/src/main.cc index fe323df..aa1253d 100644 --- a/Homework2/src/main.cc +++ b/Homework2/src/main.cc @@ -1,125 +1,128 @@ #include #include #include #include #include "series.hh" #include "compute_arithmetic.hh" #include "compute_pi.hh" #include "dumper_series.hh" #include "print_series.hh" #include "write_series.hh" using namespace std; -void decide_series(Series *&new_series, string type) -{ - if (type == "arithmetic") - { - new_series = new compute_arithmetic(); - } - else if (type == "pi") - { - new_series = new compute_pi(); - } - else - { - cout<<"ERROR: Invalid series type. Please enter 'arithmetic' or 'pi'."<compute(10) << endl; //Arithmetic series up to 10. Expected value: 55 cout << SERIES2->compute(10) << endl; //Pi series up to 10. Expected value: 3.04936 delete SERIES1; //Free up memory delete SERIES2; //Free up memory */ int N_inputs = argc; std::stringstream inputs; for (int i = 1; i < argc; ++i){ inputs << argv[i] << " "; } std::string serie_type; std::string dump_type; std::string separator; unsigned long N; // max number of iteration entered as input unsigned long freq; //frequency for dumper series inputs >> serie_type; inputs >> dump_type; inputs >> separator; inputs >> N; inputs >> freq; printf("###### User Inputs ###### \n"); std::cout << "Serie type: " << serie_type << std::endl; std::cout << "Dump type: " << dump_type << std::endl; std::cout << "Separator type: " << separator << std::endl; std::cout << "Max iterations: " << N << std::endl; std::cout << "Dump frequency: " << freq << std::endl; printf("######################## \n"); Series *ptrSeries = nullptr; DumperSeries *ptrDumper = nullptr; // Choose series to be implemented decide_series(ptrSeries, serie_type); // Dumper and writing if (dump_type == "print") { PrintSeries my_dump(*ptrSeries, N, freq); my_dump.dump(std::cout); std::string file_name = "ostream_output.txt"; std::ofstream my_os_file; my_os_file.open(file_name, std::ios::out | std::ios::trunc); my_dump.dump(my_os_file); my_os_file.close(); } else if (dump_type == "write") { WriteSeries my_dump(*ptrSeries, N); if ((separator == "comma") || (separator == "pipe")){ my_dump.setSeparator(separator); } else{ std::cout << "Data are written to a txt fille with a tab separator." << std::endl; } my_dump.dump(std::cout); } else { std::cout << "Dump type does not exist" << std::endl; abort(); } delete ptrSeries; return 0; } + +void decide_series(Series *&new_series, string type) +{ + if (type == "arithmetic") + { + new_series = new compute_arithmetic(); + } + else if (type == "pi") + { + new_series = new compute_pi(); + } + else + { + cout<<"ERROR: Invalid series type. Please enter 'arithmetic' or 'pi'."<