Question: Create a function l_mode that finds the index of the value that occurs most often in an array. The input to your function is an
Create a function l_mode that finds the index of the value that occurs most often in an array. The input to your function is an array of integers A and the number of elements in the array 0 < n <= 10^3. Assuming v is the most frequent element in the array, your function will return the index i of the first (leftmost) occurrence of v in A. You can assume that -500 <= A[i] <= 500, for 0 <= i < n. Ties must be broken by choosing the smallest element.
this is the function name and parameters:
unsigned int l_mode(const int *A, unsigned int n);
It must be written in C++ and can only include
#include
I only need the function but if you'd like to write a main to test the function out you can use some of the following test cases:
- array of all 0's
- smallest element is chosen if 2 numbers have equal occurrences
- be sure to return the index not the element
- an array where every element is equal (ex. {5, 5, 5, 5, 5})
- array is 2 elements
-
{1, 3, -500, -500, 0, 0, 0, -324, 500, 500, 500, 500} = 8
- {50, -500, -500, -500, 0, 1, 0, 3, -500, -500, 500, -500} = 1
- {5, 5, 5, 4, 4, 4, 3, 3, 3, 3, 3, 3, 3} = 6
- {1, 1, 1, 1 ,5, 5, 5, 5} = 0
- {5, 5, 5, 5, 1, 1, 1, 1} = 4
- {-5, -1, -5, -1, -5, -1} = 0 (-5 < -1)
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
