Question

Create a function l_mode that finds the index of the value that occurs most often in...

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 <iostream>
#include <cmath>

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)
0 0
Add a comment Improve this question Transcribed image text
Answer #1
#include <iostream>
#include <climits>
using namespace std;

unsigned int l_mode(const int *A, unsigned int n){
   int value = INT_MAX , count = 0, c, index;
   for(int i = 0;i<n;i++){
      c = 0;
      for(int j = 0;j<n;j++){
         if(A[i] == A[j]){
            c++;
         }
      }
      if(count <= c && A[i]<value){
         value = A[i];
         count = c;
      }
   }  
   for(int i = 0;i<n;i++){
      if(value == A[i]){
         index = i;
         break;
      }
   }
   return index;
}

int main() {
   int A1[] = {1, 3, -500, -500, 0, 0, 0, -324, 500, 500, 500, 500};
   int A2[] = {50, -500, -500, -500, 0, 1, 0, 3, -500, -500, 500, -500};
   int A3[] = {5, 5, 5, 4, 4, 4, 3, 3, 3, 3, 3, 3, 3} ;
   int A4[] = {1, 1, 1, 1 ,5, 5, 5, 5};
   int A5[] = {5, 5, 5, 5, 1, 1, 1, 1} ;
   int A6[] = {-5, -1, -5, -1, -5, -1};
   
   cout<<l_mode(A1,12)<<endl;
   cout<<l_mode(A2,12)<<endl;
   cout<<l_mode(A3,13)<<endl;
   cout<<l_mode(A4,8)<<endl;
   cout<<l_mode(A5,8)<<endl;
   cout<<l_mode(A6,6)<<endl;
   
   return 0;
}

Output:

6 4 Process exited after 0.1834 seconds with returnvalue 0 Press any key to continue . - .

Note: Please comment below if you have any doubts I will update my answer if needs

Please up vote the solution if it helped. Thanks!

Add a comment
Know the answer?
Add Answer to:
Create a function l_mode that finds the index of the value that occurs most often in...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT