From 442a49ad5a48d417345959b903ae6a6d32d55759 Mon Sep 17 00:00:00 2001 From: Haidong Ji Date: Fri, 15 Apr 2022 15:51:30 -0500 Subject: Great C programming fun Excellent fundamentals and displine training, many tools and techniques exercises: gdb, emacs, valgrind, git --- 23_power_rec/.gitignore | 2 ++ 23_power_rec/README | 19 +++++++++++++++++++ 23_power_rec/grade.txt | 45 +++++++++++++++++++++++++++++++++++++++++++++ 23_power_rec/power.c | 12 ++++++++++++ 23_power_rec/power.o | Bin 0 -> 1512 bytes 23_power_rec/test-power | Bin 0 -> 49296 bytes 23_power_rec/test-power.c | 1 + 7 files changed, 79 insertions(+) create mode 100644 23_power_rec/.gitignore create mode 100644 23_power_rec/README create mode 100644 23_power_rec/grade.txt create mode 100644 23_power_rec/power.c create mode 100644 23_power_rec/power.o create mode 100755 23_power_rec/test-power create mode 120000 23_power_rec/test-power.c (limited to '23_power_rec') diff --git a/23_power_rec/.gitignore b/23_power_rec/.gitignore new file mode 100644 index 0000000..d343b3c --- /dev/null +++ b/23_power_rec/.gitignore @@ -0,0 +1,2 @@ +power +Makefile diff --git a/23_power_rec/README b/23_power_rec/README new file mode 100644 index 0000000..3eb89f0 --- /dev/null +++ b/23_power_rec/README @@ -0,0 +1,19 @@ +Write a recursive function unsigned power(unsigned x, unsigned y) which computes x +to the y power. Note the function signature (unsigned ints). +Note that while 0 to the 0 is undefined in mathematics, +we specify that 0 to the 0 shall be 1 for this function. + +Save it into a file called power.c + +You MUST use recursion (no iteration). + + +Use your test-power.c from your previous assignment to test your implementation +of power. You can symlink the file from the previous assignment into this directory: + +ln -s ../22_tests_power/test-power.c ./ + +You should then write a Makefile which will compile each C file separately +to an object file, and like them together. + + diff --git a/23_power_rec/grade.txt b/23_power_rec/grade.txt new file mode 100644 index 0000000..6ee2635 --- /dev/null +++ b/23_power_rec/grade.txt @@ -0,0 +1,45 @@ +Grading at Sat 30 Oct 2021 02:41:44 AM UTC +Attempting to compile power.c +Attempting to compile power.o with our main +Checking for unsigned power (unsigned x, unsigned y) +Found on line 4, column 1 +Checking for no iteration (do, while, for) +Checking that power is recursive +0^0 was Correct +0^1 was Correct +0^2 was Correct +0^6 was Correct +0^8 was Correct +0^11 was Correct +1^0 was Correct +1^1 was Correct +1^2 was Correct +1^6 was Correct +1^8 was Correct +1^11 was Correct +4^0 was Correct +4^1 was Correct +4^2 was Correct +4^6 was Correct +4^8 was Correct +4^11 was Correct +5^0 was Correct +5^1 was Correct +5^2 was Correct +5^6 was Correct +5^8 was Correct +5^11 was Correct +9^0 was Correct +9^1 was Correct +9^2 was Correct +9^6 was Correct +9^8 was Correct +9^11 was Correct +12^0 was Correct +12^1 was Correct +12^2 was Correct +12^6 was Correct +12^8 was Correct +12^11 was Correct + +Overall Grade: A diff --git a/23_power_rec/power.c b/23_power_rec/power.c new file mode 100644 index 0000000..6be0c74 --- /dev/null +++ b/23_power_rec/power.c @@ -0,0 +1,12 @@ +#include +#include + +unsigned power(unsigned x, unsigned y) { + if (y == 0) { + return 1; + } + if (y == 1) { + return x; + } + return x * power(x, y-1); +} diff --git a/23_power_rec/power.o b/23_power_rec/power.o new file mode 100644 index 0000000..58b2c78 Binary files /dev/null and b/23_power_rec/power.o differ diff --git a/23_power_rec/test-power b/23_power_rec/test-power new file mode 100755 index 0000000..8c63568 Binary files /dev/null and b/23_power_rec/test-power differ diff --git a/23_power_rec/test-power.c b/23_power_rec/test-power.c new file mode 120000 index 0000000..7480020 --- /dev/null +++ b/23_power_rec/test-power.c @@ -0,0 +1 @@ +../22_tests_power/test-power.c \ No newline at end of file -- cgit v1.2.3