1

特定の部屋のサイズ (私の場合は 50) で 2 人が同じ誕生日を共有する確率を、多くの試行 (5000) にわたって計算する必要がある課題があります。部屋にいる人数に誕生日をランダムに割り当てる必要があります。違いは、誕生日が同じかどうかを確認するためにブール関数を使用する必要があることです。出力がオフになっている理由はわかりませんが、2 つのループに関係があると思います。

> 

    #include <iostream>
    #include <ctime>
    #include <cstdlib>
    using namespace std;

    bool SameBirthday(int birthdays[], int numpeople);
    const int MAX_PEOPLE = 50;
    const double NUM_TRIALS = 5000.0;
    const int DAYS_IN_YEAR = 365;

    int main(void)
    {
        int numMatches = 0;
        int people = 2;
        int trial = 0;
        int numpeople = 0;
        int i = 0;
        int birthdays[MAX_PEOPLE];
        bool Match;
        double Probability = 0;
        srand(time(0));
    for (people = 2; people <= MAX_PEOPLE; people++)
        {
            numMatches = 0;

            for (trial = 0; trial < NUM_TRIALS; trial++)
            {
                for (i = 0; i < people; i++)
                {
                    birthdays[i] = (rand() % 365 + 1);
                    numpeople = i;

                }
                if ((SameBirthday(birthdays, numpeople) == true))
                    {
                        numMatches++;
                    }
            }
            Probability = (numMatches / NUM_TRIALS);
            cout << "For " << people << ", the probability of two birthdays is about " << Probability << endl;
        }
    }
    bool SameBirthday(int birthdays[], int numpeople)
    {

        bool match = false;
        int numberofmatches = 0;
        //Use this function to attempt to search the giving array birthdays and   count up number of times
        //at least two people have matching birthdays for any given 1 trial
        for (int SpaceOne = 0; SpaceOne < numpeople; SpaceOne++)
        {
            for (int SpaceTwo = SpaceOne + 1; SpaceTwo < numpeople; SpaceTwo++)
            {
                if (birthdays[SpaceTwo] == birthdays[SpaceOne])
                {
                    return true;
                }
            }
        }
return false;
    }

    I know that the code has errors in certain spots that was because I started trying different things, but any help would be appreciated. 
EDIT- My only issue now is that for my output I have a zero for the probability of 2 people in the room have a birthday, which is not right. It seems like my outputs are like a person off, the probability of 2 people is shown as the probability for three people and so on. 
EDIT(8-31-2015): I also forgot to mention that my Professor stated that my SameBirthday function needed the parameters: birthday[], and numpeople so I cannot use MAX_PEOPLE as a parameter. My professor also suggested using a triple nested for loop within the main body of the function. I believe what is making my output off by one for each person relates to the triple nested for loop, but I am unsure what would cause the issue.
4

4 に答える 4

0

実際の機能には 2 つの問題があります。まず、一致する誕生日がない場合、SameBirthday は値 (false) を返す必要があります。すべてのループが完了した後、関数の最後にそれを行うことができます。

次に、一致が見つかったときに numMatches をインクリメントする必要があります。

于 2015-08-29T18:24:02.983 に答える
0

もう 1 つの問題は、numpeople が常に人数から 1 を引いた数になることです。実際には、その変数はまったく必要ありません。あなたの「人」変数は正しい人数です。

于 2015-08-31T05:08:56.240 に答える