diff --git a/Makefile b/Makefile index 65f48bc..e450f6f 100644 --- a/Makefile +++ b/Makefile @@ -23,6 +23,11 @@ sheet03: chmod +x build/sheet03 build/sheet03 +sheet04: + ${BUILD_CMD} src/sheet04.cpp -o build/sheet04 + chmod +x build/sheet04 + build/sheet04 + pizza: ${BUILD_CMD} src/pizza.cpp -o build/pizza chmod +x build/pizza diff --git a/src/sheet03.cpp b/src/sheet03.cpp index 7402b1a..818e2c5 100644 --- a/src/sheet03.cpp +++ b/src/sheet03.cpp @@ -24,6 +24,8 @@ int main(int argc, char **argv) pol2cart(0.5, 0, x, y); std::cout << x << " " << y << '\n'; pascal(argc, argv); + std::cout << '\n'; + pascal_mem(argc, argv); return 0; } @@ -77,39 +79,45 @@ void pascal(int argc, char **argv) std::cout<< '\n'; } } -unsigned fibonacci_nonrec(unsigned n) -{ - unsigned fibo = 0; - unsigned prev_fibo = 1; - unsigned prev_prev_fibo = 0; - for(int i=2; i<=n; ++i) - { - fibo = prev_fibo + prev_prev_fibo; - prev_prev_fibo = prev_fibo; - prev_fibo = fibo; - } - - return fibo; -} void pascal_mem(int argc, char **argv) { - - if (argc != 2 || std::stol(argv[1]) == 0) { return; } + if (argc != 2 ) { return; } unsigned depth = std::stol(argv[1]); - depth = (depth*depth-depth)/2; - std::vector pascal(depth,0); - pascal[0] = 1; - for(unsigned long long i=1; i pascal(vector_depth,0); + pascal[0] = 1; + std::cout << "1\n"; + // pascal[1] = 1; + // pascal[2] = 1; + // std::cout << "1\t1\n"; + unsigned rel_index = 2; + while(rel_index<=depth) + { + unsigned index = (rel_index*rel_index-rel_index)/2; + unsigned prev_index = ((rel_index-1)*(rel_index-1)-(rel_index-1))/2; + unsigned next_index = ((rel_index+1)*(rel_index+1)-(rel_index+1))/2; + pascal[index] = 1; + std::cout << pascal[index] << '\t'; + for(unsigned i=prev_index; i +#include +#include +class vec +{ + private: + unsigned size; + double *vector; + public: + vec(size_t size) : size(size), vector(new double[size]) {} + vec(size_t size, double ival) : size(size), vector(new double[size]) + { + for(size_t i=0; i ilist) : size(ilist.size()), vector(new double[size]) { + size_t i = 0; + for(std::initializer_list::iterator itr = ilist.begin(); itr!=ilist.end();++itr) + { + vector[i++] = *itr; + } + } + + ~vec() {delete[] vector; } + vec(const vec &m) : vector(new double[m.size]) + { + for(size_t i=0; i