diff --git a/.gitignore b/.gitignore index 9986867..ac98f20 100644 --- a/.gitignore +++ b/.gitignore @@ -62,3 +62,5 @@ build CMakeCache.txt Makefile src/huffman/testfiles +data +cmake_install.cmake diff --git a/CMakeLists.txt b/CMakeLists.txt index e499e29..ecfdd44 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -21,6 +21,7 @@ add_executable( echochar src/echochar.c ) add_executable( euler src/euler.c ) add_executable( factorial src/factorial.c ) add_executable( fail src/fail.c ) +add_executable( fib src/fib.c ) add_executable( fread src/fread.c ) add_executable( hello-world src/hello-world.c ) add_executable( options src/options.c ) diff --git a/src/euler.c b/src/euler.c index 30272da..07f0a7b 100644 --- a/src/euler.c +++ b/src/euler.c @@ -20,7 +20,7 @@ double factorial(double i) { // someone said i need 2 for loops. Totally wrong? for(int j=1;j +#include +#include + +const int START_VALUES[2] = {0, 1}; +const int LIMIT = 90; + +int main(int argc, char** argv) { + if (argc != 2) { + printf("usage: %s MAX_NUMBER\n", argv[0]); + exit(1); + } + int max = atoi(argv[1]); + if (max > LIMIT) { + printf("C does not provide a datatype large enough to compute fibonacci numbers > %d. (%d)\n", LIMIT, max); + exit(1); + } + uintmax_t* nums = malloc(max * sizeof(uintmax_t)); + nums[0] = START_VALUES[0]; + nums[1] = START_VALUES[1]; + + for(int i = 2; i < max+1; i++) { + nums[i] = nums[i-2] + nums[i-1]; + } + + for(int i = 0; i < max+1; i++) { + printf("%02d. %ju\n", i, nums[i]); + } +} diff --git a/src/miniio.c b/src/miniio.c index 6f1aa5e..0dadd8d 100644 --- a/src/miniio.c +++ b/src/miniio.c @@ -2,9 +2,9 @@ #include int my_strlen(const char *s) { - int len = 0; - while (s[len] != 0) { - len++; + register int len = 0; // try to use a register instead of a stack variable + while (s[len]) { + len++; } return len; } diff --git a/src/readfile.c b/src/readfile.c index fb5f773..ad56c6f 100644 --- a/src/readfile.c +++ b/src/readfile.c @@ -1,9 +1,13 @@ #include #include -int main(void) +int main(int argc, char** argv) { - char filename[] = "file.txt"; + if (argc != 2) { + printf("usage: %s FILE", argv[0]); + exit(1); + } + char* filename = argv[1]; char buf[2]; FILE *fp;