From 14730549f113ee6a8687fe5eb4e92f8775e55859 Mon Sep 17 00:00:00 2001 From: SakurajimaShida Date: Thu, 21 Aug 2025 11:52:11 -0700 Subject: [PATCH] Push final version of ex1_13 to repo, as only the skeleton was included in the initial commit by mistake. --- KNR C/ex1_13.c | 52 +++++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 43 insertions(+), 9 deletions(-) diff --git a/KNR C/ex1_13.c b/KNR C/ex1_13.c index 6cc80ef..7fd8d7a 100644 --- a/KNR C/ex1_13.c +++ b/KNR C/ex1_13.c @@ -18,24 +18,58 @@ int main(){ last = lCount = 0; - + + /*Begin input letter count enumeration.*/ for(int a = getchar(); a != EOF; a = getchar()){ - if(a == '\t' || a == ' ' || a == '\n'){ /*Check if current char is WS. If so, set state to OUT and check if the last character was not. If it was not, then we are exiting a word, and so comitting lCount to charCount.*/ + /*Check if current char is WS. If so, set state to OUT and check if the last character was not. If it was not, then we are exiting a word, and so comitting lCount to charCount.*/ + if(a == '\t' || a == ' ' || a == '\n'){ state = OUT; if (last != '\t' && last != ' ' && last != '\n'){ /*True on word exit only.*/ - ++charCount[lCount-'0']; - lCount = 0; - } + if(lCount > 10){ /*Array charCount only stores values up to 10, so we will add any longer words to the column for 10+*/ + ++charCount[9]; /*Adding at position 11 in the array, since it starts at 0.*/ + } else { + + ++charCount[(lCount - 1)]; /*This decrements to array position lCount-1 in order to use position 0 in the array as position 1 since there are no 0-letter words, thus saving 4 bytes in the stack by not needing an array 11 ints long*/ + } + lCount = 0; /*Reset lCount when a condition that dumps it to charCount is triggered*/ + } } else { state = IN; ++lCount; /*Increment lCount when encountering a non-WS character.*/ } - last = a; + last = a; } - printf("Word letter count composition:\n1 2 3 4 5 6 7 8 9 10\n"); + /*Input letter count enumeration complete*/ + + /*Print a table of the number of words encountered for each number of characters.*/ + printf("Word letter count composition:\n"); + + /*Initialize table header*/ + for (i = 0; i <= 8; ++i) + printf("%3d", (i + 1)); + printf ("%3s", " 10+\n"); /*10+ is printed as a special case on its own, since it is more than just a letter.*/ + + /*Print the data in charCount numerically to a table.*/ for (i = 0; i < 10; ++i) - printf("%d ", charCount[i]); - return 0; + printf("%3d", charCount[i]); + + /*Print a histogram of gathered data.*/ + printf("\nHistogram of char counts by frequency of words enountered with a given count.\n"); + i = 0; /*Reset value of i, since we're reusing it*/ + for (int b = charCount[i]; i < 10; ++i){ + if (i < 9){ + printf("%d ", (i + 1)); + } else if (i = 9){ + printf("%3s", "10+"); + } + /*For loop with no entry condition that prints a | and then decrements b. TODO: Figure out a better way to do this that doesn't require cycling this loop b number of times.*/ + for (b > 0; b > 0; --b){ + printf("|"); + } + printf("\n"); + b = charCount[(i + 1)]; /*Get the value of the next position in the array; since i hasn't incremented yet, it's i+1*/ } + return 0; +}