Reputation: 3
PLAYERS = 4
def input(noteam, arraya)
for x in 0..noteam-1
for y in 0..PLAYERS-1
arraya[x] = Array.new(PLAYERS)
puts "Team " + (x+1).to_s
arraya[x][y] = read_integer("Enter score for individual player " + (y+1).to_s)
end
end
end
def disp_arr(myarray, row, col)
print myarray[0]
for x in 0..row-1
for y in 0..col-1
print myarray[x][y]
end
end
end
def main
notm = read_integer("How many teams:")
arraya = Array.new(notm)
input(notm, arraya)
disp_arr(arraya, notm, PLAYERS)
end
main #=> invoking the main method
So I tried doing print myarray[0] for the first row and [nil,nil,nil,10] comes out so I know that there's a problem with assigning the value into the array but I'm not too sure whats wrong with it.
Upvotes: 0
Views: 40
Reputation: 32445
Move line arraya[x] = Array.new(PLAYERS)
out of internal for
loop
def input(noteam, arraya)
for x in 0..noteam-1
arraya[x] = Array.new(PLAYERS) # <-
puts "Team #{x + 1}"
for y in 0..PLAYERS-1
puts "Player: #{y + 1}"
arraya[x][y] = read_integer("Enter score for individual player " + (y+1).to_s)
end
end
end
Your original code reset arraya[x]
to new array for every team player except last.
That is why you observing [nil, nil, nil, 45]
result, because only last player results being saved.
Upvotes: 1