I am trying to sort a multidimensional array, but am not sure if this is the correct way to go about it. So far, I am getting my 5 numbers in the multidimensional array and then moving them into a single dimensional array and using array sort. Does know a better way ? or have ideas on how to improve mine? Also , the code currently isn’t working in the sorting area, it gives me an index out of array error.
Any help will be appreciated. Thanks in advance
Module q
Sub Main()
Randomize()
Dim Player,RandomNumber,NumberOfPlayers,Index As Integer
Dim Roll as Integer = 0
Console.Write("How many people will be playing Yahtzed?: ")
Player = convert.toint32(Console.Readline)
NumberOfPlayers = Player
Dim Game(Player,5) As Integer
Do until Player = 0
Console.Write("User")
Roll = 0
Do until Roll = 5
RandomNumber = CINT(Int((6 * Rnd()) + 1))
Game(Player,Roll) = RandomNumber
Roll += 1
Console.Write(" "&RandomNumber)
Loop
Player -= 1
Console.Writeline()
Loop
Player = NumberOfPlayers
Do until Player = 0
Dim Ordering(5) as Integer
Roll = 0
Do until Roll = 5
Ordering(Index) = Game(Player,Roll)
Roll += 1
Index += 1
Array.Sort(Ordering)
Loop
Loop
End Sub
End Module
The idiomatic way of iterating arrays is done using a For-statement
The array indexes always go from
0toarraysize-1.The size of your Ordering array would be
NPlayers * NRollsYours is too short. Therefore you get an exception.
Integer random numbers can be created using the
RandomclassThis creates random numbers between 0 and 10. Create the randomizer only once and then get the next random number by calling the
Nextmethod.(You must put the
Nextmethod between brackets, becauseNextis a keyword in VB.)UPDATE
I assume that you want to sort the rolls per player. The easierst way to accomplish this is to use a jagged array instead.
But the you will have to access the rolls with
game(p)(r)instead ofgame(p,r).