Advertisement
cd62131

histogram

Dec 4th, 2018
483
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.62 KB | None | 0 0
  1. #include <stdio.h>
  2. int main(void) {
  3.     int freq[6] = {0};
  4.     int n;
  5.     while (scanf("%d", &n) == 1) {
  6.         if (n < 0 || 100 < n) {
  7.             continue;
  8.         }
  9.         ++freq[n / 20];
  10.     }
  11.     freq[4] += freq[5]; // for 100
  12.     // 12345678901234567890123456789012345678901234567890
  13.     // 00-000| * * * * 5 * * * *10 * * * *15 * * * *20
  14.     printf("%17d%10d%10d%10d\n", 5, 10, 15, 20);
  15.     for (int i = 0; i < 5; ++i) {
  16.         printf("%2d-%3d|", i * 20, ((i != 4) ? (i + 1) * 20 - 1 : 100));
  17.         for (int j = 0; j < freq[i]; ++j) {
  18.             printf(" *");
  19.         }
  20.         puts("");
  21.     }
  22. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement