Write a program in C to count the frequency of each element of an array. Test Data: Input the number of elements to be stored in the Array: 3 Input 3 elements in the array: element - 0 : 25 element - 1 : 12 element - 2 : 43 Expected Output : The frequency of all elements of an array : 25 occurs 1 times 12 occurs 1 times 43 occurs 1 times
C Program:
#include <stdio.h>
#define SMALL -10000009
#define SIZE 100000
struct hashNode
{
int key;
int freq;
};
struct hashNode hashArray[SIZE];
void insert(int key)
{
for(int i=0; i<SIZE; i++)
{
///if key is equal to key, increment freq by 1 and return
if(hashArray[i].key==key)
{
hashArray[i].freq++;
return;
}
///if we found any empty location, we insert key there
else if(hashArray[i].key==SMALL)
{
hashArray[i].key=key;
hashArray[i].freq++;
return;
}
///else we iterate through the hasArray
}
}
void printHash()
{
printf("\n\nFrequency of all elemnts are:\n");
for(int i=0; i<SIZE; i++)
{
if(hashArray[i].key==SMALL)
return;
else
{
printf("%d occurs %d
times\n",hashArray[i].key,hashArray[i].freq);
}
}
}
void BuildHash()
{
///initialize all the key to SMALL
///freq is set to 0
for(int i=0; i<SIZE; i++)
{
hashArray[i].key=SMALL;
hashArray[i].freq=0;
}
}
int main()
{
///BuidHash is a function that build a simple hash function.
///the size of hash array is SIZE
BuildHash();
int n;
printf("Enter the no of elements: ");
scanf("%d",&n);
printf("\n"); ///input from user
int *arr;
arr=malloc(n);/// dynamic memeory allocation using malloc
///takes input n times
for(int i=0; i<n; i++)
{
printf("Element %d: ",i);
int temp;
scanf("%d,",&temp); ///taking ith elements
insert(temp); ///calling insert function to insert in hash
}
///printHash function prints all the content of the hash
built
printHash();
return 0;
}
SCREENSHOT:



I have tried my best to explain all the functionality in the best way. If u still feel difficulty in understanding any portion, feel free to comment down in the comment box.
If this is all you wanted and you find it helpful, please upvote the answer.
Write a program in C to count the frequency of each element of an array. Test...
in C language
Write a program to find the element of an array, which occurs maximum number of times and its count. The program should accept the array elements as input from the user and print out the most occurring element along with its count. Hint: array size is 5. For example: Enter array elements (array size 5): 22321 Output: Max occurring element: 2. count: 3
Sample input/output dialogue: Enter array element limit: 5 Enter 5 elements in the array: 1 1 2 3 1 Enter element to search for frequency : 1 Result is : 1 occurs 3 times
Write a C program convert.c that converts each number in an array by the sum of that number plus 6 modulus 10. A sample input/output: Enter the length of the array: 5 Enter the elements of the array: 3 928 4 14 77 Output: 9 4 0 0 3 The program should include the following function: void convert(int *a1, int n, int *a2) The function converts every element in array a1 of length n to an output array a2. The...
In need this in C language!
Write a program that declares a 40 element array of type double.
Initialize the array so that:
the first 10 elements are equal to the square of the index
variable
the next 10 each elements is equal to three times the index
variable
the next 10 each element is equal to the sum of an element in
the first 10 + an element in the second 10
the last 10 each element is equal...
Write a program in ARM assembly language that copies each element of array A to consecutive fourth elements of array B, i.e., A[0] to B[0], A[1] to B[3], A[2] to B[7], etc. The array A is 12 elements long, and each element is a number that is 32 bits (1 word) wide. Assume the base address of array A is in register R2, and the base address of array B is in R3.
write in c programming
Write a C program that reads in up to 10 numbers into an array. The program should count the number of duplicate elements within that array and print the result. Example test data and expected output is given below Test Data: [2,1,1,2,1,3, 4] Duplicates:
Write a complete C++ program that reads students names and their test scores from an input text file. The program should output each student’s name followed by the test scores and the relevant grade in an output text file. It should also find and display on screen the highest/lowest test score and the name of the students having the highest/lowest test score, average and variance of all test scores. Student data obtained from the input text file should be stored...
E2.15 In assembly code, write a program to count the number of elements in an array that are smaller than 16. The array is stored at memory locations starting from $1010. The array has 30 8-bit unsigned elements. Store the count in the memory location $C001.
Please do in C and only use stdio.h. Write a program that reads n integers into an array, then prints on a separate line the value of each distinct element along with the number of times it occurs. The values should be printed in ascending order. Turn in your code and input/result together. Suppose, for example, that you input the values -7 3 3 -7 5 5 3 as the elements of your array. Then your program should print -7...
in a c++ visual studio 2017 ...Write a program that simulates a lottery. The program should have an array of five integers named lottery and should generate a random number in the range 0 through 9 for each element in the array. The user should enter five digits, which should be stored in an integer array named user. The program is to compare the corresponding element in the two arrays and keep a count of the digits that match. For...