//"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<=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;
}
}
2 に答える
メソッド FindThePerctange に問題があります。while ループ内で、変数 j を宣言し、それを 0 に設定し、それを 1 にインクリメントしてから、すぐに返します。これは、毎回 1 を取得することを意味します。
あなたがやろうとしていると思うのは、whileループで毎回jをインクリメントすることです。そうであれば、次のように j の宣言を while ループの外側に置きます。
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;
}
}
これでもコードは機能しませんが、少なくとも 1 は返されません。私はあなたのためにプログラム全体を書き直すつもりはありません (おそらく他の誰かが書き直すでしょう)。行う。
編集:もっとお手伝いしたいのですが、あなたのコードには内部的な欠陥があります。あなたの最大の問題は、おそらく DayOfBirth 配列に値を割り当てていないことです。戻ってこれを再コーディングすることをお勧めします。あなたが求めている問題は些細なことです。次の点を考慮してください。
各人の誕生日が 1 から 365 までの乱数であると仮定します。最初の人の誕生日には A が割り当てられます。2 番目の人が同じ日に当たる確率は 365 分の 1 です。したがって、グループ サイズ 2 の場合、確率は 1/365、つまり約 .27 です。グループのサイズが 3 で、2 人目が 1 人目をヒットしなかったと仮定すると、3 人目が 2/365 の確率、つまり約 0.54% の確率になります。乱数ジェネレーターが完全にランダムであると仮定すると、2 つの生年月日が衝突する確率は (groupsize - 1) / 365 になります。
したがって、コードは GroupSize を受け入れるだけで済みます。次のように短縮されます。
float FindThePerctange(int GroupNumber){
return (GroupNumber - 1) / 365 * 100;
}
RandArray()
あなたはどこにも 電話していないDayofBirth[]
し、何の価値も得ていません。