summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAdam Carpenter <53hornet@gmail.com>2019-05-14 11:40:11 -0400
committerAdam Carpenter <53hornet@gmail.com>2019-05-14 11:40:11 -0400
commite0ac6e6a3c74f0e4d5936462eaae4d450fa3ed43 (patch)
treed24cf016b4996402e871e5d8da604a8e0e44b4fb
parent610018231b17e990e56396eaea4912f1b112e1f2 (diff)
downloadlearning-c-e0ac6e6a3c74f0e4d5936462eaae4d450fa3ed43.tar.xz
learning-c-e0ac6e6a3c74f0e4d5936462eaae4d450fa3ed43.zip
Finished functions.
-rw-r--r--charhist.c30
-rw-r--r--ctof.c20
-rw-r--r--ftoc.c13
-rw-r--r--power.c28
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);
+ }
+ }
+}
diff --git a/ctof.c b/ctof.c
index 7e2cbef..b95e20a 100644
--- a/ctof.c
+++ b/ctof.c
@@ -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;
+}
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<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;
+}