Bookmark and Share

free counters
Back to Problem list
Problem 22: Horizontal Histogram
Write a program that accepts a set of digits (0 to 9) as input and prints a horizontal histogram representing the occurrences of each digit.
Input:
12
1 7 2 9 6 7 1 3 7 5 7 9

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


#include<stdio.h>
#define maxn 10

int fre[maxn];

void Print() {
    int i, j;
    for(i = 0; i<10; i++) {
        printf("%d",i);
        for(j = 0; j<fre[i]; j++) {
                printf(" *");
        }
        printf("\n");
    }
}
int main () {
    int n, k, i;
    scanf("%d",&n);
    for(i = 0; i<n; i++) {
        scanf("%d",&k);
        if(k < 10) {
          fre[k]++;
        }
    }
    Print();
    return 0;
}
                                
Back to Problem list