C Program to count the frequency of the element in the array



#include<stdio.h>
void read(int a[50],int n);
void display(int a[50],int n);
int search(int a[50],int n,int x);
int main()
{
    int n,a[50],element,freq;
    printf("Enter the size of the array\n");
    scanf("%d",&n);
    printf("Enter the array elements \n");
    read(a,n);
    printf("The array elements are\n");
    display(a,n);
    printf("\nEnter the number to be searched\n");
    scanf("%d",&element);
    freq=search(a,n,element);
    printf("The frequency of %d is : %d",element,freq);
}

void read(int a[50],int n)
{
    int i;
    for(i=0;i<n;i++)
    {
        scanf("%d",&a[i]);
    }
}

void display(int a[50],int n)
{
    int i;
    for(i=0;i<n;i++)
    {
        printf("%d\t",a[i]);
    }
}

int search(int a[50],int n,int x)
{
    int i,flag=0;
    for(i=0;i<n;i++)
    {
       if(a[i]==x)
       {
           flag++;

       }

    }
    return flag;
}

Output:

C Program to count the frequency of the element in the array



Comments