C ++计算排序数组的模式

时间:2013-11-12 03:52:23

标签: c++ arrays mode sorted

我必须编写一个C ++代码来查找数组的中位数和模式。我被告知在数字排序后找到阵列模式要容易得多。我对功能进行了排序,但仍无法找到该模式。

 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; 

16 个答案:

答案 0 :(得分:6)

如果阵列已经排序,您可以一次计算一个数字的出现次数。然后只保存出现次数最多的数字。你只能在一个for循环中找到模式。 否则,您将不得不做多个for循环。 请参阅以下链接中的详细示例 Find-the-Mode-of-a-Set-of-Numbers

这是代码,

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;

答案 1 :(得分:2)

一种方法是可以使用运行长度编码。在运行长度编码中,表示将是; (项目,其频率)。

这样做时,请跟踪最大频率和项目。完成“运行长度”后,这将为您提供模式。

例如:

 1 1  2 2 2 3 3 4 5

运行长度编码将是

 {1, 2}, {2, 3}, {3, 2}, {4, 1}, {5, 1}

需要O(n)空间。

答案 2 :(得分:2)

以下是代码段:

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++;
    }
    else
    {
        if (count > countMode) 
        {
            countMode = count;
            mode = number;
        }
        count = 1;
        number = array[i];
    }
}

cout << "mode : " << mode << endl;

答案 3 :(得分:1)

这就是我的方法,我的解决方案将采用排序向量作为输入。它具有O(n)时间复杂度,可以处理向量中有多于1个“模式”数的情况。

void findMode(vector<double> data) {

double biggestMode = 1;
vector<double> mode, numbers;
numbers.push_back(data.at(0));
mode.push_back(1);
int count = 0;
for (int i = 1; i < data.size(); i++) {
    if (data.at(i) == numbers.at(count)) {
        mode.at(count)++;
    }
    else {
        if (biggestMode < mode.at(count)) {
            biggestMode = mode.at(count);
        }
        count++;
        mode.push_back(1);
        numbers.push_back(data.at(i));
    }
}

for (int i = 0; i < mode.size(); i++) {
    if (mode.at(i) == biggestMode)
        cout << numbers.at(i) << " ";
}
cout << endl;

}

答案 4 :(得分:0)

“模式”是最常出现的值。如果没有重复编号,则列表没有模式。 因此,如果您需要了解“模式”,那么排序将没有任何好处。

你确定你没有提到中位数吗?中位数是集合中的中间数。 如果你有1,2,3,4,5中位数(中间数字)是(total_number)/ 2)如果是奇数则向上舍入,2.5 - > 3,我们的中位数为3.如果您的数字已经排序,您只能真正计算中位数。 如果你在1,2,3,4,5,6中有一个偶数 你的模式是插槽3,4(巧合的是3,4) (total_number)/ 2 slot和(total_number)/ 2 + 1 slot,适用于任何偶数数组。

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

答案 5 :(得分:0)

此代码应该为您提供模式。如果两个不同数字的数量相等,则输出第一个数字。

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;

答案 6 :(得分:0)

此代码在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;
}

答案 7 :(得分:0)

