can someone please help me with this code, I have this but it not able
to execute. can you tell what is not right or any suggestions?..Thanks
here’s the initial question:
Write a statistical program in Ruby to:
a) Input the name of a data file from the command line arguments.
b) Input floating-point numbers from the data file.
c) Calculate and output the mean. This is the average, or the sum of
the
values divided by the number of values.
d) Calculate and output the median. This is the value such than half
the
values are larger and half are smaller. If you have an even number
of
values use either middle value or the average of the two middle
values.
code:
puts "This program was created by Thierry NY
puts “Project 5, November 10th 2010.”
puts "Program description: This is a statistical program to calculate
floating-
point numbers and output their mean and median.
nums = []
if ARGV[0] == nil
print “no argument detected, please enter filename.”
filename = gets.to_s.chomp
else
filename = ARGV[0]
end
f = open filename, “r”
while x = f.gets
nums << x.to_f
end
f.close
nums = nums.sort
count = nums.size -1
mean = 0
until count == -1
mean = mean + nums[count]
count = count -1
end
mean = mean/nums.size.to_f
if nums.size%2! = 0
midindex = (nums.size-1)/2
median = nums [midindex]
else
midindex1 = (nums.size-1)/2
midindex2 = (nums.size)/2
median = (nums[midindex1] + nums[midindex2])/
end
puts “The mean is: # {mean}”
puts “The median is: # {median}”