Hi, guys. I need help with a Program coded in Ruby. It is a programming assignme
ID: 3585355 • Letter: H
Question
Hi, guys. I need help with a Program coded in Ruby. It is a programming assignment but I don't know where to start.
Sorry for posting such a long question. Thanks
This assignment is designed to reinforce your understanding of elementary Ruby constructs such as the creation of variables, console input/output, data types, iteration, conditional expressions, simple arithmetic operations, string interpolation, Hashes, and general top-down program design.
You will create and process a Hash of key-value pairs where the keys are fictitious generated user names and the values are randomly-generated integers in the range 20 to 55 (inclusive). HINT: Look up the rand method in the Kernel module for an easy way to generate random integers in a given range.
The key-value pairs represent the user name and the home directory sizes in megabytes. The person running the program (either you while testing or me while grading) will provide the number of users to simulate. Validate that the number of users given is at least 10 and not greater than 35, and keep prompting until the person running the program enters a valid number.
Our fake user names will be user1, user2, … up to userN where N is the number the person running the program types in as a prompted response.
Once you have generated a hash of key-value pairs, one for each imaginary user, display the hash keys and a histogram of *'s representing the number of megabytes in the user's home directory. At the bottom of the report, add a line that shows how many were above an arbitrary threshold of your choice. It can be any number between 20 and 50 (inclusive on both ends), but you must store it and use it as the named constant QUOTA as opposed to a hard-coded number such as 35.
The following is an example run of the program with QUOTA set to 32 and with correct user input:
$ ./home_dir_report.rb
Enter a number between 10 and 35 to represent the number of users:
15
User Names and Directory sizes (* = 1 MB)
=========================================
user1 ****************************
user2 ********************
user3 ***************************************
user4 *************************
user5 *************************
user6 **********************
user7 ********************************
user8 **************************************
user9 ***************************
user10 ****************************************
user11 **************************
user12 ************************************************
user13 ****************************************
user14 ************************************
user15 *********************************
5 users have directories of at least 32 MB.
Here is a run in which the user does not initially enter a valid number. User responses are in bold font.
$ ./home_dir_report.rb
Enter a number between 10 and 35 to represent the number of users:
9
I said between 10 and 25. Try again:
1
I said between 10 and 25. Try again:
10
User Names and Directory sizes (* = 1 MB)
=========================================
user1 ****************************
user2 ********************
user3 ***************************************
user4 *************************
user5 *************************
user6 **********************
user7 ********************************
user8 **************************************
user9 ***************************
user10 ****************************************
3 users have directories of at least 32 MB.
Explanation / Answer
k=0
l=1
puts "Enter a number between 10 and 35 to represent the number of users:"
number=gets
counter=number.to_i
while k!=l do
if counter>=10 && counter<=35
k=l
else
puts "I said between 10 and 25. Try again:"
number=gets
counter=number.to_i
end
end
puts counter
i=k.to_i
details=Hash.new
r=Random.new
while i<counter do
j=1
string="num"+"#{j}"
details["#{string}"]=r.rand(20..55)
j=j+1
i=i+1
end
details.each do |key,value|
puts key + ' : ' + value.to_s
end
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.