If you are a student of CS then you are happy to know that you are in right place. Today we are sharing the c program to check frequency of an array elements
Check Frequency of an Array Elements:
Here we will discuss an article that how to check frequency of an array elements in C language. Here you know the use of arrays and the use of if statements. This program will tell you about the repetition of elements of an array.
Program:
C program to check frequency of an array elements.
#include<stdio.h> int main() { int a[10],i,size,data,count=0; printf("\nEnter the size of an array:"); scanf("%d",&size); //Here we input the size of an array printf("Enter the array elements:"); for(i=0;i<size;i++) //using for loop to input the elements of an array { scanf("%d",&a[i]); } printf("Enter the element which you want to know her frequency:"); //Here we enter the number which you want to check the frequency scanf("%d",&data); for(i=0;i<size;i++) //This for loop is used to check the frequency of elements of an array { if(data==a[i]) //This if statement is used to check repetition { count++; } } printf("frequency of %d number is:%d",data,count); }
Output:
In previous articles, we also discussed the Easy C Program to Insert a Number in an Array. But we in this article we discussed the frequency of elements of an array. First of all we declare integer type of array and others using variables. such as a[10], i, size, count, and data.
In the program, we use a[10]. This is an integer type of array and it is used to store integer values in it. And size integer variable is used to input the size of the array that how many elements did you enter in an array.
After that “i” variable is used to run the loop and and it runs on the size of an array and data variable is used to store the value which is used to check the frequency of that element. its all possible when entering the values using loops through scanf function.
After all this another loop was used to check the frequency of elements this loop runs on size that we enter previously and in this loop we use if statement to check conditon and match the elements of array if double then match and enter in if statement and if false then loop terminates.
In if statement count variable is used to save the repetition of element and firstly we intilize the count variable with zero.
After closing the body of if and loop the printf function is used to show the frequency of the letter that was stored in the count variable.
Conclusion:
All the above articles show you a way that how do we check the frequency of elements of the array. This article tells you using c language how do you check the repetition of the elements.