Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- srand(time(NULL)) function to initialize the random number generator
- Added rand() % num_participants to ensure that the two selected indexes are different to avoid a participant competing against themselves.
- */
- void generate_tournament_draw(char** participant_names, int num_participants) {
- int num_matches = num_participants / 2;
- char** matches = (char**)malloc(num_matches * sizeof(char*));
- // Initialize the random number generator
- srand(time(NULL));
- // Initial Matches generation
- for (int i = 0; i < num_matches; i++) {
- matches[i] = (char*)malloc(200 * sizeof(char)); // Assuming a maximum match display length of 200 characters
- // Randomly select participants for each match
- int index1 = rand() % num_participants;
- int index2 = rand() % num_participants;
- while (index2 == index1) {
- index2 = rand() % num_participants;
- }
- sprintf(matches[i], "Match %d: %s vs %s", i + 1, participant_names[index1], participant_names[index2]);
- }
- printf("Tournament Draw:\n");
- for (int i = 0; i < num_matches; i++) {
- printf("%s\n", matches[i]);
- }
- // Continue until there are only two participants left
- while (num_participants > 2) {
- num_participants /= 2;
- num_matches = num_participants / 2;
- // Update participant names array and shuffle it
- for (int i = 0; i < num_participants; i++) {
- free(participant_names[i]);
- participant_names[i] = (char*)malloc(100 * sizeof(char)); // Assuming a maximum name length of 100 characters
- sprintf(participant_names[i], "Winner of Match %d", i + 1);
- }
- // Shuffle the participant names array using Fisher-Yates algorithm
- for (int i = num_participants - 1; i > 0; i--) {
- int j = rand() % (i + 1);
- char* temp = participant_names[i];
- participant_names[i] = participant_names[j];
- participant_names[j] = temp;
- }
- // Generate new matches
- for (int i = 0; i < num_matches; i++) {
- free(matches[i]);
- matches[i] = (char*)malloc(200 * sizeof(char)); // Assuming a maximum match display length of 200 characters
- sprintf(matches[i], "Match %d: %s vs %s", i + 1, participant_names[i], participant_names[num_participants - i - 1]);
- }
- printf("\nNext Round:\n");
- for (int i = 0; i < num_matches; i++) {
- printf("%s\n", matches[i]);
- }
- }
- // Free the allocated memory
- for (int i = 0; i < num_matches; i++) {
- free(matches[i]);
- }
- free(matches);
- }
- int main() {
- int num_participants;
- printf("Enter the number of participants: ");
- scanf("%d", &num_participants);
- char** participant_names = (char**)malloc(num_participants * sizeof(char*));
- for (int i = 0; i < num_participants; i++) {
- participant_names[i] = (char*)malloc(100 * sizeof(char)); // Assuming a maximum name length of 100 characters
- printf("Enter the name of participant %d: ", i + 1);
- scanf("%s", participant_names[i]);
- }
- generate_tournament_draw(participant_names, num_participants);
- // Free the allocated memory
- for (int i = 0; i < num_participants; i++) {
- free(participant_names[i]);
- }
- free(participant_names);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement