diff --git a/homework2/README.md b/homework2/README.md index 6e14ade6..3594889f 100644 --- a/homework2/README.md +++ b/homework2/README.md @@ -1,38 +1,51 @@ -# How to run program +## How to run the program ### Compile and run program with the following commands ``` mkdir build/ cd build cmake .. make ./src/main arg1 arg2 arg3 arg4 ``` * `arg1` can be either `pi` or `arithmetic` depending on what you want to calculate * `arg2` can be either `write` or `print` depending on if you want to write to file or print to screen * `arg3` can be either any integer representing precision of output * `arg4` can be either `comma` or `pipe` or `space` (anything other than `comma` or `pipe` will be interpreted as `space`) and represents the separator used in writing to file. When printing to screen only `space` is used as it does not make sense (in terms of readability) to use any other separator when printing to screen Plotting of convergence of the pi calculation can be acccomplished by running python script `plotting.py` in the `homework2/src/` directory as follows: `python3 plotting.py filename` where filename is the name of the output file (e.g., `out.csv` which is located in the `build/` directory) ### Example In `homework2/build/src/`: -`./main pi write 8 comma` +`./main pi write 8 comma` + +or, for example, + +`./main arithmetic write 10 pipe` In `homework2/src/`: -`python3 plotting.py out.csv` +`python3 plotting.py out.csv` + +or, for example, + +`python3 plotting.py out.psv` + + +### Notes +* Note that `WriteSeries` has been removed due to `PrintSeries` is now able to both print to screen and write to file. Thus no need for `WriteSeries` any more as all functionality is in `PrintSeries`. You can check out the git history if you're interested. +* Since `Series`, `ComputePi` and `ComputeArithmetic` are rather small classes (i.e., have simple member functions) we chose not to separate declarations and definitions into `.hh` and `.cc` file for readability. In other words, the definitions of the member functions are found in the respective `.hh` files. -### Answer to Exercise 2.1: What is the best way/strategy to divide the work among yourselves? +## Answer to Exercise 2.1: What is the best way/strategy to divide the work among yourselves? - Define the subsets of parts which can be (mostly) independent and divide them in a suitable way between each members. Work on the independent parts (1) of the same task/project (2). (1) : To minimize overlaps/collisions ( // parallel process) (2) : To be able to debug and adapt regularly the growing project and understand the problems of each others. ( // iterative process) - Brainstorm discuss and think (with+without the members) on how to solve the most complexe tasks/parts. - Assemble and adapt each piece of the project step by step. diff --git a/homework2/src/compute_arithmetic.hh b/homework2/src/compute_arithmetic.hh new file mode 100644 index 00000000..2997a6aa --- /dev/null +++ b/homework2/src/compute_arithmetic.hh @@ -0,0 +1,27 @@ +#ifndef COMPUTEARITHMETIC_HPP +#define COMPUTEARITHMETIC_HPP + +#include "series.hh" + +class ComputeArithmetic : public Series { + +public: + //PREVIOUS IMPLEMENTATION: + //double compute(unsigned int N) { + // double result = 0; + // for (int a = 1; a<=N; ++a) { + // result=result+a; + // } + // return result; + //} + + double computeValue(int k) { + return 1.*k; + } + + double getAnalyticPrediction() { + return 1.*current_index*(current_index+1.)/2.; + } +}; + +#endif diff --git a/homework2/src/compute_pi.hh b/homework2/src/compute_pi.hh new file mode 100644 index 00000000..2464414c --- /dev/null +++ b/homework2/src/compute_pi.hh @@ -0,0 +1,32 @@ +#ifndef COMPUTEPI_HPP +#define COMPUTEPI_HPP + +#include "series.hh" + +class ComputePi : public Series { +public: + //PREVIOUS IMPLEMENTATION: + //double compute(unsigned int N) { + // double result = 0.0; + // for (int a = 1; a<=N; ++a) { + // result=result+1.0/(a*a); + // } + // result = sqrt(6.0*result); + // return result; + //} + + double compute(unsigned int N) { + double current_value = Series::compute(N); + return sqrt(6.0*current_value); + } + + double computeValue(int k) { + return 1./(1.*k*k); + } + + double getAnalyticPrediction(){ + return M_PI; + } +}; + +#endif diff --git a/homework2/src/main.cc b/homework2/src/main.cc index 19ac1599..0af658c7 100644 --- a/homework2/src/main.cc +++ b/homework2/src/main.cc @@ -1,92 +1,94 @@ /* MAIN FILE */ #include "print_series.hh" +#include "compute_pi.hh" +#include "compute_arithmetic.hh" using namespace std; // TODO: Use stringstream for argv! int main(int argc, char *argv[]) { std::stringstream sstr; for (int i = 1; i < argc; ++i) { sstr << argv[i] << " "; } std::string in_type; std::string in_print_or_write; unsigned int in_precision; std::string in_separator; sstr >> in_type; sstr >> in_print_or_write; sstr >> in_precision; sstr >> in_separator; std::cout << "You chose the following options: " << std::endl; std::cout << "Type: " << in_type << std::endl; std::cout << "Output: " << in_print_or_write << std::endl; std::cout << "Precision: " << in_precision << std::endl; std::cout << "Separator: " << in_separator << std::endl; if (in_print_or_write == "print") std::cout << "Note: The separator is only used when writing to file!" << std::endl; // Declare Variables int N = 500; int maxiter = 500; int freq = 10; // Instantiate an obj of a subclass from Series and Point its address Series *ptrobj; if (in_type.compare("arithmetic")==0) { ComputeArithmetic obj; ptrobj = &obj; //auto ptrobj = std::make_unique(); } else if (in_type.compare("pi")==0){ ComputePi obj; ptrobj = &obj; //auto ptrobj = std::make_unique(); } else { cout << "Wrong Input: Run aborted!" << endl; return 0; } // Call the associated method double MyResult = ptrobj->compute(N); // Print the Result cout << "The result is: " << MyResult << endl; // Print to screen or Write to file PrintSeries print_obj = PrintSeries(*ptrobj, maxiter, freq); print_obj.setPrecision(in_precision); if (in_print_or_write.compare("print")==0){ print_obj.dump(); } else if (in_print_or_write.compare("write")==0){ std::string filename; std::string separator; if (in_separator.compare("comma")==0){ filename = "out.csv"; separator = ","; } else if (in_separator.compare("pipe")==0){ filename = "out.psv"; separator = "|"; } else{ // default is txt file std::cout << "Choosing default filename and separator" << std::endl; filename = "out.txt"; separator = " "; } print_obj.setSeparator(separator); std::ofstream outFile(filename); print_obj.dump(outFile); } else{ cout << "Wrong Input: Run aborted!" << endl; return 0; } return 0; } diff --git a/homework2/src/print_series.cc b/homework2/src/print_series.cc index 152c4d44..4d244003 100644 --- a/homework2/src/print_series.cc +++ b/homework2/src/print_series.cc @@ -1,36 +1,36 @@ #include "print_series.hh" void PrintSeries::setSeparator(std::string in_sep){ if (in_sep == ","){ sep = ","; } else if (in_sep == "|"){ sep = "|"; } else{ // default is txt file with space separated values sep = " "; } } void PrintSeries::setPrecision(unsigned int precision){ prec = precision; } void PrintSeries::dump(std::ostream & os){ double exact = series.getAnalyticPrediction(); if ( std::isnan(exact) ){ os << "Step" << sep << "Result \n"; - for (int i=1; i class Series { public: unsigned int current_index = 0; double current_value = 0.; virtual double compute(unsigned int N) { - if (current_index <= N) { - N = N - current_index; - } - else { current_value = 0.; - current_index = 0; - } - for (int k = 0;k