samir82show

c_ch01_ex13.c

Sep 26th, 2014
213
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.75 KB | None | 0 0
  1. /*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.
  2. */
  3. #include <stdio.h>
  4. #define INDEX 50
  5. int main()
  6. {
  7. int hist[INDEX], c, i, longest = 0;
  8. int j, VertScan, HorzScan;
  9. for (i = 0;i < INDEX; i++)
  10. hist[i] = 0;
  11. i = 0;
  12. /*to read the value into array*/
  13. while ((c = getchar()) != EOF) {
  14. if (c != ' ' && c != '\t' && c != '\n') {
  15. i++;
  16. if (longest < i)
  17. longest = i;
  18. } else {
  19. if (i > 0)
  20. ++hist[i];
  21. i = 0;
  22. }
  23. }
  24. /*to print horzentally*/
  25. for (i = 0;i <= longest;i++) {
  26. printf ("hist[%d]|",i);
  27. for (j = 0;j < hist[i];j++)
  28. putchar ('#');
  29. printf ("\n");
  30. }
  31. printf ("\n");
  32. /*to print vertically*/
  33. for (VertScan = INDEX-20;VertScan > 0;VertScan--) {
  34. printf ("%2d|",VertScan);
  35. for (HorzScan = 1;HorzScan < 20;HorzScan++)
  36. if (VertScan <= hist[HorzScan])
  37. printf (" # ");
  38. else
  39. printf (" ");
  40. putchar ('\n');
  41. }
  42. for (HorzScan = 1;HorzScan <= 20;HorzScan++)
  43. printf (" -");
  44. putchar ('\n');
  45. printf (" ");
  46. for (HorzScan = 1;HorzScan <= 20;HorzScan++)
  47. printf ("%3d",HorzScan);
  48. putchar ('\n');
  49. return 0;
  50. }
Advertisement
Add Comment
Please, Sign In to add comment