Back to Problem list
int array[102] = {0}; --- what does it mean ? It means that we have declared an array with size 102 where each element assign to 0.
Another point to note that when we declare an array as global, all elements of that array asigned to 0 by default.
Back to Problem list
Problem 6: Given a set of integers, write a program to count the frequency of each integer. integer range is 0 - 100.
Solution: Very easy, take an array of size 102, let array[102], Assign 0 to all elements of the array. For each integer, say 'n', set array[n] ++, finally print the array.
Source Code:
#include<stdio.h>
int main() {
int n, i, m;
scanf("%d",&n); // number of integers will be read
int array[102] = {0};
while(n--) {
scanf("%d",&m); // reading each integer
array[m] ++;
}
for(i = 0; i<=100; i++) {
if(array[i] > 0) {
printf("%d -> %d\n",i,array[i]);
}
}
return 0;
}
|
int array[102] = {0}; --- what does it mean ? It means that we have declared an array with size 102 where each element assign to 0.
Another point to note that when we declare an array as global, all elements of that array asigned to 0 by default.
Try yourself: How can we print the frequency in descending order?
Example: let given integer set is: 2 3 2 2 5 6 7 7
Output:
2 -> 3 // 2 has maximum frequency, 3
7 -> 2
3 -> 1
5 -> 1
6 -> 1
Back to Problem list