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 */
--- /dev/null
+#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);
+};
--- /dev/null
+#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+ */
--- /dev/null
+* 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[]~.