c - Need some suggestions on how to print a histogram more neatly -
i'm writing program read input , give histogram of character count k & r - ex. 1.13
any suggestions on how can improve code? matter whether or not if test status in condition or out first? have noticed in examples people test see if c blank or tab first.
i think need revisit histogram. doesn't scale results. draws hyphen based on length.
revised make little bit more readable think.
// print histogram of length of words in it's input. #include <stdio.h> #define in 1 #define out 2 #define max 99 int main(){ int c; // character int countofletters = 0; int insideword = out; int frequencyoflengths[max]; int longestwordcount = 0; int i, j; // counters (i = 0; < max; i++){ frequencyoflengths[i] = 0; } while ((c = getchar()) != eof){ if (c == ' ' || c == '\n' || c == '\t'){ if (insideword == in){ if (countofletters > max){ return 1; } ++frequencyoflengths[countofletters]; if (countofletters >= longestwordcount) longestwordcount = countofletters; } countofletters = 0; } else { countofletters++; insideword = in; } } (i = 1; <= longestwordcount; i++){ printf("%3i : %3i ", i, frequencyoflengths[i]); (j = 0; j < frequencyoflengths[i]; j++){ printf("*"); } printf("\n"); } return 0; }
definitely scale results, check out character histogram horizontal scaling histogram.
also, benefit y-axis label. it's hard tell bar kind of word length. have no idea bar word length.
i added code right before display histogram, halves every value, throw off bar number labels. can figure out!
// iterates , tells frequent word length int mostfrequent = 0; (i = 1; < maxword; i++) if (charcount[i] > mostfrequent) mostfrequent = charcount[i]; // if bar big, cut every value in half while (mostfrequent > 60) { (i = 1; < maxword; i++) if (charcount[i] > 0) { charcount[i] /= 2; charcount[i] |= 1; } // check again find frequent word length category mostfrequent = 0; (i = 1; < maxword; i++) if (charcount[i] > mostfrequent) mostfrequent = charcount[i]; }
honestly bars hard read, maybe use single row of characters such █ !
great book far, we're practically reading , on same page!
cheers
Comments
Post a Comment