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

C++ 8. Mode Function In statistics, the mode of a set of values is the value tha

ID: 3768466 • Letter: C

Question

C++

8. Mode Function

In statistics, the mode of a set of values is the value that occurs most often or with the greatest frequency. Write a function that accepts as arguments the following:

A) An array of integers

B) An interger that indicates the number of elements in the array

The function should determine the mode of the array. That is, it should determine which value in the array occurs most often. The mode is the value the function should return. If the array has no mode( none of the values occur more than once), the function should return -1. (Assume the array will always contain nonnegative values.) Demonstrate your pointer prowess by using pointer notation instead of array notation in this function.

Explanation / Answer

#include<iostream>

#include<map>

using namespace std;

int mode_(int *arr,int size)

{

map<int,int> count;

  

for(int i=0;i<size;i++)

count[*(arr+i)]++;

  

int mode = -1;

int max = 1;

map<int,int>::iterator it;

  

for(it = count.begin();it!=count.end();it++)

{

if(it->second>max)

{

max = it->second;

mode = it->first;

}

}

  

return mode;

}

int main()

{

int arr[10] = {11,1,2,34,5,11,2,3,34,34};

int mode = mode_(arr,10);

  

cout << "Mode is " << mode;

return 0;

}