1

我有一个任务,我需要计算两个人在给定房间大小(在我的例子中为 50)在多次试验(5000)中共享相同生日的概率。我必须将生日随机分配给房间里的人数。不同之处在于我需要使用布尔函数来检查生日是否相同。我不知道为什么我的输出关闭了,但我相信这与我的两个循环有关。

> 

    #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

2

Just do it like this:

bool SameBirthday(int birthdays[], int numPeople)
{
    for(int x=0; x<numPeople; x++){         
        for(int y=0; y<numPeople; y++){
            if(birthdays[x] == birthdays[y])
                return true;
        }
    }
    return false;
}

Your logic in your nested loop is wrong..

for (SpaceOne = 0; SpaceOne < numpeople - 1; SpaceOne++)
    for (SpaceTwo = SpaceOne + 1; SpaceTwo < numpeople; SpaceTwo++)

Your inner loop is skipping n number of checks where n equals SpaceOne.

By the way, this is not C programming. You can declare variable within a for-loop.

于 2015-08-29T18:19:02.773 回答
0

我发现实际功能存在两个问题。首先,当没有生日匹配时,SameBirthday 需要返回一个值 (false)。在所有循环完成后,您可以在函数结束时执行此操作。

其次,您需要在找到匹配项时增加 numMatches。

于 2015-08-29T18:24:02.983 回答
0

您的另一个问题是 numpeople 将始终是人数减 1。您实际上根本不需要该变量。您的“人”变量是正确的人数。

于 2015-08-31T05:08:56.240 回答
0

澄清编码其他部分的问题。我想这就是你们学校想要的。

int main(){
    //All your variables

    for(int x=0; x<NUM_TRIALS; x++){
        for(int y=0; y< MAX_PEOPLE; y++){
            birthdays[y] = (rand() % 365 + 1);
        }

        if(SameBirthday(birthdays, MAX_PEOPLE) == true)
            numMatches ++;              
    }
    Probability = ((double)numMatches / NUM_TRIALS);
    cout << "For " << people << ", the probability of two birthdays is about " 
    << Probability << endl;     
}

NUM_TRIALS生成 5000 个数据集。因此,您为 50 个学生生成了 5000 次生日。对于一个 50 人的班级中的每个试验,您检查是否有 2 个人的生日相同。如果有,numMatches + 1.

经过 5000 次试验后,您得到概率。

于 2015-08-29T19:00:55.720 回答