C++找不到多个模式

C++ unable to find multiple modes

本文关键字:模式 找不到 C++      更新时间:2023-10-16

我的任务要求我编写一个函数,该函数以数组和该数组的大小为参数,并查找模式。如果有多种模式,我要找到它们,并将它们放在一个向量中,然后按升序打印所述向量。

例如,如果我输入以下整数:

3, 4, 2, 1, 2, 3

然后输出应显示

2, 3

如果我输入以下整数:

1, 2, 3, 4

然后输出应显示:

1, 2, 3, 4.

然而,我的程序不知何故只找到了第一种模式,并以一种非常尴尬的方式显示它。

这是我的输入:

3, 4, 2, 3, 2, 1

这就是输出:

3
3
3
3
3

这是我的密码。如有任何帮助,我们将不胜感激。感谢大家抽出时间!

#include <iostream>
#include <iomanip>
#include <algorithm>
#include <vector>
using namespace std;
int main() {
   int size;                              //array size
   int* array;                            //array of ints
   int arraycount;                        //counter for array loop
   void findMode(int array[], int size);  //function prototype
//intialize array
   cout << "Enter number of integers ";
   cout << "you wish to input." << endl;
   cin >> size;
   cout << "Enter the integers." << endl;
   array = new int[size];
   for (arraycount = 0; arraycount < size;
   arraycount++)
      cin >> array[arraycount];
//call function
   findMode(array, size);
   return 0;
}
void findMode(int array[], int size) {
   int counter = 1;
   int max = 0;
   int mode = array[0];
   int count;
   vector <int> results;
//find modes
   for(int pass = 0; pass < size - 1; pass++) {
      if(array[pass] == array[pass+1]) {
         counter++;
         if(counter > max) {
            max = counter;
            mode = array[pass];
         }
      }
      else {
         counter = 1;
      }
   }
//push results to vector
   for (count=0; count < size - 1; count++) {
      if(counter == max) {
      std::cin >> mode;
      results.push_back(mode);
      }
   }
//sort vector and print
   std::sort(results.begin(), results.end());
   for (count=0; count < size - 1; count++) {
         cout << mode << endl;
   }
}
#include <iostream>
#include <iomanip>
#include <algorithm>
#include <vector>
#include <map>
using namespace std;
int main() {
    int size;                              //array size
    int* array;                            //array of ints
    std::vector<int> vecInput;
    int arraycount;                        //counter for array loop
    void findMode(std::vector<int> vec);  //function prototype
    //intialize array
    cout << "Enter number of integers ";
    cout << "you wish to input." << endl;
    cin >> size;
    cout << "Enter the integers." << endl;
    array = new int[size];
    for (arraycount = 0; arraycount < size; arraycount++) 
    {
            int num;
            cin >> num;
            vecInput.push_back(num);
    }

    //call function
    findMode(vecInput);
    return 0;
}
void findMode(std::vector<int> vec) 
{
    std::sort(vec.begin(), vec.end());
    std::map<int, int> modMap;
    std::vector<int>::iterator iter = vec.begin();
    std::vector<int> results;
    int prev = *iter;
    int maxCount = 1;
    modMap.insert(std::pair <int,int>(*iter, 1));
    iter++;
    for (; iter!= vec.end(); iter++)
    {
        if (prev == *iter)
        {
            std::map<int, int>::iterator mapiter = modMap.find(*iter);
            if ( mapiter == modMap.end())
            {
                modMap.insert(std::pair <int,int>(*iter, 1));
            }
            else
            {
                mapiter->second++;
                if (mapiter->second > maxCount)
                {
                    maxCount = mapiter->second;
                }
            }
        }
        else
        {
            modMap.insert(std::pair <int,int>(*iter, 1));
        }
        prev = *iter;
    }
    std::map<int, int>::iterator mapIter = modMap.begin();
    for (; mapIter != modMap.end(); mapIter++)
    {
        if (mapIter->second == maxCount)
        {
            results.push_back(mapIter->first);
        }
    }
    cout << "mod values are " <<endl;
    std::vector<int>::iterator vecIter = results.begin();
    for (; vecIter != results.end(); vecIter++)
        cout<<*vecIter<<endl;

}

感谢大家的帮助,我能够让代码正常工作。原来问题出在函数的向量部分。这是我修改后的代码:

#include <iostream>
#include <iomanip>
#include <algorithm>
#include <stdlib.h>
#include <vector>
using namespace std;
#define N 100
void findMode(int x[], int size);       //function prototype
vector<int> results;    //vector
int main(void) {
   int* x;
   int size=0;
   int arraycount;
//intialize array
   cout << "Enter number of integers ";
   cout << "you wish to input."  << endl;
   cin >> size;
   cout << "Enter the integers." << endl;
   x = new int[size];
   for (arraycount = 0; arraycount < size;
   arraycount++)
      cin >> x[arraycount];
//send array and size to function
   findMode(x, size);
   return 0;
}
//findMode function    
void findMode(int x[], int size) {
   int y[N]={0};
   int i, j, k, m, cnt, count, max=0;
   int mode_cnt=0;
   int num;
   int v;
   vector<int> results;
   vector<int>::iterator pos;
//loop to count an array from left to right
   for(k=0; k<size; k++) {
      cnt=0;
      num=x[k]; //num will equal the value of x[k]
      for(i=k; i<size; i++) {   
         if(num==x[i])
            cnt++;              
      }
      y[k]=cnt; //
   }
//find highest number in array
   for(j=0; j<size; j++) {
      if(y[j]>max)
         max=y[j];
   }
//find how many modes there are
   for(m=0; m<size; m++) {
      if(max==y[m])
         mode_cnt++;
   }
//push results to vector
   for (m=0; m < size; m++) {
      if(max == y[m]) {
//after taking out this line the code works properly
//      std::cin >> x[m];      
       results.push_back(x[m]);
      }
   }
//sort vector and print
   std::sort(results.begin(), results.end());
   cout << "The mode(s) is/are: ";
   for (pos=results.begin(); pos!=results.end(); ++pos) {
         cout << *pos << " ";
   }
}