{"content":{"id":318,"title":"K\u0026R Solutions - 1.13, Part 2","body":"\u003cp\u003e\u003cb\u003eQ:\u003c/b\u003e The same as part one, but vertical.\u003c/p\u003e\r\n\u003cp\u003e\u003cb\u003eA:\u003c/b\u003e\u003c/p\u003e \r\n\u003cpre\u003e\u003ccode class=\"cpp\"\u003e#include \u0026ltstdio.h\u0026gt\r\n\r\nint main(void) \r\n{\r\n int FREQUENCY_LENGTH = 10; \r\n int frequencies[FREQUENCY_LENGTH];\r\n int i, j, c;\r\n int tally = 0;\r\n int highest = 0;\r\n\r\n // We're tallying in the exact same way.\r\n for (i = 0; i \u003c FREQUENCY_LENGTH; ++i) {\r\n frequencies[i] = 0;\r\n }\r\n\r\n while ((c = getchar()) != EOF) {\r\n if (c == '.' || c == '\\n' || c == '\\t' || c == ' ' || c == ',') {\r\n if (tally \u003e FREQUENCY_LENGTH) {\r\n tally = FREQUENCY_LENGTH - 1;\r\n }\r\n ++frequencies[tally];\r\n tally = 0;\r\n } else {\r\n ++tally;\r\n }\r\n }\r\n\r\n /* First, we need the height of the highest bar.\r\n * At the top of the function, temp is set to zero. Normally a function\r\n * wouldn't be this long, but we haven't got to that part fo the book yet.\r\n */\r\n for (i = 0; i \u003c FREQUENCY_LENGTH; i++) {\r\n if (frequencies[i] \u003e highest) {\r\n highest = frequencies[i];\r\n }\r\n }\r\n\r\n /* Now we keep drawing the bars untill our 'highest' variable, and everything\r\n * in our tallies is zeroed out.\r\n */\r\n while (highest) {\r\n // Remember there are no zero-length words so we skip 0. Feel free to play\r\n // around with this, because some junk data is getting stored in\r\n // frequencies. Why?\r\n for(i = 1; i \u003c FREQUENCY_LENGTH; i++) {\r\n if (frequencies[i] == highest) {\r\n printf(\" | \");\r\n frequencies[i]--;\r\n } else {\r\n printf(\" \");\r\n }\r\n }\r\n printf(\"\\n\");\r\n highest--;\r\n }\r\n\r\n /* Now we print a handy guide at the bottom.\r\n */\r\n for (i = 1; i \u003c FREQUENCY_LENGTH; i++) {\r\n printf(\"---\");\r\n }\r\n printf(\"-\\n\");\r\n for (i = 1; i \u003c FREQUENCY_LENGTH; i++) {\r\n if (i == FREQUENCY_LENGTH - 1) {\r\n printf(\" %d+\", i);\r\n } else {\r\n printf(\" %d \", i);\r\n }\r\n }\r\n printf(\"\\n\");\r\n\r\n return 0;\r\n}\r\n\u003c/code\u003e\u003c/pre\u003e\r\n\u003cpre\u003e\u003ccode class=\"lisp\"\u003eThe worst of misery\r\nIs when a nature framed for noblest things\r\nCondemns itself in youth to petty joys,\r\nAnd, sore athirst for air, breathes scanty life\r\nGasping from out the shallows.\r\nSupercalifragilisticexpialidocious.\r\n\r\n(ctrl+d)\r\n\r\n | \r\n | | \r\n | | | \r\n | | | | \r\n | | | | | | | \r\n | | | | | | | \r\n | | | | | | | | | \r\n----------------------------\r\n 1 2 3 4 5 6 7 8 9+ \r\n\u003c/code\u003e\u003c/pre\u003e\r\n\u003cp\u003eYou can play around with this quite a bit. Removing one line and then changing one character, and we get a nice little graph:\u003c/p\u003e\r\n\u003cpre\u003e\u003ccode class=\"lisp\"\u003e . \r\n . \r\n . \r\n . \r\n . . . \r\n \r\n . . \r\n----------------------------\r\n 1 2 3 4 5 6 7 8 9+ \r\n\u003c/code\u003e\u003c/pre\u003e","publication_date":"2018-10-14T01:00:00.000Z","created_at":"2018-10-13T17:15:36.000Z","updated_at":"2018-10-13T19:47:42.000Z","user_id":1,"rating":null},"tags":"\u003ca class=\"changeable-title\" href=\"/q?tag=k%26r\"\u003ek\u0026amp;r\u003c/a\u003e \u003ca class=\"changeable-title\" href=\"/q?tag=programming\"\u003eprogramming\u003c/a\u003e \u003ca class=\"changeable-title\" href=\"/q?tag=answers\"\u003eanswers\u003c/a\u003e \u003ca class=\"changeable-title\" href=\"/q?tag=c\"\u003ec\u003c/a\u003e \u003ca class=\"changeable-title\" href=\"/q?tag=unix\"\u003eunix\u003c/a\u003e"}

## K&R Solutions - 1.13, Part 2

Q: The same as part one, but vertical.

A:

``````#include <stdio.h>

int main(void)
{
int FREQUENCY_LENGTH = 10;
int frequencies[FREQUENCY_LENGTH];
int i, j, c;
int tally = 0;
int highest = 0;

// We're tallying in the exact same way.
for (i = 0; i < FREQUENCY_LENGTH; ++i) {
frequencies[i] = 0;
}

while ((c = getchar()) != EOF) {
if (c == '.' || c == '\n' || c == '\t' || c == ' ' || c == ',') {
if (tally > FREQUENCY_LENGTH) {
tally = FREQUENCY_LENGTH - 1;
}
++frequencies[tally];
tally = 0;
} else {
++tally;
}
}

/* First, we need the height of the highest bar.
*  At the top of the function, temp is set to zero. Normally a function
*  wouldn't be this long, but we haven't got to that part fo the book yet.
*/
for (i = 0; i < FREQUENCY_LENGTH; i++) {
if (frequencies[i] > highest) {
highest = frequencies[i];
}
}

/* Now we keep drawing the bars untill our 'highest' variable, and everything
* in our tallies is zeroed out.
*/
while (highest) {
// Remember there are no zero-length words so we skip 0. Feel free to play
// around with this, because some junk data is getting stored in
// frequencies. Why?
for(i = 1; i < FREQUENCY_LENGTH; i++) {
if (frequencies[i] == highest) {
printf(" | ");
frequencies[i]--;
} else {
printf("   ");
}
}
printf("\n");
highest--;
}

/* Now we print a handy guide at the bottom.
*/
for (i = 1; i < FREQUENCY_LENGTH; i++) {
printf("---");
}
printf("-\n");
for (i = 1; i < FREQUENCY_LENGTH; i++) {
if (i == FREQUENCY_LENGTH - 1) {
printf(" %d+", i);
} else {
printf(" %d ", i);
}
}
printf("\n");

return 0;
}
``````
``````The worst of misery
Is when a nature framed for noblest things
Condemns itself in youth to petty joys,
And, sore athirst for air, breathes scanty life
Gasping from out the shallows.
Supercalifragilisticexpialidocious.

(ctrl+d)

|
|        |
|  |     |
|  |  |     |
|  |  |  |  |  |  |
|  |  |  |  |  |  |
|  |  |  |  |  |  |  |  |
----------------------------
1  2  3  4  5  6  7  8  9+
``````

You can play around with this quite a bit. Removing one line and then changing one character, and we get a nice little graph:

``````       .
.
.
.
.     .  .

.                       .
----------------------------
1  2  3  4  5  6  7  8  9+
``````