]> zdv2.bktei.com Git - BK-2023-05.git/commitdiff
feat(src/kr/ch1/s1.6/e1-14):Complete exercise
authorSteven Baltakatei Sandoval <baltakatei@gmail.com>
Tue, 13 Sep 2022 08:47:42 +0000 (08:47 +0000)
committerSteven Baltakatei Sandoval <baltakatei@gmail.com>
Tue, 13 Sep 2022 08:47:42 +0000 (08:47 +0000)
src/kr_exercises/ch1/s1.6/e1-13..histogram_word_length.c
src/kr_exercises/ch1/s1.6/e1-14-2..print_char_stats.c [new file with mode: 0644]
src/kr_exercises/ch1/s1.6/e1-14..histogram_char_frequency.c [new file with mode: 0644]
src/kr_exercises/ch1/s1.6/e1-14..notes.org [new file with mode: 0644]

index 86184150eb45829f8c9be79f2b05df7ea6d92fc6..011d27abc7f459abd6649fcbcfc427e1caecf405 100644 (file)
@@ -4,7 +4,7 @@
    of words in its input. It is easy to draw the histogram with the
    bars horizontal; a vertical orientation is more challenging. */
 
    of words in its input. It is easy to draw the histogram with the
    bars horizontal; a vertical orientation is more challenging. */
 
-#define MAX_LEN      4  /* Max acceptable word length */
+#define MAX_LEN      20  /* Max acceptable word length */
 #define MAX_DISP_LEN 70  /* Max length of horizontal bar */
 #define IN            1  /* Inside a word */
 #define OUT           0  /* Outside a word */
 #define MAX_DISP_LEN 70  /* Max length of horizontal bar */
 #define IN            1  /* Inside a word */
 #define OUT           0  /* Outside a word */
diff --git a/src/kr_exercises/ch1/s1.6/e1-14-2..print_char_stats.c b/src/kr_exercises/ch1/s1.6/e1-14-2..print_char_stats.c
new file mode 100644 (file)
index 0000000..99d5bfa
--- /dev/null
@@ -0,0 +1,27 @@
+#include <stdio.h>
+
+/* Prints stats of chars provided. */
+
+int main() {
+  int c, n, cmin, cmax;
+
+  n = 0;
+  cmin = cmax = -1;
+  while( (c = getchar()) != EOF) {
+    if( n == 0 )
+      cmin = cmax = c;
+
+    if( c != 10 ) {
+      if( c < cmin )
+        cmin = c;
+      else if (c > cmax)
+        cmax = c;
+    };
+
+    n++;
+  };
+
+  printf("char count  :%d\n", n   );
+  printf("char int min:%d\n", cmin);
+  printf("char int max:%d\n", cmax);
+};
diff --git a/src/kr_exercises/ch1/s1.6/e1-14..histogram_char_frequency.c b/src/kr_exercises/ch1/s1.6/e1-14..histogram_char_frequency.c
new file mode 100644 (file)
index 0000000..907a0b1
--- /dev/null
@@ -0,0 +1,48 @@
+#include <stdio.h>
+
+/* Exercise 1-14. Write a program to print a histogram of the
+   frequencies of different characters in its input. */
+
+#define INT_CHAR_MIN   32
+#define INT_CHAR_MAX  126
+#define INT_CHAR_LEN  INT_CHAR_MAX - INT_CHAR_MIN + 1
+#define INT_NEWLINE    10
+
+
+int main() {
+  int c, counts_idx, flg_intchar_err;
+  int counts[INT_CHAR_LEN];
+  for(int i = 0; i < INT_CHAR_LEN; ++i)
+    counts[i] = 0;
+
+  
+  /* Read char from input; Ignore newlines. */
+  flg_intchar_err = 0;
+  while( (c = getchar()) != EOF ) {
+    if( c >= INT_CHAR_MIN && c <= INT_CHAR_MAX ) {
+      /* Increment corresponding element in counts[] array */
+      counts_idx = c - INT_CHAR_MIN;
+      ++counts[counts_idx];
+    } else if ( c != INT_NEWLINE )
+      flg_intchar_err = 1;
+  };
+
+  /* Print contents of counts[i] */
+  printf("counts[i]:(ASCII code):( symbol ): count : histogram bar\n");
+  for(int i = 0; i < INT_CHAR_LEN; ++i) {
+    printf("counts[%03d]:(%03d):( %c ):%03d:",
+           i, i + INT_CHAR_MIN, i + INT_CHAR_MIN, counts[i]);
+    for(int j = 0; j < counts[i]; ++j)
+      printf("#");
+    printf("\n");
+  };
+    
+
+  /* Report input errors */
+  if( flg_intchar_err == 1 )
+    printf("ERROR:Unrecognized char int provided.");
+  return 0;
+};
+
+/* Author: Steven Baltakatei Sandoval
+   License: GPLv3+ */
diff --git a/src/kr_exercises/ch1/s1.6/e1-14..notes.org b/src/kr_exercises/ch1/s1.6/e1-14..notes.org
new file mode 100644 (file)
index 0000000..be75f2a
--- /dev/null
@@ -0,0 +1,19 @@
+* Example 1-14 notes
+
+** Problem
+Exercise 1-14. Write a program to print a histogram of the frequencies
+of different characters in its input.
+
+** Strategy
+- Create array ~counts[]~ to store anticipated types of char to be
+  returned by ~getchar()~, one element per possible char ~int~.
+  - Each ~int~ produced by ~getchar()~ should map to a single index
+    value in ~counts[]~. Limit to ASCII range.
+  - Note: Using ~e1-14-2..print_char_stats.c~, ASCII range in decimal
+    is determined to be ~[32 126]~. This means ~counts[]~ should have
+    a total of ~126 - 32 + 1 = 95~ elements.
+- Read every char using a ~while()~ loop and ~getchar()~.
+  - Increment appropriate element of ~counts[]~.
+- Display horizontal histogram by printing a line of ~#~'s for each
+  index of ~counts[]~; the number of ~#~'s is the size of the
+  corresponding element of ~counts[]~.