Using arrays data structure, write a C program that gives the number with high occurrences
#include <stdio.h>
int main()
{
int numbers[100],n,i,j;
int count;
int numberCounter[100];
int maxCounter;
int numberWighCccurrences;
printf("Enter n: ");
scanf("%d",&n);
if(n>0){
//read numbers
for(i=0;i< n;i++)
{
printf("Enter number %d: ",i+1);
scanf("%d",&numbers[i]);
numberCounter[i]=-1;
}
//count occurance of numbers
for(i=0; i<n; i++)
{
count = 1;
for(j=i+1; j<n; j++)
{
if(numbers[i]==numbers[j])
{
count++;
numberCounter[j] = 0;
}
}
if(numberCounter[i] != 0)
{
numberCounter[i] = count;
}
}
printf("\nFrequency of all elements of array : \n");
for(i=0; i<n; i++)
{
if(numberCounter[i] != 0)
{
printf("%d occurs %d times\n", numbers[i], numberCounter[i]);
}
}
//find the number with high occurrences
maxCounter=numberCounter[0];
numberWighCccurrences=numbers[0];
for(i=1; i<n; i++)
{
//find max values in array "numberCounter"
if(numberCounter[i]>maxCounter){
maxCounter=numberCounter[i];
numberWighCccurrences=numbers[i];
}
}
//display the number with high occurrences
printf("\n%d is the number with high occurrences.\n",numberWighCccurrences);
}
return 0;
}
Example:
Comments
Leave a comment