Question

In: Computer Science

This is a ruby program, thank you Given a set of random integer numbers in a...

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

Solutions

Expert Solution

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


Related Solutions

Ruby programming Create a Ruby program which reads numbers, deposits them into an array and then...
Ruby programming Create a Ruby program which reads numbers, deposits them into an array and then calculates the arithmetic mean (otherwise known as the average), the median (that is, the number that splits the set of value in half with half being above and half being below the number), and the standard deviation (that is, the average of distance each number is from the mean then squared). Further information about these calculations can be found here: average: http://en.wikipedia.org/wiki/Average (add up...
Write a program to produce an array of integer random numbers. Your program should find out...
Write a program to produce an array of integer random numbers. Your program should find out from the user how many numbers to store. It should then generate and store that many random integers (the random numbers must be between 1 and 999 inclusive). The program should then determine the smallest number, the largest number, and the average of all the numbers stored in the array. Finally, it should print out all the numbers on the screen, five numbers to...
Write a Java program to convert decimal (integer) numbers into their octal number (integer) equivalents. The...
Write a Java program to convert decimal (integer) numbers into their octal number (integer) equivalents. The input to the program will be a single non-negative integer number. If the number is less than or equal to 2097151, convert the number to its octal equivalent. If the number is larger than 2097151, output the phrase “UNABLE TO CONVERT” and quit the program. The output of your program will always be a 7-digit octal number with no spaces between any of the...
Write a Java program to convert decimal (integer) numbers into their octal number (integer) equivalents. The...
Write a Java program to convert decimal (integer) numbers into their octal number (integer) equivalents. The input to the program will be a single non-negative integer number. If the number is less than or equal to 2097151, convert the number to its octal equivalent. If the number is larger than 2097151, output the phrase “UNABLE TO CONVERT” and quit the program. The output of your program will always be a 7-digit octal number with no spaces between any of the...
A program for generating random numbers on a computer is to be tested. The program is...
A program for generating random numbers on a computer is to be tested. The program is instructed to generate 100 single-digit integers between 0 and 9. The frequencies of the observed integers were as follows. At the 0.05 level of significance, is there sufficient reason to believe that the integers are not being generated uniformly? Integer 0 1 2 3 4 5 6 7 8 9 Frequency 9 7 9 6 10 14 8 10 12 15 (a) Find the...
A program for generating random numbers on a computer is to be tested. The program is...
A program for generating random numbers on a computer is to be tested. The program is instructed to generate 100 single-digit integers between 0 and 9. The frequencies of the observed integers were as follows. At the 0.05 level of significance, is there sufficient reason to believe that the integers are not being generated uniformly? Integer 0 1 2 3 4 5 6 7 8 9 Frequency 9 7 9 6 10 14 8 10 12 15 (a) Find the...
A program for generating random numbers on a computer is to be tested. The program is...
A program for generating random numbers on a computer is to be tested. The program is instructed to generate 100 single-digit integers between 0 and 9. The frequencies of the observed integers were as follows. At the 0.05 level of significance, is there sufficient reason to believe that the integers are not being generated uniformly? Integer 0 1 2 3 4 5 6 7 8 9 Frequency 10 8 7 9 13 11 5 9 14 14 (a) Find the...
A program for generating random numbers on a computer is to be tested. The program is...
A program for generating random numbers on a computer is to be tested. The program is instructed to generate 100 single-digit integers between 0 and 9. The frequencies of the observed integers were as follows. At the 0.05 level of significance, is there sufficient reason to believe that the integers are not being generated uniformly? Integer 0 1 2 3 4 5 6 7 8 9 Frequency 12 9 7 7 12 12 6 8 12 15 (a) Find the...
its a c++ programme. In your program, you will randomly generate integer numbers between -50 and...
its a c++ programme. In your program, you will randomly generate integer numbers between -50 and +50 (including -50 and +50). You will repeat this process 1000 times. While generating these numbers, you need to count the numbers based on their signs separately, as positive and negative. For instance, if your program generate +25 15 times, and -25 19 times. Then this should be reported as, Num PosFre NegFre 25 15 19 For this problem, you need to use array...
Write a program that repeatedly generates a random integer in the range [1, 100], one integer...
Write a program that repeatedly generates a random integer in the range [1, 100], one integer at a time, and displays the generated numbers on the screen according to the following rules: If the second number generated is greater than the first number, they are displayed on the screen in the order of their input and while the next random number generated is greater than the previous one, the random number is displayed on the screen and the program continues....
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT