diff options
-rw-r--r-- | charhist.c | 30 | ||||
-rw-r--r-- | ctof.c | 20 | ||||
-rw-r--r-- | ftoc.c | 13 | ||||
-rw-r--r-- | power.c | 28 |
4 files changed, 83 insertions, 8 deletions
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<stdio.h> + +#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); + } + } +} @@ -1,5 +1,11 @@ #include<stdio.h> +#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; +} @@ -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); +} @@ -0,0 +1,28 @@ +#include<stdio.h> + +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; +} |