Count Frequencies
Completed
Read n marks between 0 and 10. Print a bar chart: for each mark that appears, mark: *** with one star per occurrence.
Input
Line 1: n. Line 2: n integers.
Output
One line per mark that appears.
Example
Input
8 7 3 7 10 0 7 3 9
Output
0: * 3: ** 7: *** 9: * 10: *
Editor
C
Hints
Initialise the counts array to 0 first.
Use the mark itself as the index.
Solution explanation
Local arrays start with garbage values in C - always initialise them.
Show solution
#include <stdio.h>
int main() {
int n, x, count[11];
for (int i = 0; i <= 10; i++) count[i] = 0;
scanf("%d", &n);
for (int i = 0; i < n; i++) {
scanf("%d", &x);
count[x]++;
}
for (int m = 0; m <= 10; m++) {
if (count[m] > 0) {
printf("%d: ", m);
for (int s = 0; s < count[m]; s++) printf("*");
printf("\n");
}
}
return 0;
}