Thursday 26 June 2014

C++ programs based on frequency of elements in an array

Here are C++ programs based on frequency of elements in an array. First program print frequency of all elements in array, second program sort array elements based on their frequency, third program print least and most frequent elements and fourth program finds the frequency of the given number in an array 


C++ program to print the frequency of elements in an array
#include<iostream>
using namespace std;
int main()
{
    int a[20][2],num,freq=1;
    cout<<"How many elements to be stored (max 20) : ";
    cin>>num;
    cout<<"Enter elements of array : ";
    for(int i=0;i<num;i++)
    {
        cin>>a[i][0];
        a[i][1]=0;
    }
    for(int i=0;i<num;i++)
    {
        if(a[i][1]==-1)
            continue;
        for(int y=i+1;y<num;y++)
          if(a[y][0]==a[i][0])
          {
              freq++;
              a[y][1]=-1;
          }
        a[i][1]=freq;
        freq=1;
    }
    cout<<"Elements  :  Frequency "<<endl;
    for(int i=0;i<num;i++)
        if(a[i][1]!=-1)
           cout<<"  "<<a[i][0]<<"      :  "<<a[i][1]<<endl;
    return 0;
}

OUTPUT




C++ program to sort array element based on their frequency
#include<iostream>
using namespace std;
int main()
{
    int a[20][2],num,freq=1,fre[20][2],k=0,temp;
    cout<<"How many elements to be stored (max 20) : ";
    cin>>num;
    cout<<"Enter elements of array : ";
    for(int i=0;i<num;i++)
    {
        cin>>a[i][0];
        a[i][1]=0;
    }
    for(int i=0;i<num;i++)
    {
        if(a[i][1]==-1)
            continue;
        for(int y=i+1;y<num;y++)
          if(a[y][0]==a[i][0])
          {
              freq++;
              a[y][1]=-1;
          }
        fre[k][1]=freq;
        fre[k][0]=a[i][0];
        k++,freq=1;
    }
    for(int i=0;i<k-1;i++)
    {
        for(int y=0;y<k-1;y++)
        {
            if(fre[y][1]>fre[y+1][1])
            {
                temp=fre[y][0];
                fre[y][0]=fre[y+1][0];
                fre[y+1][0]=temp;
                freq=fre[y][1];
                fre[y][1]=fre[y+1][1];
                fre[y+1][1]=freq;
            }
        }
    }
    cout<<"Elements  :  Frequency "<<endl;
    for(int i=0;i<k;i++)
        cout<<"  "<<fre[i][0]<<"      :  "<<fre[i][1]<<endl;
    return 0;
}

OUTPUT




C++ program to print least and most frequent elements
#include<iostream>
#include<stdlib.h>
using namespace std;
int main()
{
    int a[20][2],smchtr[20],lrchtr[20];
    int smfre=30,lrfre=0,num,n=1,k=0,l=0;
    cout<<"How many elements to be stored (max 20) : ";
    cin>>num;
    cout<<"Enter elements of array : ";
    for(int i=0;i<num;i++)
    {
        cin>>a[i][0];
        a[i][1]=0;
    }
    for(int i=0;i<num;i++)
    {
        if(a[i][1]==-1)
            continue;
        for(int y=i+1;y<num;y++)
            if(a[i][0]==a[y][0])
            {
               a[y][1]=-1;
                n++;
            }
        if(smfre>n)
        {
           smchtr[0]=a[i][0];
           smfre=n;
           k=0;
        }
        else if(smfre==n)
            smchtr[++k]=a[i][0];
        if(lrfre<n)
        {
           lrchtr[0]=a[i][0];
           lrfre=n;
           l=0;
        }
        else if(lrfre==n)
            lrchtr[++l]=a[i][0];
        n=1;
    }
    cout<<"Most frequent elements with frequency "<<lrfre<<" : ";
    for(int i=0;i<=l;i++)
       cout<<lrchtr[i]<<" ";
    cout<<"\nLeast frequent elements with frequency "<<smfre<<" : ";
    for(int i=0;i<=k;i++)
       cout<<smchtr[i]<<" ";
    return 0;
}

OUTPUT




C++ program to find the frequency of the given number in an array
#include<iostream>
using namespace std;
int main()
{
    int a[20],num,freq=0,element;
    cout<<"How many elements to be stored (max 20) : ";
    cin>>num;
    cout<<"Enter elements of array : ";
    for(int i=0;i<num;i++)
        cin>>a[i];
    cout<<"Enter the element whose frequency to be found : ";
    cin>>element;
    for(int i=0;i<num;i++)
        if(element==a[i])
            freq++;
    cout<<"Number of times "<<element<<" appears : "<<freq;
    return 0;
}

OUTPUT

No comments:

Post a Comment