Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Introduction: If you have ever collected hockey cards (or other sports cards) yo

ID: 3795062 • Letter: I

Question

Introduction:

If you have ever collected hockey cards (or other sports cards) you may have wondered how many packages you have to buy to get a complete set. In this assignment, we will try and find out. Let’s start with some assumptions to pin down the problem. First, we will assume that a complete set consists of 500 different cards. You buy cards in packages of 7. We will assume that the frequency of the cards follows a uniform distribution. That is, assume that the manufacturer produces the same quantity of each card so that the probability of getting any specific card is the same as any other. (This assumption may not be realistic, but we make it to simplify the problem.)

Assignment:

Your task is to write a program that simulates buying packages of cards until the set is complete. At each step of the program you should “buy a package of cards”. For each card in the package that you buy, you should keep track of how many copies of it you now have in your collection. An experiment consists of repeating this process until you have a complete set of cards. You should first write a program that conducts a single experiment. Since the number of packages you have to buy will vary depending on the exact cards you get, you will have to run many such experiments to determine what how many packages you would expect to buy to complete the set. In this assignment you are to run at least 200 experiments and gather statistics about them. When you are done, you should output the following information:

1. The average number of packages you had to buy.

2. The maximum number of packages you had to buy to complete your set.

3. The minimum number of packages you had to buy to complete your set.

Methodology:

You can use an array to keep track of the individual cards you own and another array to keep track of the cards in the package you just purchased. To simulate buying a package of cards, you should use a random number generator to generate seven values between 0 and 499. Then you should update the list of cards in your collection by adding these new cards. To determine whether you have a complete set, you can either keep track of the total number of different cards you have or keep track of the number of cards missing from your set. Each time you update the list you can update this count. Make sure to seed your random number generator differently for each experiment to ensure that each experiment generates a different collection of cards.

You must define the following function:

A function which generates a package of seven cards. This function has a single array parameter. It should populate should an array of seven cards by generating seven distinct values between 0 and 499. If a duplicate value is found, it should be discarded so that the seven values are all distinct. The random number generator should be seeded in the main program or the numbers generated will always be the same. (Hint: When developing your program you can define this function to allow duplicate cards. Eliminating the duplicates is more difficult and should be done after you get the rest of the program working. You will get part marks if you do not finish that part of the program.)

Explanation / Answer

#include<stdio.h>

#include<time.h>

#define testcases 200

#define maxCards 500

void maxDuplicates(int deck[maxCards][testcases],int testcase,int noOfCards){

int i;

int max=deck[0][testcase];

for(i=0;i<noOfCards;i++){

if(max<deck[i][testcase])

max=deck[i][testcase];

}

printf("Maximum number of duplicates in testcase %d =%d ",testcase+1,max);

return;

}

void getNewPack(int pack[],int noOfCards){

int i;

for(i=0;i<7;i++){

pack[i]=rand()%noOfCards;

}

}

int addCardsToCollection(int deck[maxCards][testcases],int pack[],int testcase){

int i,changes=0;

for(i=0;i<7;i++){

if(deck[pack[i]][testcase]==0)

changes++;

deck[pack[i]][testcase]++;

}

return changes;

}

void avgPacks(long int packs[]){

int i;

long int total=packs[0];

float avg;

for(i=0;i<testcases;i++){

total+=packs[i];

}

avg=(float)((float)total/(float)testcases);

printf("Minimum number of packs = %f ",avg);

return;

}

void maxPacks(long int packs[]){

int i;

long int max=packs[0];

for(i=0;i<testcases;i++){

if(max<packs[i]){

max=packs[i];

}

}

printf("Maximum number of packs = %ld ",max);

return;

}

void minPacks(long int packs[]){

int i;

long int min=packs[0];

for(i=0;i<testcases;i++){

if(min>packs[i]){

min=packs[i];

}

}

printf("Minimum number of packs = %ld ",min);

return;

}

int main(){

int pack[7];

int noOfCards=300;

int cardsRemaining;

long int boughtPacks[testcases];

int deck[maxCards][testcases];

srand(time(NULL));

int i;

for(i=0;i<testcases;i++){

cardsRemaining=noOfCards;

boughtPacks[i]=0;

while(cardsRemaining>0){

getNewPack(pack,noOfCards);

boughtPacks[i]++;

cardsRemaining-=addCardsToCollection(deck,pack,i);

}

maxDuplicates(deck,i,noOfCards);

}

avgPacks(boughtPacks);

maxPacks(boughtPacks);

minPacks(boughtPacks);

printf("press Enter to exit ");

char hold;

scanf("%c",&hold);

return 0;

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote