vendredi 8 mars 2019

Ruby Iterating through 2D arrays and Populating with random data

I have to generate a dynamically sized 2D array with a predetermined value in the first index of each sub array, three random values in each of the three following indices (each falling in a different range), and finally, a calculated total of the three random indices. Here is what I have so far.

Sample code

print("Please enter the number of athletes competing in the triathalon: ")
field=gets.to_i

    count=1
    athlete = Array.new(5)
    triathalon = Array.new(field){athlete}
    triathalon.each do
            athlete.each do
                    athlete.insert(0,count)
                    athlete.insert(1,rand(30..89))
                    athlete.insert(2,rand(90..119))
                    athlete.insert(3,rand(120..360))
            #calculate total time per athlete
                    athlete.insert(4,athlete[1]+athlete[2]+athlete[3])
                    count+=1
            end
    end




Aucun commentaire:

Enregistrer un commentaire