开发者

When I input a number I get 1 no matter what number I use. How can I fix this?

//"This program will ask for the number of people in a group and then output percentage likelyhood that two birthdays occur on the same day." << endl << endl;


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

int FindThePerctange(int [], int);
void RandArray(int, int []);
void Counter(int []);

int main (){
    int GroupNumber;
    int DayOfBirth [365] = {};
    char Percant;
    Percant = '%';
    cout << "This program will ask for the number of people in a group and then output percentage likelyhood that two birthdays occur on the same day." << endl << endl;
    cout << "How many in group (0 quits)? "; 
    cin >> GroupNumber;
    if (GroupNumber == 0){
        cout << "\nThanks for using this program.";
    srand(time(0));
    }
    else{
        cout << "In a group of " << GroupNumber << " the chances for two birthdays the same is " << FindThePerctange(DayOfBirth, GroupNumber) << Percant << "." << endl << endl;
        cout << "How many in group (0 quits)? "; 
        cin >> GroupNumber;
        cout << "In a group of " << GroupNumber << " the chances for two birthdays the same is " << FindThePerctange(DayOfBirth, GroupNumber) << Percant << "." << endl << endl;
        cout << "How many in group (0 quits)? " << endl;
        cin >> GroupNumber;
        cout << "Thanks for using this program.";
    }
    cin.get();
    cin.get();
    return 0;
}


int FindThePerctange(int DayOfBirth [], int GroupNumber){
    double Overlap = 0, Percentage;
    for (int d =0; d&l开发者_如何学Pythont;=10000; d++){
        (GroupNumber, DayOfBirth);
        while (d <= 365){
            int j;
            j = 0;
            j++;
            if(DayOfBirth[j] >= 2){
                Overlap = Overlap + 1;
                j=365;
            }
        return j;
        }
    Percentage = (Overlap/10000)*100;
    return Percentage;
    }
}

void RandArray(int GroupNumber,  int DayOfBirth[]){
    int Day, d;
    while (d < GroupNumber){
        Day = rand()%365;
        DayOfBirth[Day] +=1;
        d++;
    }
}

void Counter(int DayOfBirth[]){
    for (int d = 0; d<=365; d++){
        DayOfBirth[d] = 0;
    }
}


In your method FindThePerctange you have an issue. Inside the while loop, you declare the variable j, set it to 0, increment it to 1, then return it immediately. This means you'll get 1 every time.

What I think you are trying to do is increment j each time in the while loop. If you are, put the declaration of j outside the while loop, like so:


int FindThePerctange(int DayOfBirth [], int GroupNumber){
    double Overlap = 0, Percentage;
    int j;
    for (int d =0; d<=10000; d++){
        //(GroupNumber, DayOfBirth);
        j = 0;
        while (d <= 365){
            j++;
            if(DayOfBirth[j] >= 2){
                Overlap++;
                j=365;
            }
        }
    Percentage = (Overlap/10000)*100;
    return Percentage;
    }
}

This still doesn't make your code work, but it will at least not return 1. I'm not going to re-write your entire program for you (someone else probably will) but it does not do what you are expecting it to do.

Edit: I want to help you more, but your code is internally flawed. Your biggest problem is probably the fact that you don't assign any value to the DayOfBirth array. I would suggest going back and re-coding this. The problem you are asking is trivial. Consider the following:

Assume each person's birthday is a random number from 1 to 365. The first person's birthday then is assigned A. The second person has a 1 in 365 chance of hitting the same day. So for group size 2, the chance is 1 / 365, or about .27 . Assuming group size is 3, and the 2nd person didn't hit the first, the third person has a chance of 2 / 365, or about .54% chance. Assuming that the random number generator is completely random, your chance that two birthdates will collide is going to be (groupsize - 1) / 365.

So your code will only need to accept a GroupSize. It is shortened to:


float FindThePerctange(int GroupNumber){
    return (GroupNumber - 1) / 365 * 100;
}


Neither are you calling RandArray() anywhere nor is DayofBirth[] getting any value.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