fork download
  1. #include <stdio.h>
  2.  
  3. struct Record
  4. {
  5. int start;
  6. int end;
  7. int freq;
  8. };
  9. void display(const struct Record *records, const struct Record *x_axis, const struct Record *y_axis, int length, int width);
  10. int count_digits(int n);
  11.  
  12. int main(int argc, char *argv[])
  13. {
  14. struct Record x_axis, y_axis;
  15. int length, interval;
  16. scanf("%u %u %u %u\n", &x_axis.start, &x_axis.end, &y_axis.start, &y_axis.end);
  17. scanf("%u\n", &interval);
  18. scanf("%u\n", &length);
  19. int size = length / interval;
  20. struct Record records[size];
  21. int k = 0;
  22. int biggest_start = 0, biggest_end = 0;
  23. for(int i = 0; i < length; )
  24. {
  25. int a, b;
  26. struct Record current = {.start = 0, .end = 0, .freq = 0};
  27. for(int j = 0; j < interval; j++, i++)
  28. {
  29. scanf("%u %u\n", &a, &b);
  30. if(current.start == 0)
  31. current.start = a;
  32. current.freq += b;
  33. }
  34. current.end = a;
  35. //keeping track of the largest boundaries in order to estimate the width of the bars
  36. //eg : 132 and 1934 will give a width of 3 (132) + 4 (1934) + 1 (space inbetween the numbers) = 8
  37. if(current.start > biggest_start)
  38. biggest_start = current.start;
  39. if(current.end > biggest_end)
  40. biggest_end = current.end;
  41. current.freq /= interval;
  42. records[k++] = current;
  43. }
  44. display(records, &x_axis, &y_axis, size, count_digits(biggest_start) + count_digits(biggest_end) + 1);
  45.  
  46. return 0;
  47. }
  48.  
  49. void display(const struct Record *records, const struct Record *x_axis, const struct Record *y_axis, int length, int width)
  50. {
  51. int left_pad = count_digits(y_axis->end);
  52. for(int freq = y_axis->end; freq >= 1; freq--)
  53. {
  54. printf("%*d ", left_pad, freq);
  55. for(int i = 0; i < length; i++)
  56. {
  57. //http://stackoverflow.com/a/16299867/3729391
  58. if(records[i].freq >= freq)
  59. printf("%.*s ", width, "##########################");
  60. else
  61. printf("%.*s ", width, " ");
  62. }
  63. printf("\n");
  64. }
  65. printf("%*c ", left_pad, ' ');
  66. for(int i = 0; i < length; i++)
  67. printf("%d%*d ", records[i].start, width - count_digits(records[i].start), records[i].end);
  68. }
  69.  
  70. int count_digits(int n)
  71. {
  72. if(n == 0)
  73. return 1;
  74. for(int i = 1, count = 0; ; i *= 10, count++)
  75. if(n / i == 0)
  76. return count;
  77. }
Success #stdin #stdout 0s 9432KB
stdin
1 4 1 10
	2
	4
	1 3
	2 3
	3 2
	4 6
stdout
10         
 9         
 8         
 7         
 6         
 5         
 4     ### 
 3 ### ### 
 2 ### ### 
 1 ### ### 
   1 2 3 4