Answer to Question #175992 in C for Rania

Question #175992


 Using arrays data structure, write a C program that gives the number with high occurrences 


1
Expert's answer
2021-03-28T00:25:10-0400
#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:


Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS