[ create a new paste ] login | about

Link: http://codepad.org/Otk7vmsx    [ raw code | output | fork ]

C, pasted on Jul 18:
#include <stdio.h>

int main(void) {
  int i, j, ch;
  int cnt_max = 0;
  static int cnt[10];
  
  while(1) {
    if ((ch = getchar()) == EOF)
      break;
    if(ch >= '0' || ch <= '9')
      cnt[ch - '0']++;
  }
  
  for(i = 0; i < 10; i++)
    if(cnt[i] > cnt_max)
      cnt_max = cnt[i];
  
  puts("histogram");
  for(j = 0; j < 10; j++) 
    printf(" %d ", j);
  putchar('\n');

  for(i = 1; i <= cnt_max ;i++){
    for(j = 0; j < 10; j++)
      if(cnt[j] >= i)
        printf(" * ");
      else
        printf("   ");
    putchar('\n');
  }
  return 0;
}
/* end */


Output:
1
2
histogram
 0  1  2  3  4  5  6  7  8  9 


Create a new paste based on this one


Comments: