2016-08-28 10 views
-3

こんにちは私は、ユーザーが数字を入力できるようにするプロジェクトに取り組んでいます。コードは、最大のものから順に整理し、各数字の数が入力されたことを示します。私は最大に最小の組織に問題があるとstd上配列を最小から最大まで整理C++

using namespace std; 
#include <iostream> 

void main(){ 
    double a[30]; 
    double e[30]; 
    double b; 
    int c[30]; 
    int d; 
    double f=0; 
    cout << "how many input values [max 30]:"; 
    cin >> d; 
    cout << "enter " << d << " numbers:"<<endl; 
    for(int x=0; x<d;x++){ 
     cin >> a[x]; 
     c[x]=0; 
    } 
    cout << endl; 
    for(int y=0; y<d;y++){ 
     if(a[y]>=f){ 
      f=a[y]; 
     } 
    } 
    for(int z=0;z<d;z++){ 
     if(a[z]){ 
      c[z]++; 
     } 
     if(a[z]>=a[z+1]){ 
      e[z]=a[z]; 
     } 
    } 
    cout << "numbers  count"<< endl; 
    for(int printloop=0;printloop<d;printloop++){ 
     if(a[printloop]>0){ 
      cout << e[printloop]<< "    " << c[printloop] << endl; 
     } 
    } 
    cout << "max value:" << b << endl; 
} 
+1

おそらく、配列の代わりにベクターを使用する必要があります。そうすれば、ユーザーを30人に制限する必要はありません。 –

+1

より良い変数名を使用して、何をするのかを簡単に判断できるようにする必要があります。また、あなたの(しようとしている)ことを読者に伝えるためにコメントを使用してください。問題はソートしていない第2回目のループとあなたの思考をしない3回目のループで見る限りです。 – Surt

+0

番号の範囲は何ですか? – Shravan40

答えて

1

ソリューションベース::マップhttp://en.cppreference.com/w/cpp/container/mapCompareテンプレート引数に注意し、C++ 11(http://en.cppreference.com/w/cpp/language/range-forを)inputedているどのように多くの各数の数えています

#include <iostream> 
#include <map> 


int main(int argc, char** argv) 
{ 

    std::map<int, int> count; 

    int n; 
    std::cout << "How many numbers ? "; 
    std::cin >> n; 

    std::cout << "Now enter " << n << " numbers : "; 

    for(auto i=0; i<n; i++) 
    { 
     int tmp; 
     std::cin >> tmp; 
     count[tmp]++; 
    } 

    for(auto const& elem : count) 
     std::cout << "Element " << elem.first << " count : " << elem.second << "\n"; 

    return 0; 
} 
関連する問題