#include <iostream> #include <stdlib.h> #include <iomanip> using namespace std;
ID: 3629576 • Letter: #
Question
#include <iostream>#include <stdlib.h>
#include <iomanip>
using namespace std;
void tostring(int d,char value[]);
string todate(int doy);
void PrintMenu();
void explain();
void generate(int[]);
void check();
bool matches(int[]);
void display();
void print(int,int);
int main()
{char choice;
srand(time(0));
do
{PrintMenu();
cin>>choice;
switch(choice)
{case '1': explain();
break;
case '2': check();
break;
case '3': display();
break;
case 'E': return 0;
default: cout<<"Invalid choice ";
}
}while(true);
}
void display()
{int dates[365];
int i,line=0;
generate(dates);
cout<<"Here are the results of generating a set of 23 birthdays ";
cout<<"============================================================";
for(i=0;i<365;i++)
if(dates[i]!=0)
{if(line==0)
cout<<endl;
print(i+1,dates[i]);
line++;
if(line==3)
line=0;
}
cout<<endl<<endl<<endl;
}
void print(int i,int d)
{if(d==1)
cout<<" ";
else
cout<<"("<<d<<") ";
cout<<todate(i)<<" ";
}
void generate(int dates[])
{int i,n;
for(i=0;i<365;i++)
dates[i]=0;
for(i=0;i<23;i++)
{n=rand()%365;
dates[n]++;
}
}
bool matches(int dates[])
{
for(int i=0;i<365;i++)
if(dates[i]>1)
return true;
return false;
}
void check()
{int i,dates[365];
int count=0;
cout<<"Generating 1000 sets of 23 birthdays and checking for matches... ";
for(i=0;i<1000;i++)
{generate(dates);
if(matches(dates))
count++;
}
cout<<"Results : "<<count<<" out of 1000 ("<<setprecision(1)<<fixed<<
count/1000.*100<<"%) of the sets contained matching birthdays ";
}
void PrintMenu()
{cout<<"1- Explain birthday paradox ";
cout<<"2- Check birthday paradox by generating 1000 sets of birthdays ";
cout<<"3- Display one set of 23 birthdays ";
cout<<"E- Exit ";
}
void explain()
{cout<<"The birthday paradox can be described as follows: ";
cout<<"If 23 persons are chosen at random, then the chances ";
cout<<"are more than 50% that at least two will have the same birthday! ";
}
string todate(int doy)
{
int days=0,i=0,n;
string mth[12]={"January ","February ","March ","April ","May ","June ","July ",
"August ","September ","October ","November ","December "};
int daysinmonth[12]={31,28,31,30,31,30,31,31,30,31,30,31};
string date;
char doys[3];
while(doy>daysinmonth[i])
{doy=doy-daysinmonth[i++];
}
tostring(doy,doys); //convert integer to string
date=mth[i]+doys;
return date;
}
void tostring(int d,char value[])
{
int t;
if(d>=10)
{t=d%10;
d=d/10;
value[0]=d+48;
value[1]=t+48;
value[2]='';
}
else
{value[0]=d+48;
value[1]='';
}
return ;
}
Explanation / Answer
please rate - thanks
I love seeing my code on here.
what's wrong with it?
here is the final version, you may have gotten an incomplete one
#include <iostream>
#include <stdlib.h>
#include <iomanip>
#include <time.h>
#include <string.h>
using namespace std;
void todate(int );
void PrintMenu();
void explain();
void generate(int[]);
void check();
bool matches(int[]);
void display();
void print(int,int);
#define SETS 23
#define SIZE 1000
int main()
{char choice;
srand(time(NULL));
do
{PrintMenu();
cin>>choice;
switch(choice)
{case '1': explain();
break;
case '2': check();
break;
case '3': display();
break;
case 'E': return 0;
default: cout<<"Invalid choice ";
}
}while(true);
}
void display()
{int dates[365];
int i,line=0;
generate(dates);
cout<<"Here are the results of generating a set of "<<SETS<<" birthdays ";
cout<<"============================================================";
for(i=0;i<365;i++)
if(dates[i]!=0)
{if(line==0)
cout<<endl;
print(i+1,dates[i]);
line++;
if(line==3)
line=0;
}
cout<<endl<<endl<<endl;
}
void print(int i,int d)
{
if(d==1)
cout<<" ";
else
cout<<"("<<d<<") ";
todate(i);
}
void generate(int dates[])
{int i,n;
for(i=0;i<365;i++)
dates[i]=0;
for(i=0;i<SETS;i++)
{n=rand()%365;
dates[n]++;
}
}
bool matches(int dates[])
{
for(int i=0;i<365;i++)
if(dates[i]>1)
return true;
return false;
}
void check()
{int i,dates[365];
int count=0;
cout<<"Generating "<<SIZE<<" sets of "<<SETS<<" birthdays and checking for matches... ";
for(i=0;i<SIZE;i++)
{generate(dates);
if(matches(dates))
count++;
}
cout<<"Results : "<<count<<" out of "<<SIZE<<" ("<<setprecision(1)<<fixed<<
count/(SIZE*1.)*100<<"%) of the sets contained matching birthdays ";
}
void PrintMenu()
{cout<<"1- Explain birthday paradox ";
cout<<"2- Check birthday paradox by generating "<<SIZE<<" sets of birthdays ";
cout<<"3- Display one set of "<<SETS<<" birthdays ";
cout<<"E- Exit ";
}
void explain()
{cout<<"The birthday paradox can be described as follows: ";
cout<<"If "<<SIZE<<" persons are chosen at random, then the chances ";
cout<<"are more than 50% that at least two will have the same birthday! ";
}
void todate(int doy )
{
int days=0,i=0;
char mth[12][12]={"January","February","March","April","May","June","July",
"August","September","October","November","December"};
int daysinmonth[12]={31,28,31,30,31,30,31,31,30,31,30,31};
string doys;
while(doy>daysinmonth[i])
{doy=doy-daysinmonth[i++];
}
cout<< mth[i]<<","<<doy<<" ";
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.