Create a list of integers from user input. Write the C program to find the element that occurs with the highest frequency and also find the second-highest number in the given elements.Sample Input:
Question
Create a list of integers from user input. Write the C program to find the element that occurs with the highest frequency and also find the second-highest number in the given elements.Sample Input:
Solution
Sure, here is a simple C program that does what you asked for:
#include <stdio.h>
#define SIZE 100
int main() {
int arr[SIZE], freq[SIZE];
int size, i, j, count;
printf("Enter size of array: ");
scanf("%d", &size);
printf("Enter elements in array: ");
for(i=0; i<size; i++) {
scanf("%d", &arr[i]);
freq[i] = -1;
}
for(i=0; i<size; i++) {
count = 1;
for(j=i+1; j<size; j++) {
if(arr[i]==arr[j]) {
count++;
freq[j] = 0;
}
}
if(freq[i] != 0) {
freq[i] = count;
}
}
int max = freq[0];
int num = arr[0];
for(i=0; i<size; i++) {
if(freq[i] > max) {
max = freq[i];
num = arr[i];
}
}
printf("The number with highest frequency is %d and it occurs %d times\n", num, max);
int second_max = arr[0];
for(i=0; i<size; i++) {
if(arr[i] > second_max && arr[i] < num) {
second_max = arr[i];
}
}
printf("The second highest number is %d\n", second_max);
return 0;
}
This program first reads the size of the array and the elements from the user. It then calculates the frequency of each element and stores it in a separate array. The element with the highest frequency is then found and printed. The second highest number is also found and printed.
Similar Questions
You are given an array nums consisting of positive integers.Return the total frequencies of elements in nums such that those elements all have the maximum frequency.The frequency of an element is the number of occurrences of that element in the array. Example 1:Input: nums = [1,2,2,3,1,4]Output: 4Explanation: The elements 1 and 2 have a frequency of 2 which is the maximum frequency in the array.So the number of elements in the array with maximum frequency is 4
write a c program Find the largest number among the three numbers.
Frequency of all NumbersWrite a Program to print all the frequencies in a given an Array.Constraints:First line consists an integer representing the length of the array(n).Length of the array must be greater than zero or else Print Invalid Input.Second line consists all the Elements of an array.All the Elements in the array must be greaterthan zero or else print Invalid Input.Example:Input 1 : 6 1 2 2 3 3 3Output 1 : 1 - 1 2 - 2 3 - 3Input 1 : -6Output 1 : Invalid Input
Write a C program that takes an array of integers and its size as input and returns the maximum element in the array.Requirements:The function should be named findMax.The function should take two parameters: an integer array and its size.The function should return the maximum element in the array.
The numbers 3, 6, 12, 14 have frequencies 𝑥+6x+6, 𝑥+2x+2, 𝑥−3x−3 and 𝑥x respectively. If their mean is 5.97, find the value of 𝑥x. (Enter the value as next highest integer)
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.