In: Computer Science
This is a ruby program, thank you Given a set of random integer numbers in a user-defined range, count the number of occurrences of each integer, and generate a histogram using # characters. The input and output of your program should match the following in format:
$ ./histogram.rb
How many random integer numbers do you want to generate?
10
Please input the maximum value for the random numbers:
9
The frequency of 1 | #
The frequency of 2 |#
The frequency of 3 |###
The frequency of 4 |#
The frequency of 6 |##
The frequency of 7 |###
The frequency of 8 |##
The frequency of 9 |####
Prompt the user to input the number of random integers to generate. Prompt the user to input the maximum value for the random integers. The minimum will always be 1 and the numbers generated should include the maximum value. Use the rand method available in Kernel to generate random numbers in a given range. The documentation for Random#rand is a bit more detailed than Kernel#rand, but it is the same method. Force the user input to be read from the line following the prompt in both cases. You must use an Array of the appropriate size to store the randomly generated numbers. In other words, if they ask for 50 random numbers, size the Array at 50. You must use a hash to store the frequency of each random number that is generated. HINT: Use a counting Hash. Do not generate lines in the histogram for numbers that are not found in the Array; note that there are no lines shown below for 5, 10, 16, and others. The numbers represented in the histogram should be in sorted order (low to high) when displayed. You must use an iterator method (which requires a code block) at least once in the program to iterate over the Array or the Hash (or both).
This is a ruby program, thank you
Code for “historgram.rb” is as follows:
#input the random integers for generation
puts "How many random integer numbers do you want to generate?"
numberIndex = gets.chomp.to_i
#input the maximum value of integer
puts "Please input the maximum value for the random numbers:"
maxNumber = gets.chomp.to_i
#Array of numbers to store values generated by random number utility
numbers = Array.new(numberIndex)
#generate the random numbers and store the values in array
for i in 0..numberIndex-1
numbers[i] = rand(maxNumber) + 1 #added one to include the maximum number as well
end
#declare a hash to store numbers as key and their occurrence as value
number_histogram = Hash.new(0)
#iterate array and update the value of hash, key would be number and value would be frequency
numbers.each { |index| number_histogram[index]+=1 }
# sorting the hash based on key
number_histogram = number_histogram.sort{|a,b| a[0]<=>b[0]}
#iterate the hash to print the desired output
number_histogram.each do |key,value|
output = "The frequency of"
output+= key.to_s
output += "|"
#for each occurrence, append the #
for index in 0..value-1
output+= ("#")
end
#print the output for each number in hash
puts output
end
output:
Screen shot of the code for your reference:
In case of any query, do comment.
Please rate answer. Thanks