Question

In: Computer Science

C LANGUAGE ONLY Write a C program to count the frequency of each element in an...

C LANGUAGE ONLY

Write a C program to count the frequency of each element in an array.


Enter the number of elements to be stored in the array: 3

Input 3 elements of 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
3 occurs 1 times

Solutions

Expert Solution

#include <stdio.h>

int main()
{
//variable declaration
int num, temp, count, flag;
  
//get user input
printf("Enter the number of elements to be stored in the array: ");
scanf("%d", &num);
  
//declare array
int arr[num];
  
//get the array element
printf("Input 3 elements of the array:\n");
for(int i=0; i<num; i++)
{
printf("element [%d]: ", i);
scanf("%d", &arr[i]);
}
  
//display the frequency of elements of the array
printf("\nThe frequency of all elements of an array:\n");
for(int i=0; i<num; i++)
{
temp = arr[i];
count = 0;
flag = 0;
for(int j=0; j<i; j++)
{
if(temp==arr[j])
{
flag = 1;
break;
}
}
if(!flag)
{
for(int j=i; j<num; j++)
{
if(temp==arr[j])
count++;
}
printf("%d occurs %d times\n", temp, count);
}
}
  
  
return 0;
}

OUTPUT:

Enter the number of elements to be stored in the array: 3
Input 3 elements of the array:
element [0]: 25
element [1]: 12
element [2]: 43

The frequency of all elements of an array:
25 occurs 1 times
12 occurs 1 times
43 occurs 1 times



Related Solutions

C LANGUAGE ONLY Write a C program to count the total number of duplicate elements in...
C LANGUAGE ONLY Write a C program to count the total number of duplicate elements in an array. Enter the number of elements to be stored in the array: 3 Input 3 elements in the arrangement: element [0]: 5 element [1]: 1 element [2]: 1 Expected output: The total number of duplicate elements found in the array is: 1
write a program in c language to count the number of spaces ina sentence .the sentence...
write a program in c language to count the number of spaces ina sentence .the sentence is saved in the string 'sentence' write it in a repl.it and along with input,output and algorithm
ONLY IN C LANGUAGE Write a C program to print all the unique elements of an...
ONLY IN C LANGUAGE Write a C program to print all the unique elements of an array. Print all unique elements of an array Enter the number of elements to be stored in the array: 4 Input 4 elements in the arrangement: element [0]: 3 element [1]: 2 element [2]: 2 element [3]: 5 Expected output: The only items found in the array are: 3 5
LANGUAGE: C Only using <stdio.h> & <stdlib.h> Write a program that gives the user a menu...
LANGUAGE: C Only using <stdio.h> & <stdlib.h> Write a program that gives the user a menu to choose from – 1. Convert temperature input from the user in degrees Fahrenheit to degrees Celsius 2. Convert temperature input from the user in degrees Celsius to degrees Fahrenheit 3. Quit. Formulae you will need: C = (5 / 9) * (F-32) and F = (9/5) * C + 32 1. Use functions to accomplish 1 and 2 above. 2. Use at least...
in C language only. Write a program called gamecards.c that has a card game logic by...
in C language only. Write a program called gamecards.c that has a card game logic by comparing the cards from 4 people and calculating which player has the best card. 1. Every card must be represented by exactly two chars representing a rank and a suit. ranks: '2', '3', '4', '5', '6', '7', '8', '9', 'T', 'J', 'Q', 'K', 'A'. ('T' = 10) Suits: 'H', 'D', 'S', 'C'. 7D represents the “7 of Diamond” etc.. 2. Write a function called...
Write a C++ program to open and read a text file and count each unique token...
Write a C++ program to open and read a text file and count each unique token (word) by creating a new data type, struct, and by managing a vector of struct objects, passing the vector into and out of a function. Declare a struct TokenFreq that consists of two data members: (1) string value; and (2) int freq; Obviously, an object of this struct will be used to store a specific token and its frequency. For example, the following object...
IN C LANGUAGE This program will read in a series of strings and print only the...
IN C LANGUAGE This program will read in a series of strings and print only the consonants (including Y) until the word "stop" appears. No string will be longer than 100 characters. A consonant is any letter that is not a vowel. Don't forget to follow the standard read pattern! Examples Enter a string: Hello Hll Enter a string: World! Wrld Enter a string: 123! Enter a string: stop Enter a string: stop
In C++ For this assignment, you will write a program to count the number of times...
In C++ For this assignment, you will write a program to count the number of times the words in an input text file occur. The WordCount Structure Define a C++ struct called WordCount that contains the following data members: An array of 31 characters named word An integer named count Functions Write the following functions: int main(int argc, char* argv[]) This function should declare an array of 200 WordCount objects and an integer numWords to track the number of array...
C language <stdio.h> ONLY USE double and const int Write a program to convert Celsius temperature...
C language <stdio.h> ONLY USE double and const int Write a program to convert Celsius temperature to Fahrenheit temperature. The formula for converting Celsius temperature into Fahrenheit temperature is:    F = (9 / 5) * C + 32 Create integer constants for the 3 numbers in the formula (9, 5, and 32).  Follow the 3 steps in the Information Processing Cycle - Input, Processing, and Output. Convert the 9 to a double when converting the temperature (use type casting). Have a...
Answer ONLY in C language. Server program: The server program provides a search to check for...
Answer ONLY in C language. Server program: The server program provides a search to check for a specific value in an integer array. The client writes a struct containing 3 elements: an integer value to search for, the size of an integer array which is 10 or less, and an integer array to the server through a FIFO. The server reads the struct from the FIFO and checks the array for the search value. If the search value appears in...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT