1

クラスの値の配列のモードを見つける関数を作成する必要があります。値の配列と、配列内の有効な値の数に等しい変数の 2 つの引数を取ります。簡単そうに思えましたが、1 週間試してみたところ、自分のコードが機能していないように思えます。

アイデアは、1 次元配列を取得して 2 次元配列に格納することです。最初の値は値の配列からの値であり、2 番目の値はその値が発生する回数です。これまでのところ、まったく正しく機能していないか、まったく機能していません。問題は明らかであるように感じますが、私は 1 週間困惑しています。

次を含む配列でテストしています:9.0、4.0、4.0、4.0、5.0、5.0、6.0、6.0、6.0、7.0、1.0、9.0、10.0

コード:

void mode(double x[], const int n)
{
    int j, k, m=1, p, numofmodes=0;
    bool match=false, breaker;
    double y[100][2]={0}, max=0;



    y[0][0] = x[0];
    y[0][1] = 1;

    for(j=1; j<=(n-1); j++)  //
    {
        for (k=0; k<=(m-1); k++)
            if (x[j] == y[k][0])
            {
                y[k][1]++;
                match = true;
            }

        if (match == false)
        {
            y[m][0] = x[j];
            y[m][1] = 1;
            m++;
        }
        match = false;
    }



    for(j=0; j<=(n-1); j++)
    {
        if (y[j][1] > max)
            max = y[j][1];
    }

    for(j=0; j<=(n-1); j++)
    {
        if (y[j][1] = max)
            numofmodes++;
    }


    for(j=0; j<=(n-1); j++)
    {
        cout<<y[j][0]<<"    "<<y[j][1]<<endl;
    }


    cout<<"There are "<<numofmodes<< " modes in the data set."<<endl;

    for(j=0; j<=(n-1); j++)
    {
        if (y[j][1] = max)
        {
            cout<<y[j][0]<<" appears "<<max<<" times."<<endl;
        }
    }

}

出力: 9 2 0 2 0 2 0 2 0 2 0 2 0 2 0 2 0 2 0 2 0 2 0 2 0 2 データ セットには 13 のモードがあります。9は2回登場。0 が 2 回表示されます。0 が 2 回表示されます。0 が 2 回表示されます。0 が 2 回表示されます。0 が 2 回表示されます。0 が 2 回表示されます。0 が 2 回表示されます。0 が 2 回表示されます。0 が 2 回表示されます。0 が 2 回表示されます。0 が 2 回表示されます。0 が 2 回表示されます。

4

2 に答える 2

0

修正しました。ここに、動作するように記述されたメイン関数を含めました。

Input1.txt:9.0、4.0、4.0、4.0、4.0、4.0、5.0、5.0、6.0、6.0、6.0、6.0、6.0、6.0、7.0、1.0、9.0、10.0、4.0、-9999

=いくつかを変更して==、forループの1つで数学エラーを修正しましたが、今では...金色に見えます。オーラムエストポテスタ。:D

#include <iostream>
#include <cstdlib>
#include <fstream>
void mode(double x[], const int n);
using namespace std;

int main()
{
        const int N = 100;
        double x[N];
        void mode (double x[],int n);
        int n;
        ifstream inFile;

        inFile.open("input1.txt");
        n=0;
        inFile>>x[n];
        while (x[n]!=-9999)
        {
            n++;
            inFile >>x[n];
        }
        inFile.close( );
        mode(x,n);

        inFile.open("input2.txt");
        n=0;
        inFile >>x[n];
        while (x[n]!=-9999)
        {
            n++;
            inFile >>x[n];
        }
        inFile.close( );
        mode(x,n);



        inFile.open("input3.txt");
        n=0;
        inFile >>x[n];
        while (x[n]!=-9999)
        {
            n++;
            inFile >>x[n];
        }
        inFile.close( );
        mode(x,n);

        system ("PAUSE");
        return 0;
}






void mode(double x[], const int n)
{
    int j, k, m=1, p, numofmodes=0;
    bool match=false, breaker;
    double y[100][2]={0}, max=0;

    for(p=0;p<=n-1;p++)
    {
        y[p][0] = x[p];
        y[p][1]++;
    }


    for(j=1; j<=(n-1); j++)
    {
        for (k=0; k<=m; k++)
            if (x[j] == y[k][0])
            {
                y[k][1]++;
                match = true;
                cout<<y[k][0]<<"   "<<y[k][1]<<endl;
            }

        if (match == true)
        {
            y[m][0] = x[j];
            y[m][1] = 1;
            m++;
        }
        match = false;
    }



    for(j=0; j<=(n-1); j++)
    {
        if (y[j][1] > max)
            max = y[j][1];
    }

    for(j=0; j<=(n-1); j++)
    {
        if (y[j][1] == max)
            numofmodes++;
    }


    cout<<"There are "<<numofmodes<< " modes in the data set."<<endl;

    for(j=0; j<=(n-1); j++)
    {
        if (y[j][1] == max)
        {
            cout<<"The number "<<y[j][0]<<" appears "<<max<<" times."<<endl;
        }
    }
    cout<<endl;
}

出力:データセットには2つのモードがあります。数字の4は6回現れます。数字の6は6回表示されます。

データセットには2つのモードがあります。数字の2が3回表示されます。数字の5が3回表示されます。

データセットには4つのモードがあります。数字の1が2回表示されます。数字の7が2回表示されます。数字の19は2回表示されます。30という数字が2回現れます。

何かキーを押すと続行します 。。。

于 2012-12-07T22:23:07.723 に答える
0

2 x if (y[j][1] = max)- (match = false) の問題と同様 - この時点maxでは 2 であるため、値は 2 で上書きされます。

最初の列の問題は、問題が原因のようmatch = falseです。修正して再コンパイルした後もまだ発生していますか?

于 2012-12-07T01:30:26.643 に答える