72. C Program To Display Frequency Table from the Given Array Elements.
How does this program work?
- This Program is used to display the frequency table using array elements.
Here is the code
#include <stdio.h>
int main(void)
{
int arr[100], freq[100];
int size, i, j, count;
/* Input size of array */;
printf("Enter size of array: ");
scanf("%d", &size);
/* Input elements in array */
printf("Enter elements in array: ");
for(i=0; i< size; i++)
{
scanf("%d", &arr[i]);
/* Initially initialize frequencies to -1 */
freq[i] = -1;
}
for(i=0; i < size; i++)
{
count = 1;
for(j=i+1; j < size; j++)
{
// If duplicate element is found increment count
if(arr[i]==arr[j])
{
count++;
//To stop count frequency of same element again
freq[j] = 0;
}
}
//if element is not repeated count will be same
if(freq[i] != 0)
{
freq[i] = count;
}
}
//Display output of array
printf("\nFrequency of all elements of array : \n");
for(i=0; i < size; i++)
{
if(freq[i] != 0)
{
printf("%d occurs %d times\n", arr[i], freq[i]);
}
}
return 0;
}