C++ Calculating the Mode of a Sorted Array

2019-03-30 02:21发布

I have to write a C++ code that finds the median and mode of an array. I'm told that it's much easier to find the mode of an array AFTER the numbers have been sorted. I sorted the function but still cannot find the mode.

 int counter = 0;
    for (int pass = 0; pass < size - 1; pass++)
        for (int count = pass + 1; count < size; count++) {
            if (array [count] == array [pass])
                counter++;
            cout << "The mode is: " << counter << endl; 

11条回答
forever°为你锁心
2楼-- · 2019-03-30 03:18

The "mode" is the value that occurs most often. If no number is repeated, then there is no mode for the list. So there would be no benefit to sorting if you needed to know the "mode".

Are you sure you are not referring to the median? The median is the middle number in a set. If you have 1,2,3,4,5 the Median (middle number) is the (total_number)/2) rounded up if it is odd, 2.5 -> 3 and our median would be 3. you can only really calculate the median if your numbers are sorted. If you have an even number in a set 1,2,3,4,5,6 your mode is slots 3,4 (coincidentally also, 3,4) (total_number)/2 slot and (total_number)/2 + 1 slot, for any even array of numbers.

http://www.purplemath.com/modules/meanmode.htm

查看更多
smile是对你的礼貌
3楼-- · 2019-03-30 03:18

This code should give you the mode. If there are equal number of two different numbers, it will output the first of such.

int count = 1, mode = 0, m = 0, i = 1;
size_t sz = sizeof(array)/sizeof(*array);
while(i != sz+1) {
    if(array[i-1] != array[i]) {
        if(count > m) {
            mode = array[i-1];
            m = count;
            count = 1;
        }
    }
    else
        ++count;
    ++i;
}
std::cout << "mode: " << mode << std::endl;
查看更多
看我几分像从前
4楼-- · 2019-03-30 03:19

If the array has been sorted already, you can count the occurrences of a number at once. Then just save the number that has biggest occurrences. And you can find out the mode in only one for-loop. Otherwise, you'll have to do more than one for-loops. See a details example at the link below Find-the-Mode-of-a-Set-of-Numbers

Here is the code,

int number = array[0];
int mode = number;
int count = 1;
int countMode = 1;

for (int i=1; i<size; i++)
{
      if (array[i] == number) 
      { // count occurrences of the current number
         ++count;
      }
      else
      { // now this is a different number
            if (count > countMode) 
            {
                  countMode = count; // mode is the biggest ocurrences
                  mode = number;
            }
           count = 1; // reset count for the new number
           number = array[i];
  }
}

cout << "mode : " << mode << endl;
查看更多
Summer. ? 凉城
5楼-- · 2019-03-30 03:20

This had worked.

int vals[9];                
sort(vals, vals + 9);
int key = vals[0], value = 1,max_key=0,max_value=0;

for (int l = 1; l < 9; l++){
    if (key == vals[l]){
        value++;
    }
    else{
        if (value>max_value){
            max_key = vals[l-1];
            max_value = value;
        }
        key = vals[l];
        value = 1;
    }
}
cout<< "Mode: "<< max_key << endl;
查看更多
男人必须洒脱
6楼-- · 2019-03-30 03:23

This code finds the mode in C++:

#include <iostream>
using namespace std;

int main(int argc, char** argv)
{
    int i,j,k=0,n,repeat_max=0,cn=0;
    int array1[50],mode[50],count[50]={0},c[50];

    cout<<"\n inter count:\t";
    cin>>n; 


    cout<<"\n";

    for(i=0;i<n;i++)
    cin>>array1[i];

    for(i=0;i<n;i++)
    {
        for(j=0;j<n;j++)
        {

            if(array1[i]==array1[j])
            {   
                count[i]++;
                if(count[i]>=repeat_max)
                {
                    repeat_max=count[i];
                    mode[k++]=array1[i];        
                }
            }
        }
    }
    cout<<"\n================\n";
    for(i=1;i<k;i++)
    cout<<"\t mode[i]="<<mode[i]<<"\n";
    cout<<"\t\n\nrepeat array:"<<repeat_max;

    return 0;
}
查看更多
登录 后发表回答