How to resolve the algorithm Round-robin tournament schedule step by step in the BASIC programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm Round-robin tournament schedule step by step in the BASIC 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 BASIC programming language
Source code in the basic programming language
arraybase 1
print "Twelve teams"
call roundrob(12)
print "Nine teams with byes"
call roundrob(9)
end
function nob(n,i,byes)
#helper function to allow byess to be printed intelligently
if n > 9 then pad = " " else pad = ""
if n = i and byes then
return pad + "B"
else
if i < 10 then return pad + string(i) else return string(i)
end if
end function
subroutine roundrob(n)
byes = 0
if n mod 2 = 1 then #if there is an odd number of competitors
byes = 1 #make note of this fact
n += 1 #and treat the tournament as having one more competitor
end if
dim schd(n)
for i = 1 to n
schd[i] = i #initial population of the array with numbers 1-n
next i
for r = 1 to n-1
print "Round "; rjust(string(r), 2); ": ";
for i = 1 to n/2 #print the pairings according to the scheme
#1 2 3 4
#5 6 7 8
print "("; nob(n,schd[i],byes); " -"; nob(n,schd[i+n/2],byes); " ) ";
next i
print
#now move positions 2-n around clockwise
temp1 = schd[n/2] #need to track two temporary variables
temp2 = schd[n/2+1]
for i = n/2 to 3 step -1 #top row
schd[i] = schd[i-1]
next i
for i = n/2+1 to n-1 #bottom row
schd[i] = schd[i+1]
next i
schd[n] = temp1 #end ifll in the ones that "jumped" between rows
schd[2] = temp2
next r
end subroutine
function nob( n as uinteger, i as uinteger, bye as boolean ) as string
'helper function to allow byes to be printed intelligently
dim as string pad
if n > 9 then pad = " " else pad = ""
if n = i and bye then
return pad+"B"
else
if i<10 then return pad + str(i) else return str(i)
end if
end function
sub roundrob( byval n as uinteger )
dim as boolean bye = false
if n mod 2 = 1 then 'if there is an odd number of competitors
bye = 1 'make note of this fact
n += 1 'and treat the tournament as having one more competitor
end if
dim as uinteger schd(1 to n), r, i, temp1, temp2
for i = 1 to n
schd(i) =i 'initial population of the array with numbers 1-n
next i
for r = 1 to n-1
print using "Round ##: ";r;
for i = 1 to n/2 'print the pairings according to the scheme
'1 2 3 4
'5 6 7 8
print using "(& - &) ";nob(n,schd(i),bye);nob(n,schd(i+n\2),bye);
next i
print
'now move positions 2-n around clockwise
temp1 = schd(n/2) 'need to track two temporary variables
temp2 = schd(n/2+1)
for i = n/2 to 3 step -1 'top row
schd(i) = schd(i-1)
next i
for i = n/2+1 to n-1 'bottom row
schd(i) = schd(i+1)
next i
schd(n) = temp1 'fill in the ones that "jumped" between rows
schd(2) = temp2
next r
end sub
print "Twelve teams"
roundrob(12)
print "Nine teams with byes"
roundrob(9)
Print "Twelve teams"
Proc _Roundrob(12)
Print
Print "Nine teams with byes"
Proc _Roundrob(9)
End
_Roundrob
Param (1)
Local (5)
b@ = 0
' if there is an odd number of competitors
If a@ % 2 = 1 Then b@ = 1 : a@ = a@ + 1
' make note of this fact and treat the tournament
For d@ = 1 To a@ ' as having one more competitor
@(d@) = d@ ' initial population of the array with numbers 1-n
Next
For c@ = 1 To a@-1 ' print the pairings according to the scheme
Print Using "Round __: ";c@;
' 1 2 3 4
For d@ = 1 To a@/2 ' 5 6 7 8
Print Show(Iif (a@ = @(d@) * b@, " ( B - ", Str(" (_# - ", @(d@))));
Print Show(Iif (a@ = @(d@+a@/2) * b@, " B) ", Str("_#) ", @(d@+a@/2))));
Next
Print ' now move positions 2-n around clockwise
e@ = @(a@/2) ' need to track two temporary variables
f@ = @(a@/2+1)
' top row
For d@ = a@/2 To 3 Step -1
@(d@) = @(d@-1)
Next
' bottom row
For d@ = a@/2+1 To a@-1
@(d@) = @(d@+1)
Next
@(a@) = e@ ' fill in the ones that "jumped" between rows
@(2) = f@
Next
Return
print "Twelve teams"
roundrob(12)
print "Nine teams with byes"
roundrob(9)
end
sub nob$(n,i,byes)
//helper sub to allow byess to be printed intelligently
//dim as string pad
if n > 9 then pad$ = " " else pad$ = "" : fi
if n = i and byes then
return pad$+"B"
else
if i < 10 then return pad$+str$(i) else return str$(i) : fi
fi
end sub
sub roundrob(n)
byes = 0
if mod(n, 2) = 1 then //if there is an odd number of competitors
byes = 1 //make note of this fact
n = n+1 //and treat the tournament as having one more competitor
fi
dim schd(n)
//, r, i, temp1, temp2
for i = 1 to n
schd(i) = i //initial population of the array with numbers 1-n
next i
for r = 1 to n-1
print "Round ", r using "##", ": ";
for i = 1 to n/2 //print the pairings according to the scheme
//1 2 3 4
//5 6 7 8
print "(", nob$(n,schd(i),byes), " -", nob$(n,schd(i+n/2),byes), " ) ";
next i
print
//now move positions 2-n around clockwise
temp1 = schd(n/2)//need to track two temporary variables
temp2 = schd(n/2+1)
for i = n/2 to 3 step -1 //top row
schd(i) = schd(i-1)
next i
for i = n/2+1 to n-1 //bottom row
schd(i) = schd(i+1)
next i
schd(n) = temp1 //fill in the ones that "jumped" between rows
schd(2) = temp2
next r
end sub
You may also check:How to resolve the algorithm Sparkline in unicode step by step in the Elixir programming language
You may also check:How to resolve the algorithm Zero to the zero power step by step in the CLU programming language
You may also check:How to resolve the algorithm Cumulative standard deviation step by step in the JavaScript programming language
You may also check:How to resolve the algorithm Repeat a string step by step in the Objective-C programming language
You may also check:How to resolve the algorithm Mad Libs step by step in the Icon and Unicon programming language