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
In the language c using the isspace() function: Write a program to count the number of...
In the language c using the isspace() function: Write a program to count the number of words, lines, and characters when user enter statements as input. A word is any sequence of non-white-space characters. Have the program continue until end-of-file. Make sure that your program works for the case of several white space characters in a row. The character count should also include white space characters. Example of the user input could be the statement below:              You're traveling through ​              ...
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
>>>>>C#<<<<< A.     Number Frequency-Arrays Write a program to count the number of 10’s in the given...
>>>>>C#<<<<< A.     Number Frequency-Arrays Write a program to count the number of 10’s in the given list of 15 numbers. The output should look like:                                                                                                        [01] Extend your program to work with a list of n numbers.                                         [01]                                                                                              Random Numbers-Methods Write a program to generate a 10 Random Numbers:                                           [01] Extend your program to generate n Random numbers.                                         [01] B.    Word Count -String Processing Write a program to count the occurrence...
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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT