Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*Exercise 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 <stdio.h>
- #define INDEX 50
- int main()
- {
- int hist[INDEX], c, i, longest = 0;
- int j, VertScan, HorzScan;
- for (i = 0;i < INDEX; i++)
- hist[i] = 0;
- i = 0;
- /*to read the value into array*/
- while ((c = getchar()) != EOF) {
- if (c != ' ' && c != '\t' && c != '\n') {
- i++;
- if (longest < i)
- longest = i;
- } else {
- if (i > 0)
- ++hist[i];
- i = 0;
- }
- }
- /*to print horzentally*/
- for (i = 0;i <= longest;i++) {
- printf ("hist[%d]|",i);
- for (j = 0;j < hist[i];j++)
- putchar ('#');
- printf ("\n");
- }
- printf ("\n");
- /*to print vertically*/
- for (VertScan = INDEX-20;VertScan > 0;VertScan--) {
- printf ("%2d|",VertScan);
- for (HorzScan = 1;HorzScan < 20;HorzScan++)
- if (VertScan <= hist[HorzScan])
- printf (" # ");
- else
- printf (" ");
- putchar ('\n');
- }
- for (HorzScan = 1;HorzScan <= 20;HorzScan++)
- printf (" -");
- putchar ('\n');
- printf (" ");
- for (HorzScan = 1;HorzScan <= 20;HorzScan++)
- printf ("%3d",HorzScan);
- putchar ('\n');
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment