1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
|
#!/usr/bin/env ruby
require 'pp'
BIG_NUMBER = 32768
# Get data from a file, turn it in to a float, and find the max
data = File.readlines('random.txt')
max = -BIG_NUMBER
for n in (0 ... data.length)
data[n] = data[n].chomp.to_f
if data[n] > max
max = data[n].to_i + 1 # max is ceil(max(data[n]))
end
end
# Create the empty histogram
histogram = []
for n in (0 ... max)
histogram.push(0)
end
# Fill the histogram
for n in (0 ... data.length)
histogram[data[n].to_i] += 1
end
# Print the histogram
pp histogram
puts
for n in (0 ... max)
puts "*" * histogram[n]
end |