Cleaned up formatting. Removed an unused comparison in the for loop responsible for printing the histogram.
This commit is contained in:
parent
14730549f1
commit
136abce9bf
1 changed files with 30 additions and 13 deletions
|
|
@ -1,6 +1,10 @@
|
|||
#include <stdio.h>
|
||||
|
||||
/*Slight modification to ex1_13.c that makes charCount an array of ints 11 entries long, in order to circumvent the need for additional mathematical operations down the line as a consequence of ocurrences of the value "1" being store at position 0. This sacrifices 4 bytes of memory in the stack by extending the array, but results a shorter binary.
|
||||
/*Slight modification to ex1_13.c that makes charCount an array of ints 11
|
||||
* entries long, in order to circumvent the need for additional mathematical
|
||||
* operations down the line as a consequence of ocurrences of the value "1"
|
||||
* being store at position 0. This sacrifices 4 bytes of memory in the stack
|
||||
* by extending the array, but results a shorter binary.
|
||||
*
|
||||
* how much shorter, you ask?
|
||||
*
|
||||
|
|
@ -17,7 +21,12 @@ int main(){
|
|||
|
||||
int charCount[11]; /*Array for storing char lengths*/
|
||||
|
||||
int last, lCount, state, i; /*last stores last character, or 0 at init. lCount increments every time the main for loop encounters a non-newline/whitespace character and then resets when one is encountered after commiting its value to charCount.*/
|
||||
int last, lCount, state, i; /*last stores last character, or 0 at init.
|
||||
lCount increments every time the main for
|
||||
loop encounters a non-newline/whitespace
|
||||
character and then resets when one is
|
||||
encountered after commiting its value
|
||||
to charCount.*/
|
||||
/*Begin array initialization*/
|
||||
for(i = 0; i < 11; ++i){
|
||||
charCount[i] = 0;
|
||||
|
|
@ -30,27 +39,33 @@ int main(){
|
|||
/*Begin input letter count enumeration.*/
|
||||
for(int a = getchar(); a != EOF; a = getchar()){
|
||||
|
||||
/*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.*/
|
||||
if(lCount > 10){ /*Array charCount only stores values up to 10, so we will add any longer words to the column for 10+*/
|
||||
if (last != '\t' && last != ' ' && last != '\n'){ /*True on word
|
||||
exit only.*/
|
||||
if(lCount > 10){ /*Array charCount only stores values up to 10,
|
||||
so we will add any longer words to the
|
||||
column for 10+*/
|
||||
++charCount[10];
|
||||
} else {
|
||||
|
||||
++charCount[(lCount)]; }
|
||||
lCount = 0; /*Reset lCount when a condition that dumps it to charCount is triggered*/
|
||||
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.*/
|
||||
++lCount; /*Increment lCount when encountering a non-WS
|
||||
character.*/
|
||||
}
|
||||
last = a;
|
||||
}
|
||||
/*Input letter count enumeration complete*/
|
||||
|
||||
/*Print a table of the number of words encountered for each number of characters.*/
|
||||
/*Print a table of the number of words encountered for
|
||||
* each number of characters.*/
|
||||
printf("Word letter count composition:\n");
|
||||
for (i = 1; i <= 9; ++i)
|
||||
printf("%3d", (i));
|
||||
|
|
@ -68,11 +83,13 @@ int main(){
|
|||
printf("%3s", "10+");
|
||||
}
|
||||
|
||||
for (b > 0; b > 0; --b){
|
||||
for (; 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*/
|
||||
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;
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue