From 2dfff9908e48a9a57750957df70e22b6227c920f Mon Sep 17 00:00:00 2001 From: anh Date: Sun, 21 Jan 2024 12:14:43 -0500 Subject: [PATCH] added recursion tests to directory 62 --- 62_Cleanup/tests/input151.c | 19 +++++++++++++++++++ 62_Cleanup/tests/input152.c | 28 ++++++++++++++++++++++++++++ 62_Cleanup/tests/input153.c | 24 ++++++++++++++++++++++++ 62_Cleanup/tests/out.input151.c | 5 +++++ 62_Cleanup/tests/out.input152.c | 1 + 62_Cleanup/tests/out.input153.c | 1 + 6 files changed, 78 insertions(+) create mode 100644 62_Cleanup/tests/input151.c create mode 100644 62_Cleanup/tests/input152.c create mode 100644 62_Cleanup/tests/input153.c create mode 100644 62_Cleanup/tests/out.input151.c create mode 100644 62_Cleanup/tests/out.input152.c create mode 100644 62_Cleanup/tests/out.input153.c diff --git a/62_Cleanup/tests/input151.c b/62_Cleanup/tests/input151.c new file mode 100644 index 0000000..6e35a49 --- /dev/null +++ b/62_Cleanup/tests/input151.c @@ -0,0 +1,19 @@ +#include "stdio.h" + +// recursion test 1 + +// recursively computes n!, assuming n is nonnegative +int factorial(int n){ + if (n == 0 || n == 1) + return(1); + return(n * factorial(n - 1)); +} + +int main(void) { + printf("0! = %d\n", factorial(0)); + printf("1! = %d\n", factorial(1)); + printf("3! = %d\n", factorial(3)); + printf("4! = %d\n", factorial(4)); + printf("7! = %d\n", factorial(7)); + return(0); +} \ No newline at end of file diff --git a/62_Cleanup/tests/input152.c b/62_Cleanup/tests/input152.c new file mode 100644 index 0000000..9a69ccb --- /dev/null +++ b/62_Cleanup/tests/input152.c @@ -0,0 +1,28 @@ +#include "stdio.h" + +// recursion test 2 + +/* recursively computes the nth fibonacci number +assuming n is nonnegatve */ +int fib(int n){ + if (n == 0 || n == 1) + return(n); + return(fib(n - 1) + fib(n - 2)); +} + +// prints the first 11 numbers in the fibonacci sequence +int main(void){ + printf("%d %d %d %d %d %d %d %d %d %d %d", + fib(0), + fib(1), + fib(2), + fib(3), + fib(4), + fib(5), + fib(6), + fib(7), + fib(8), + fib(9), + fib(10)); + return(0); +} \ No newline at end of file diff --git a/62_Cleanup/tests/input153.c b/62_Cleanup/tests/input153.c new file mode 100644 index 0000000..53317b2 --- /dev/null +++ b/62_Cleanup/tests/input153.c @@ -0,0 +1,24 @@ +#include "stdio.h" + +// mutual recursion test + +// both of these assume the argument is nonnegative +int is_even(int n); +int is_odd(int n); + +int is_even(int n){ + if (n == 0) + return(1); + return(is_odd(n - 1)); +} + +int is_odd(int n){ + if (n == 0) + return(0); + return(is_even(n - 1)); +} + +int main(void){ + printf("%d %d %d %d", is_even(14), is_even(59), is_odd(2), is_odd(37)); + return(1); +} \ No newline at end of file diff --git a/62_Cleanup/tests/out.input151.c b/62_Cleanup/tests/out.input151.c new file mode 100644 index 0000000..bc109ff --- /dev/null +++ b/62_Cleanup/tests/out.input151.c @@ -0,0 +1,5 @@ +0! = 1 +1! = 1 +3! = 6 +4! = 24 +7! = 5040 diff --git a/62_Cleanup/tests/out.input152.c b/62_Cleanup/tests/out.input152.c new file mode 100644 index 0000000..d39ff16 --- /dev/null +++ b/62_Cleanup/tests/out.input152.c @@ -0,0 +1 @@ +0 1 1 2 3 5 8 13 21 34 55 \ No newline at end of file diff --git a/62_Cleanup/tests/out.input153.c b/62_Cleanup/tests/out.input153.c new file mode 100644 index 0000000..367dd0e --- /dev/null +++ b/62_Cleanup/tests/out.input153.c @@ -0,0 +1 @@ +1 0 0 1 \ No newline at end of file