commit cd77509ab7500d3eaf0a12be089f8b9dcf4aaa16 from: jrmu date: Tue Jan 20 21:26:08 2026 UTC Solution for 1-13 commit - e86e8af835e80df38893086e43738548c6fc0c43 commit + cd77509ab7500d3eaf0a12be089f8b9dcf4aaa16 blob - /dev/null blob + 992ec22b1c4b6364fa8b30832a325f8525bbcbf6 (mode 644) --- /dev/null +++ 1-13-output @@ -0,0 +1,22 @@ +THE WAY TO WEALTH + +(From "Father Abraham's Speech," forming +the preface to Poor _Richard's Almanac_ for 1758.) + + 1: + 2: == + 3: ==== + 4: = + 5: = + 6: == + 7: === + 8: == + 9: = + 10: = + + | + | | + | | | | | + | | | | | | | | | + 1 2 3 4 5 6 7 8 9 10 + blob - /dev/null blob + f857b27aa2b18343e2fd5a17ba57d6ce4ba3e59f (mode 644) --- /dev/null +++ 1-13-output-2 @@ -0,0 +1,59 @@ +It would be thought a hard Government that should tax its People +one-tenth Part of their _Time_, to be employed in its Service. But +_Idleness_ taxes many of us much more, if we reckon all that is spent +in absolute _Sloth_, or doing of nothing, with that which is spent in +idle Employments or Amusements, that amount to nothing. _Sloth_, by +bringing on Diseases, absolutely shortens Life. _Sloth, like Rust, +consumes faster than Labor wears; while the used key is always bright, +as Poor Richard says. But dost thou love Life, then do not squander +Time, for that's the stuff Life is made of, as Poor Richard_ says. How +much more than is necessary do we spend in sleep, forgetting that _The +sleeping Fox catches no Poultry_, and that _There will be sleeping +enough in the Grave_, as _Poor Richard_ says. + + 1: = + 2: ================================= + 3: ================ + 4: ============================ + 5: ==================== + 6: =========== + 7: ======= + 8: =============== + 9: ==== + 10: ==== + 11: == + | + | + | + | + | + | | + | | + | | + | | + | | + | | + | | + | | + | | | + | | | + | | | + | | | + | | | | + | | | | | + | | | | | + | | | | | + | | | | | + | | | | | | + | | | | | | + | | | | | | + | | | | | | + | | | | | | | + | | | | | | | + | | | | | | | + | | | | | | | | | + | | | | | | | | | + | | | | | | | | | | + | | | | | | | | | | | + 1 2 3 4 5 6 7 8 9 10 11 + blob - /dev/null blob + a22dafbf6d5116dbe43af03557fe4dc05d55377c (mode 644) --- /dev/null +++ 1-13-text @@ -0,0 +1,4 @@ +THE WAY TO WEALTH + +(From "Father Abraham's Speech," forming +the preface to Poor _Richard's Almanac_ for 1758.) blob - /dev/null blob + 69be2704d4f3b1ac171315af6def07ec90b39026 (mode 644) --- /dev/null +++ 1-13-text-2 @@ -0,0 +1,12 @@ +It would be thought a hard Government that should tax its People +one-tenth Part of their _Time_, to be employed in its Service. But +_Idleness_ taxes many of us much more, if we reckon all that is spent +in absolute _Sloth_, or doing of nothing, with that which is spent in +idle Employments or Amusements, that amount to nothing. _Sloth_, by +bringing on Diseases, absolutely shortens Life. _Sloth, like Rust, +consumes faster than Labor wears; while the used key is always bright, +as Poor Richard says. But dost thou love Life, then do not squander +Time, for that's the stuff Life is made of, as Poor Richard_ says. How +much more than is necessary do we spend in sleep, forgetting that _The +sleeping Fox catches no Poultry_, and that _There will be sleeping +enough in the Grave_, as _Poor Richard_ says. blob - /dev/null blob + 8d86e31ea2888414c6414c8019f5f8e53c655eeb (mode 644) --- /dev/null +++ 1-13.c @@ -0,0 +1,65 @@ +/* 1-13 Write a program to print a histogram of the lengths of words in its + * input. It is easy to draw the histogram with the bars horizontal; a vertical + * orientation is more challenging. */ + +#include + +#define MAXLEN 100 + +int main() { + int c; /* current character */ + int words[MAXLEN]; /* occurrences of words by length */ + int length = 0; /* length of current word */ + int last = 0; /* last index of longest word */ + int mode = 0; /* length which occurs most frequently */ + for (int i = 0; i < MAXLEN; i++) { + words[i] = 0; + } + while ((c = getchar()) != EOF) { + if (c == ' ' || c == '\n' || c == '\t') { + if (length > 0) { + words[length]++; + } + length = 0; + } else { + length++; + } + } + + /* Determine last index and mode */ + for (int i = 0; i < MAXLEN; i++) { + if (words[i] > 0) { + last = i; + } + if (words[i] > mode) { + mode = words[i]; + } + } + + /* Horizontal histogram */ + for (int i = 1; i <= last; i++) { + printf("%3d: ", i); + for (int j = 0; j < words[i]; j++) { + printf("="); + } + printf("\n"); + } + + /* Vertical histogram */ + for (int j = mode; j > 0; j--) { + for (int i = 1; i <= last; i++) { + if (words[i] >= j) { + printf(" |"); + } else { + printf(" "); + } + if (i == last) + printf("\n"); + } + } + for (int i = 1; i <= last; i++) { + printf("%3d", i); + } + printf("\n"); + +}