Bookmark and Share

free counters
Back to Problem list
Problem 23:
Write a program that accepts a set of digits (0 to 9) as input and
prints a vertical histogram representing the occurrences of each digit.

Input:
12
1 7 2 9 6 7 1 3 7 5 7 9

Output:

       *
       *
 *     * *
 *** *** *
0123456789

#include<stdio.h>
#define maxn 10

int fre[maxn];


void Print(int max) {
    int i, j;
    for(i = max; i>=1; i--) {
        for(j = 0; j<10; j++) {
                if(fre[j] >= i) {
                   printf("*");
                }else {
                   printf(" ");
                }
        }
        printf("\n");
    }
    for(i = 0; i<10; i++) printf("%d",i);
}
int main () {
    int n, k, i, max = 0;
    scanf("%d",&n);
    for(i = 0; i<n; i++) {
        scanf("%d",&k);
        if(k < 10) {
          fre[k]++;
          if(max < fre[k]) max = fre[k];
        }
    }
    Print(max);
    return 0;
}


Back to Problem list