From e0ac6e6a3c74f0e4d5936462eaae4d450fa3ed43 Mon Sep 17 00:00:00 2001 From: Adam Carpenter <53hornet@gmail.com> Date: Tue, 14 May 2019 11:40:11 -0400 Subject: Finished functions. --- charhist.c | 30 ++++++++++++++++++++++++++++++ ctof.c | 20 ++++++++++++++++---- ftoc.c | 13 +++++++++---- power.c | 28 ++++++++++++++++++++++++++++ 4 files changed, 83 insertions(+), 8 deletions(-) create mode 100644 charhist.c create mode 100644 power.c diff --git a/charhist.c b/charhist.c new file mode 100644 index 0000000..312df76 --- /dev/null +++ b/charhist.c @@ -0,0 +1,30 @@ +#include + +#define CHARSET_LENGTH 128; + +/* + * Prints a histogram of the lengths of words in its input. + */ +main() { + int c, i; + int count; + int length = CHARSET_LENGTH; + int c_counts[length]; + + for (i = 0; i < length; ++i) { + c_counts[i] = 0; + } + + while ((c = getchar()) != EOF) { + ++c_counts[c]; + } + + for (i = 0; i < length; ++i) { + count = c_counts[i]; + + if (count > 0 && (i != ' ' && i != '\t' && i != '\n' )) { + printf("%c\t", i); + printf("%d\n", count); + } + } +} diff --git a/ctof.c b/ctof.c index 7e2cbef..b95e20a 100644 --- a/ctof.c +++ b/ctof.c @@ -1,5 +1,11 @@ #include +#define LOWER 0; +#define UPPER 300; +#define STEP 20; + +float ctof(int c); + /* * Print Celcius-Fahrenheit table for celcius = 0, 20, ... 300. */ @@ -7,18 +13,24 @@ main() { float fahr, celcius; int lower, upper, step; - lower = 0; // lower limit of temperature table - upper = 300; // upper limit of temperature table - step = 20; // step size + lower = LOWER; // lower limit of temperature table + upper = UPPER; // upper limit of temperature table + step = STEP; // step size celcius = lower; printf("%3s %6s\n", "C", "F"); while (celcius <= upper) { - fahr = (9.0 / 5.0) * celcius + 32.0; + fahr = ctof(celcius); printf("%3.0f %6.1f\n", celcius, fahr); celcius = celcius + step; } } +/* + * Convert Celcius to Fahrenheit + */ +float ctof(int celcius) { + return (9.0 / 5.0) * celcius + 32.0; +} diff --git a/ftoc.c b/ftoc.c index 2efa6f7..70b994a 100644 --- a/ftoc.c +++ b/ftoc.c @@ -4,6 +4,8 @@ #define UPPER 300 // upper limit of table #define STEP 20 // step size +float ftoc(int fahr); + /* * Print Fahrenheit-Celcius table for fahr = 0, 20, ... 300. */ @@ -11,9 +13,9 @@ main() { float fahr, celcius; int lower, upper, step; - lower = 0; // lower limit of temperature table - upper = 300; // upper limit of temperature table - step = 20; // step size + lower = LOWER; // lower limit of temperature table + upper = UPPER; // upper limit of temperature table + step = STEP; // step size fahr = lower; @@ -26,7 +28,10 @@ main() { // } for (fahr = UPPER; fahr >= LOWER; fahr = fahr - STEP) { - printf("%3.0f %6.1f\n", fahr, (5.0 / 9.0) * (fahr - 32)); + printf("%3.0f %6.1f\n", fahr, ftoc(fahr)); } } +float ftoc(int fahr) { + return (5.0 / 9.0) * (fahr - 32); +} diff --git a/power.c b/power.c new file mode 100644 index 0000000..8bca4fd --- /dev/null +++ b/power.c @@ -0,0 +1,28 @@ +#include + +int power(int m, int n); + +/* + * Test power function + */ +main() { + int i; + + for (i = 0; i < 10; ++i) + printf("%d %d %d\n", i, power(2, i), power(-3, i)); + + return 0; +} + +/* + * Raise base to n-th power; n >= 0 + */ +int power(int base, int n) { + int i, p; + + p = 1; + + for (i = 1; i <= n; ++i) + p = p * base; + return p; +} -- cgit v1.2.3