#include <stdio.h>

//function prototypes
void generatePackage(int *array);                   //generates a package of seven cards
int isDuplicate(int arr[], int arr_size, int num)   //checks if the given card is already in the package
int findMaxCopies(int collection[], int arr_size);  //scans the array of cards in the collection to count the most duplicates
int isSetFull(int collection[], int arr_size);      //checks if the collection contains all 500 cards

//main function
int main(){
    srand(time(NULL));      //seed random number generator

    int experiments = 1;            //keeps count of the number of experiments
    int totalPackagesBought = 0;    //keeps count of the number of packages bought in all experiments
    int maxPackagesBought = 0;      //stores the maximum number of packages bought in a single experiment
    int minPackagesBought = 0;      //stores the minimum number of packages bought in a single experiment

    //do 100 experiments
    while(experiments <= 100){
        int collection[1000];       //collection of bought cards
        int collectionCount = 0;
        int packagesBought = 0;

        int i = 0;

        //buy packages until set is full
        while(isSetFull(collection, collectionCount) == 0){
            int package[7];
            generatePackage(package);
            packagesBought++;
            totalPackagesBought++;
            collectionCount += 7;

            int j;
            for(j=0; j<7; j++){
                collection[i] = package[j];
                i++;
            }
        }

        //update number of maximum copies
        int maxCopies = findMaxCopies(collection, collectionCount);

        //update number of maximum packages bought
        if(packagesBought > maxPackagesBought){
            maxPackagesBought = packagesBought;
        }

        //update number of minimum packages bought
        if(experiments == 1){
            minPackagesBought = packagesBought;     //we set the number of packages bought in experiment 1 as the initial minimum
        }
        if(packagesBought < minPackagesBought){
            minPackagesBought = packagesBought;
        }

        printf("\n\nMaximum copies for experiment %i: %i", experiments, maxCopies);
        experiments++;
    }

    //calculate and print average number of packages bought
    int average = totalPackagesBought / 100;
    printf("\n\n\n\nAverage number of packages bought: %i", average);

    printf("\n\nMaximum number of packages bought: %i", maxPackagesBought);

    printf("\n\nMinimum number of packages bought: %i\n\n", minPackagesBought);

    return 0;
}

//generates a package of seven cards and stores it in the given array
void generatePackage(int *array){
   int i;

   for ( i = 0; i < 7; ++i) {
      int r = rand() % 500;
      //repeat the process until the card is not a duplicate
      while(isDuplicate(array, i, r) == 1){
        r = rand() % 500;
      }
      array[i] = r;
   }

}

//checks if the given card is already in the package
//returns 1 if the card is a duplicate or else 0
int isDuplicate(int arr[], int arr_size, int num){
    int duplicate = 0;
    int i, j;
    for(i=0; i<arr_size; i++)
       {
            if(arr[i]==num)
            {
                duplicate = 1;      //the card is a duplicate
            }
       }

   return duplicate;
}

//scans the array of cards in the collection to count the most duplicates
//returns the maximum number of single card duplicates
int findMaxCopies(int collection[], int arr_size){
    int maxCopies = 0;      //stores maximum number of copies
    int i, j;
    for(i=0; i<arr_size; i++)
       {
           int i_copies = 0;        //keeps count of number copies of the card
            for(j=i+1;j<arr_size;j++)
            {
                if(collection[i]==collection[j])
                {
                    i_copies++;
                }
           }
           if(i_copies > maxCopies){
                maxCopies = i_duplicates;
           }
       }

   return maxCopies;
}

//checks if the collection contains all 500 cards
//returns 1 if the set is full or 0 if the set is not full
int isSetFull(int collection[], int arr_size){
    int isFull = 1;
    if(arr_size == 0){
        isFull = 0;     //the collection is empty
    }
    int i, j;
    for(i=0; i<500; i++)        //loop from 0 to 499 checking how many copies of each are in the collection
       {
           int i_copies = 0;
            for(j=0;j<arr_size;j++)
            {
                if(collection[i]==collection[j])
                {
                    i_copies++;
                }
           }
           if(i_copies == 0){
                isFull = 0;     //if any number between 0 to 499 is not in the collection the set is not full
           }
       }

   return isFull;
}
