2017-02-13 6 views
0

基本的には、ベクトルが1 1 1であればモードを返さないなど、ベクトル内のモードを探しています。また、2つ以上のモードについて心配しないで、最高2つの可能なモードを読むことができる必要があります。どんな提案も非常に役に立ちます。 また、void calcModeはその一部です。コードに追加することはできません。変更方法はわかりません。複数のモードをベクトルに読み込み、それを印刷しようとしています。C++

void calcMode(vector <double> const& vec) 
{ 
    int counter = 1; 
    int max = 0; 
    vector<double> mode; 
    for (int pass = 0; pass < vec.size() - 1; pass++) 
     { 
      if (vec[pass] == vec[pass + 1]) 
      { 
       counter++; 
       // If they are the same number add it to the mode vector 
       if (counter > max) 
       { 
        mode.clear(); 
        max = counter; 
        mode.push_back(vec[pass]); 
       } 
       // if it is greater clear the vector and assign it the new value 
       else if (counter == max) 
       { 
        mode.push_back(vec[pass]); 
       } 

      } 
      else 
       counter = 1; // reset counter. 
     } 
    // print out the freq and the mode(s) 
    cout << mode.size() << endl; 
    cout << "Mode: " << setw(25) << setprecision(3); 

    cout << setw(25) << setprecision(3); 

    if (vec.size() == 1) 
    { 
     cout << "Freq = " << counter << endl; 
     cout << vec[0] << endl; 
    } 
    else if (vec.size() == 2) 
    { 
     cout << "Freq = " << counter << endl; 
     cout << vec[0] << vec[1] << endl; 
    } 
    else 
     cout << "No mode" << endl; 
} 
+0

クイックハック: 'のstd ::マップ FREQ;'あなたは 'FREQ [VEC [インデックス]]は++ができ、この小さな美しさ;'周波数を取得します'ベクトル '中の全ての要素の数。次に、最も高い(または2つの最も高い)カウントを探して 'freq'を歩いてください。関数は5-6行のコードで実行されます。 – user4581301

+0

それ以外の場合は、1つの問題を選択し、その問題に焦点を当てます。さもなければ、あなたは票を集め、近い票を集め、断片化された回答を集めるつもりです。それのどれもあなたの時間の価値がありません。 – user4581301

+0

返信いただきありがとうございます、あなたのメソッドを試しましたが、最高の値を数えますが、どのように表示するのですか? 20 20 10 11のようにfreqは2になりますが、20をどのように出力するのですか? –

答えて

0

可能な限り標準ライブラリを使用し、意味があるところで使用します。

std::max_element documentation

#include <iostream> 
#include <vector> 
#include <map> 
#include <algorithm> 

// comparator function. When you compare two std::pair, you get both sides 
// in the compare. We only want the value side, so we define a function to 
// only test the value side. 
static bool comp(std::pair<unsigned,unsigned> lhs, 
       std::pair<unsigned,unsigned> rhs) 
{ 
    return (lhs.second < rhs.second); 
} 

int main() 
{ 
    std::vector <double> const& vec {0, 1, 1, 2, 2, 4, 3, 2, 4, 0, 1, 3}; 
    std::map<double, int> freq; 

    // build frequency count 
    for (double val:vec) 
    { 
     freq[val]++; 
    } 
    // or 
    //std::for_each(vec.begin(), vec.end(), [&freq](double val){ freq[val]++; }); 
    // not sure if using for_each makes sense here. I don't think we get much 

    // find highest frequency 
    std::map<double, int>::iterator found = std::max_element(freq.begin(), 
                  freq.end(), 
                  comp); 
    // cache what we found so we have it later 
    std::pair<double, int> mode = *found; 
    std::cout << found->first << " freq: " << found->second << '\n'; 
    // remove so we don't find again 
    freq.erase(found); 

    // look for next highest frequency 
    found = std::max_element(freq.begin(), freq.end(), comp); 
    // test for same frequency 
    if (mode.second == found->second) 
    { 
     std::cout << found->first << " freq: " << found->second << '\n'; 
    } 
} 

std::for_each documentation

+0

ありがとう、あなたの方法は魅力のように機能します。 –

関連する問題