From 44d39a638be1d1054d0e94f787dc1f6652bb8a90 Mon Sep 17 00:00:00 2001 From: Steven Baltakatei Sandoval Date: Tue, 13 Sep 2022 08:47:42 +0000 Subject: [PATCH] feat(src/kr/ch1/s1.6/e1-14):Complete exercise --- .../ch1/s1.6/e1-13..histogram_word_length.c | 2 +- .../ch1/s1.6/e1-14-2..print_char_stats.c | 27 +++++++++++ .../s1.6/e1-14..histogram_char_frequency.c | 48 +++++++++++++++++++ src/kr_exercises/ch1/s1.6/e1-14..notes.org | 19 ++++++++ 4 files changed, 95 insertions(+), 1 deletion(-) create mode 100644 src/kr_exercises/ch1/s1.6/e1-14-2..print_char_stats.c create mode 100644 src/kr_exercises/ch1/s1.6/e1-14..histogram_char_frequency.c create mode 100644 src/kr_exercises/ch1/s1.6/e1-14..notes.org diff --git a/src/kr_exercises/ch1/s1.6/e1-13..histogram_word_length.c b/src/kr_exercises/ch1/s1.6/e1-13..histogram_word_length.c index 8618415..011d27a 100644 --- a/src/kr_exercises/ch1/s1.6/e1-13..histogram_word_length.c +++ b/src/kr_exercises/ch1/s1.6/e1-13..histogram_word_length.c @@ -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. */ -#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 */ 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 index 0000000..99d5bfa --- /dev/null +++ b/src/kr_exercises/ch1/s1.6/e1-14-2..print_char_stats.c @@ -0,0 +1,27 @@ +#include + +/* 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 index 0000000..907a0b1 --- /dev/null +++ b/src/kr_exercises/ch1/s1.6/e1-14..histogram_char_frequency.c @@ -0,0 +1,48 @@ +#include + +/* 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 index 0000000..be75f2a --- /dev/null +++ b/src/kr_exercises/ch1/s1.6/e1-14..notes.org @@ -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[]~. -- 2.39.5