In: Computer Science
write a function that accept an array and its size than return an array call modeAry that store the following information: modeAry[0] = Number of modes; modeAry[1] = Frequency of the modes ; modeAry[>=2] = All the modes you found ; EXP:if the array input is [1,1,2,2,4,3,3], the function modAry should be [3,2,1,2,3]. In c++
modeAry.cpp
#include<iostream>
#include<cstdlib>
using namespace std;
int *modeAry(int arr[],int size)
{
    int
max=INT_MIN,i,freqOfModes,noOfModes,modesInd;
    int *modeArr = new int[size]; //dynamic mode
array
    //find maximum element in array
    // maximum lement is required to define size of
count array
    for(i=0;i<size;i++)
    {
        if(arr[i] > max)
        max=arr[i];
   }
   int count[max]; //Array to count frequency of each
element
  
   //initialize frequency of each element to 0
   for(i=0;i<size;i++)
   count[i] = 0;
  
   //find frequency of each element
   for(i=0;i<size;i++)
   count[arr[i]]++;
  
   //modes of an array are most frequent numbers
   //find modes of an array
   freqOfModes = 0;
   for(i=0;i<max;i++)
   {
       if (count[i] >
freqOfModes)
       freqOfModes = count[i];
      
   }
  
   // find no of modes
   noOfModes = 0 ;
   modesInd = 2;
   for(i=0;i<max;i++)
   {
       if (count[i] > 0 &&
count[i] == freqOfModes)
       {
       noOfModes++;
       modeArr[modesInd] = i;
       modesInd++;
   }
   }
  
   modeArr[0] = noOfModes;
   modeArr[1] = freqOfModes;
     
   return modeArr;
     
}
int main()
{
   int n,i,modeArrSize;
   int arr[100]; //array of size 100 to store
numebrs;
   cout<<"Enter size of Array : ";
   cin>>n;
   cout<<"\nEnter " <<n<<" elements of
array : ";
   for(i=0;i<n;i++)
   cin>>arr[i];
  
   int *modeArr = modeAry(arr,n); //modeArr is a mode
Array return by modeAry function;
  
   modeArrSize = modeArr[0] + 2; //all modes and
modeArr[0] , modeArr[1] i.e +2
  
   //print mode array
   cout<<"\nMode Array : [ ";
   for(i=0;i<modeArrSize;i++)
   cout<<modeArr[i]<<" ";
   cout <<"]";
}
OUTPUT
