Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

The mode is the value that appears most often in a set of data. Write a function

ID: 3794639 • Letter: T

Question

The mode is the value that appears most often in a set of data. Write a function named findMode that takes as parameters an array of int and the size of the array, and returns a vector containing the mode(s). If there is just a single most frequent value, the vector will only contain that one value, but if multiple values tie for maximum frequency, the vector will need to contain all such values. This includes the case where every number in the array appears only once. Each mode should appear only once in the vector. The values in the vector that is returned must be in ascending order. If you need to sort a vector, it's similar to sorting an array, but specifying the beginning and end of the vector look a little bit different. If your vector is named result, then it would look like this: "std::sort(result.begin(), result.end());". The most straightforward approach is to: Iterate (loop) through the array to find out what the highest frequency for any value is without worrying about storing any modes. Iterate through the array again, this time comparing the counts for each value to the highest frequency that you already found, if the count for a value is the same as the highest frequency, push that value into your results vector. The file must be named: findMode.cpp

Explanation / Answer

#include<bits/stdc++.h>
using namespace std;
vector<int> findMode(int a[],int n)
{ int mod,modValue;
mod=a[0];

int i,count=0,max=0;
    vector<int> result;

for(i=0;i<n-1;i++)
{
    if(a[i]==a[i++])
    {
    count++;
    }
   else if(max<count)
    {
        max=count;
        mod=a[i-1];
        count=1;
              
       }
   }
   if(max<count)
    {
        max=count;
        mod=a[i-1];
        count=1;
              
       }

for(i=0;i<n;i++)
   {
      modValue=a[i]%mod;
      result.push_back(modValue);
   }
    sort(result.begin(),result.end());
    return result;     
}
int main()
{
   int size;
   cout<<"Enter the size of array"<<endl;
   cin>>size;
   int arr[size];
   int i;
   for(i=0;i<size;i++)
   cin>>arr[i];
   sort(arr,arr+size); //sort the array
vector<int> results;
           results= findMode(arr,size);
           cout<<"Print all mod values";
for(int i=0;i<results.size();i++)
   cout<<results[i]<<" ";  
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote