How to resolve the algorithm Round-robin tournament schedule step by step in the AWK programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Round-robin tournament schedule step by step in the AWK programming language

Table of Contents

Problem Statement

A round-robin tournament is also known as an all-play-all-tournament; each participant plays every other participant once. For N participants the number of rounds is N-1 if N is an even number. When there are an odd number of participants then each round one contestor has no opponent (AKA as a "bye"). The number of rounds is N in that case. Write a program that prints out a tournament schedule for 12 participants (represented by numbers 1 to 12).

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Round-robin tournament schedule step by step in the AWK programming language

Source code in the awk programming language

# syntax: GAWK -f ROUND-ROBIN_TOURNAMENT_SCHEDULE.AWK
BEGIN {
    main(1)
    main(2)
    main(5,"The Bizzaros")
    main(12)
    exit(0)
}
function main(n,description,  arr,i,j,leng,tmp) {
    if (n < 2) {
      printf("\n%d is too few participants\n",n)
      return
    }
    printf("\n%d players  %s\n",n,description)
    for (i=1; i<=n; i++) {
      arr[i] = i
    }
    if (n % 2 == 1) {
      arr[++n] = 0 # a "bye"
    }
    leng = length(n-1)
    for (i=1; i<n; i++) {
      printf("\nround %*d:",leng,i)
      for (j=1; j<=n/2; j++) {
        printf("%4s",arr[j]==0?"bye":arr[j])
      }
      printf("\n%*s",leng+7,"")
      for (j=n; j>n/2; j--) {
        printf("%4s",arr[j]==0?"bye":arr[j])
      }
      printf("\n")
      tmp = arr[n]
      for (j=n; j>2; j--) {
        arr[j] = arr[j-1]
      }
      arr[2] = tmp
    }
}


  

You may also check:How to resolve the algorithm File size step by step in the PHP programming language
You may also check:How to resolve the algorithm Zig-zag matrix step by step in the OCaml programming language
You may also check:How to resolve the algorithm Subleq step by step in the Wren programming language
You may also check:How to resolve the algorithm Loops/While step by step in the Dyalect programming language
You may also check:How to resolve the algorithm Function definition step by step in the Transd programming language