Advertisement
bitwise_gamgee

Untitled

May 20th, 2023
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.40 KB | None | 0 0
  1. /*
  2. srand(time(NULL)) function to initialize the random number generator
  3. Added rand() % num_participants to ensure that the two selected indexes are different to avoid a participant competing against themselves.
  4. */
  5.  
  6.  
  7. void generate_tournament_draw(char** participant_names, int num_participants) {
  8.     int num_matches = num_participants / 2;
  9.     char** matches = (char**)malloc(num_matches * sizeof(char*));
  10.  
  11.     // Initialize the random number generator
  12.     srand(time(NULL));
  13.  
  14.     // Initial Matches generation
  15.     for (int i = 0; i < num_matches; i++) {
  16.         matches[i] = (char*)malloc(200 * sizeof(char)); // Assuming a maximum match display length of 200 characters
  17.        
  18.         // Randomly select participants for each match
  19.         int index1 = rand() % num_participants;
  20.         int index2 = rand() % num_participants;
  21.         while (index2 == index1) {
  22.             index2 = rand() % num_participants;
  23.         }
  24.  
  25.         sprintf(matches[i], "Match %d: %s vs %s", i + 1, participant_names[index1], participant_names[index2]);
  26.     }
  27.  
  28.     printf("Tournament Draw:\n");
  29.     for (int i = 0; i < num_matches; i++) {
  30.         printf("%s\n", matches[i]);
  31.     }
  32.  
  33.     // Continue until there are only two participants left
  34.     while (num_participants > 2) {
  35.         num_participants /= 2;
  36.         num_matches = num_participants / 2;
  37.  
  38.         // Update participant names array and shuffle it
  39.         for (int i = 0; i < num_participants; i++) {
  40.             free(participant_names[i]);
  41.             participant_names[i] = (char*)malloc(100 * sizeof(char)); // Assuming a maximum name length of 100 characters
  42.             sprintf(participant_names[i], "Winner of Match %d", i + 1);
  43.         }
  44.         // Shuffle the participant names array using Fisher-Yates algorithm
  45.         for (int i = num_participants - 1; i > 0; i--) {
  46.             int j = rand() % (i + 1);
  47.             char* temp = participant_names[i];
  48.             participant_names[i] = participant_names[j];
  49.             participant_names[j] = temp;
  50.         }
  51.  
  52.         // Generate new matches
  53.         for (int i = 0; i < num_matches; i++) {
  54.             free(matches[i]);
  55.             matches[i] = (char*)malloc(200 * sizeof(char)); // Assuming a maximum match display length of 200 characters
  56.             sprintf(matches[i], "Match %d: %s vs %s", i + 1, participant_names[i], participant_names[num_participants - i - 1]);
  57.         }
  58.  
  59.         printf("\nNext Round:\n");
  60.         for (int i = 0; i < num_matches; i++) {
  61.             printf("%s\n", matches[i]);
  62.         }
  63.     }
  64.  
  65.     // Free the allocated memory
  66.     for (int i = 0; i < num_matches; i++) {
  67.         free(matches[i]);
  68.     }
  69.     free(matches);
  70. }
  71.  
  72. int main() {
  73.     int num_participants;
  74.     printf("Enter the number of participants: ");
  75.     scanf("%d", &num_participants);
  76.  
  77.     char** participant_names = (char**)malloc(num_participants * sizeof(char*));
  78.  
  79.     for (int i = 0; i < num_participants; i++) {
  80.         participant_names[i] = (char*)malloc(100 * sizeof(char)); // Assuming a maximum name length of 100 characters
  81.         printf("Enter the name of participant %d: ", i + 1);
  82.         scanf("%s", participant_names[i]);
  83.     }
  84.  
  85.     generate_tournament_draw(participant_names, num_participants);
  86.  
  87.     // Free the allocated memory
  88.     for (int i = 0; i < num_participants; i++) {
  89.         free(participant_names[i]);
  90.     }
  91.     free(participant_names);
  92.  
  93.     return 0;
  94. }
  95.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement