in C++. please put comments. spaceing between lines (Simulation: coupon collecto
ID: 3568666 • Letter: I
Question
in C++. please put comments. spaceing between lines
(Simulation: coupon collectors problem) Coupon collector is a classic statistic problem with many practical applications. The problem is to pick objects from a set of objects repeatedly and determine how many picks are needed for all the objects to be picked at least once. A variation of the problem is to pick cards from a shuffled deck of 52 cards repeatedly and find out how many picks are needed before you see one of each suit. Assume a picked card is placed back in the deck before picking another. Write a program to simulate the number of picks needed to get four cards from each suit and display the four cards picked (it is possible that a card may be picked twice). Here is a sample run of the program: Queen of Spades 5 of Clubs Queen of Hearts 4 of Diamonds Number of picks: 12Explanation / Answer
#include <iostream>
#include <algorithm>
#include<ctime>
using namespace std;
// names of ranks.
static const char *ranks[] =
{
"Ace", "Two", "Three", "Four", "Five", "Six", "Seven",
"Eight", "Nine", "Ten", "Jack", "Queen", "King"
};
// name of suites
static const char *suits[] =
{
"Spades", "Clubs", "Diamonds", "Hearts"
};
int print_card(int n)
{
int count=0;
int j,i;
int sum=0;
i=n/13;
j=0;
if(count==4)
{
cout<<"Number of picks: "<<sum<<endl;
return 0;
}
else
{
if(j!=i)
{
if(n/13==4 && count<5)
{
cout << ranks[n % 13] << " of " << suits[0] << endl;
count++;
sum++;
}
else if(count<5)
{
cout << ranks[n % 13] << " of " << suits[n / 13] << endl;
count++;
sum++;
}
j=i;
}
}
}
int main()
{
srand((unsigned int)time(NULL));
int deck[52];
// Prime, shuffle, dump
for (int i=0;i<52;deck[i++]=i);
random_shuffle(deck, deck+52);
for_each(deck, deck+52, print_card);
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.