我是这样做的:

    int main()
{ 
    int mode,modecount2,modecount1;
    bool is_nomode=false;
    vector<int> numbers = { 15,43,25,25,25,25,16,14,93,93,58,14,55,55,55,64,14,43,14,25,15,56,78,13,15,29,14,14,16 };
    sort(numbers);

    //If you uncomment the following part, you can see the sorted list of above numbers
    //for (int i = 0; i < numbers.size(); ++i) std::cout << numbers[i] << '\n';
    //keep_window_open();

    mode = numbers[0];
    modecount1 = 0;
    modecount2 = 1; //Obviously any number exists at least once!
    for (int i = 1; i < numbers.size(); ++i) {
        if(numbers[i]==numbers[i-1]) ++modecount2;
        else {
            if (modecount2 > modecount1) {
                mode = numbers[i - 1];
                modecount1 = modecount2;
            }
            else if (i != 1 && modecount2 == modecount1) { std::cout << "No mode!\n"; is_nomode = true; break; }
            modecount2 = 1;
        }
    }
    if(!is_nomode) std::cout << "Mode of these numbers is: " << mode << std::endl;
    keep_window_open();

此外,您可以在数字列表中添加另外25个,看看如果两个数字相同,会发生什么! 我希望它有所帮助。

答案 8 :(得分:0)

此代码使用“map”从给定数组中找出MODE。 它假设数组已经排序。

int findMode(int * arr, int arraySize)
{
    map<int, int> modeMap;
    for (int i = 0; i < arraySize; ++i) {
        ++modeMap[arr[i]];
    }

    auto x = std::max_element(modeMap.begin(), modeMap.end(),
        [](const pair<int, int>& a, const pair<int, int>& b) {
        return a.second < b.second; });

    return x->first;
}

答案 9 :(得分:0)

这很有用。

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;

答案 10 :(得分:0)

有一句古老的谚语说:“如果在一个房间里放10个程序员,并给他们相同的程序进行编码,您将得到12个不同的结果”,因此,我的回答是我的版本。速度可能不那么快(我打算测试速度与其他一些建议),但我觉得它很容易理解。

#include <iostream>

using namespace std;

int main ()
{
    short z[10];
    short maxCount = 0, curCount = 0, cur = 0, most = 0;

    for (int i = 0; i < 10; i++)
        {
         cout << "Enter a number: " << endl;
         cin >> z[i];
        }

    for (int i = 0; i < 10; i++)
        {
         cur = z[i];
            for (int a = i; a < 10; a++)
                {
                 if (cur == z[a])
                    {
                     curCount++;
                     cur = z[a];
                    }
                if (curCount > maxCount)
                   {
                    maxCount = curCount;
                    most = z[a];
                   }
            }
            curCount = 0;
        }

    cout << "the mode is : " << maxCount << ", the number is: " << most << endl;
}

答案 11 :(得分:0)

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

       count = 1; // reset count for the new number
       number = array[i];
  }
  if (count > countMode) {
              countMode = count;
              mode = number;
  }
}

cout << "mode : " << mode << endl;

答案 12 :(得分:0)

虽然Diedrei的答案很接近,但有些人指出了一些缺点,例如,如果模式是由排序数组的最后一个数字定义的(1,2,3,3,4,4,4将返回3作为模式)。另外,根据对如何处理多种模式的要求,将有不同的解决方案。

此解决方案可以完成几件事:

  1. 解决模式在数组末尾的问题
  2. 如果存在多种模式(超过1个数字且出现次数相同且计数> 1),则返回最小的数字作为模式
  3. 如果没有模式,则返回-1(每个数字仅出现一次)
int number = array[0];
int mode = number;
int count = 1;
int countMode = 1;

for (int i=1; i<size; i++)
{
      if (array[i] == number) 
      { // increment the count of occurrences for the current number
         ++count;
         if (count > countMode) 
         {
               countMode = count; // this number now has the most occurrences 
               mode = number; // this number is now the mode
         }
      }
      else
      { // now this is a different number
           count = 1; // reset count for the new number
           number = array[i]; // set the new number
  }
}
if (countMode == 1) {
  mode = -1; // set the mode to -1 if each number in the array occur only once
}

cout << "mode : " << mode << endl;

答案 13 :(得分:0)

int findModa(int *arr, int n) {
    int count=1;
    int countmax=0;
    int current = arr[0];
    int moda = 0;
    for (int i=1; i<n; i++) {
        if(arr[i] == curr) {
            count++;
        }
        else if (count>countmax) {
            countmax=count;
            count=1;
            moda=arr[i-1];
            current=arr[i];
        }
        current=arr[i];
    }
    return moda;
}

答案 14 :(得分:0)

我知道这个问题很老了,但这里有一个干净而简短的代码来计算统计模式:

std::sort(vector.begin(), vector.end());
int mode = vector[0], count = 0, countMode = 1;
for (int i = 1; i < vector.size(); ++i)
{
    if (vector[i] == mode) ++countMode;
    else ++count;
    if (count > countMode)
    {
        mode = vector[i];
        countMode = count;
        count = 0;
    }
}

答案 15 :(得分:0)

这是我为排序向量编写的代码

void print_mode(vector<int>& input)
{

int mode=0, count = 0;
int current_number = input[0];
int mode_number = current_number;

for (int i=0; i < input.size(); i++)
{
    if (current_number == input[i])//check if the number is the same
    {
        count++;
    }

    else //this fuction works when the value are no longer the same and 
         //this is when it updates the mode value
        {

        if (count > mode)//update mode value
        {
            mode = count;
            mode_number = current_number;
            
        }

        count = 1;// it is not reset back to zero because when it the program detect a 
                  //different number it doesn't count it so this is to solve that issue

    }
    if (i == input.size() - 1)// this function before it doesn't work when the largest value 
                               //is mode so I added this if state to solve it
    {

        if (count > mode)
        {
            mode = count;
            mode_number = current_number;
        }

    }

    current_number = input[i];//prepare for next value
}
cout << mode_number << " is the mode number and it is repeated " << mode << " times" << endl;
}