Question

In: Computer Science

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 all the numbers in the set and divide that total by the quantity of values in the set)

median: http://en.wikipedia.org/wiki/Median
(for a set that holds an odd number of values, sort the numbers in the set and then take the value in the middle of the set. for a set that holds an even number of values, sort the numbers in the set and find the average of the two values in the middle of the set).

standard deviation: http://en.wikipedia.org/wiki/Standard_deviation
(find the average of the set. for each value in the set, calculate the difference between each value and the average. square each of these difference. find the average of these squared differences. take the square root of this average).

IN ORDER TO RECEIVE FULL CREDIT, YOUR CODE SHOULD PROPERLY READ VALUES, STORE THEM IN AN ARRAY AND MANIPULATE THE ARRAY TO ARRIVE AT THE DESIRED CALCULATIONS.

Hint : In order to have Ruby calculate the square root of a numeric value named i, please say squareRootOfI = Math::sqrt( i )

A number of different program dialogues describe the program I am looking for.

Math Calculator
[N]umbers [A]verage [M]edian [S]tandard Deviation [C]lear [Q]uit: N
2 4 4 4 5 5 7 9
[N]umbers [A]verage [M]edian [S]tandard Deviation [C]lear [Q]uit: A
Average = 5.0
[N]umbers [A]verage [M]edian [S]tandard Deviation [C]lear [Q]uit: M
Median = 4.5
N]umbers [A]verage [M]edian [S]tandard Deviation [C]lear [Q]uit: S
Standard Deviation = 2.0
[N]umbers [A]verage [M]edian [S]tandard Deviation [C]lear [Q]uit: C
[N]umbers [A]verage [M]edian [S]tandard Deviation [C]lear [Q]uit: N
4 4 4 6

[N]umbers [A]verage [M]edian [S]tandard Deviation [C]lear [Q]uit: N
6 3 1

[N]umbers [A]verage [M]edian [S]tandard Deviation [C]lear [Q]uit: A
Average = 4.0
[N]umbers [A]verage [M]edian [S]tandard Deviation [C]lear [Q]uit: M
Median = 4
[N]umbers [A]verage [M]edian [S]tandard Deviation [C]lear [Q]uit: S
Standard Deviation = 1.603
[N]umbers [A]verage [M]edian [S]tandard Deviation [C]lear [Q]uit: Q

Solutions

Expert Solution

Working code implemented in Ruby and appropriate comments provided for better understanding.

Source Code for Math_Calculator.rb:

STDOUT.sync = true
#squareRootOfI = Math::sqrt( i )

class Math_Calculator

def prompt
puts "[N]umbers [A]verage [M]edian [S]tandard Deviation [C]lear [Q]uit:"
@inputs1=gets.chomp
testing(@inputs1)
end

def testing(inputs)
case inputs
when "N" then inn
when "M" then median
when "A" then average
when "S" then deviation
when "C" then cleared
when "Q" then quit
else
prompt
end
end

def inn
@arr_inputs = gets.chomp
input = @arr_inputs.split(" ").map(&:to_i)
@avrg = (input.inject(:+)/(input.size).to_f)
input += input.map {@arr_inputs}
testing @arr_inputs
end

def median
medi = @arr_inputs.split(" ").map(&:to_i)
medium = medi.sort
medisize = medium.size
if medisize.even?
eve = (medisize) / 2
puts eve
@median = ((medium[eve-1] + medium[eve]) / 2.0)
else
odd = ((medisize/2).to_i)
puts odd
@median = medium[odd]
end
puts "The median is: #{@median}"
prompt
end

def average
puts "The average is: #{@avrg}"
prompt
end

def deviation
dev = @arr_inputs.split(" ").map(&:to_i);
sum = 0
i = 0
while i < dev.size
sum += (dev[i] - @avrg)**2
i += 1
end
squareRootOfI = Math::sqrt(sum/(dev.size))
puts "Standard Deviation: #{squareRootOfI}"
prompt
end

def cleared
@arr_inputs = []
prompt
end

def quit
@arr_inputs = []
prompt
end

def quit
exit
end
end

calcu = Math_Calculator.new
calcu.prompt

Code Screenshots:

Sample Output Screenshots:


Related Solutions

Write a program named FinalExamProgram2 that reads numbers from a file (which you will create using...
Write a program named FinalExamProgram2 that reads numbers from a file (which you will create using Notepad) into a one-dimensional array and then analyzes the numbers as described below. Your program must use loops to read the numbers into the array and to analyze the contents of the array. The program’s main function should do the following:  Read eight floating-point numbers from the file named numbers.txt into a onedimensional array, displaying each number on the screen.  Pass the...
c++ language Create a file program that reads an int type Array size 10; the array...
c++ language Create a file program that reads an int type Array size 10; the array has already 10 numbers, but your job is to resize the array, copy old elements of array to the new one and make it user input and add an additional 5 slots in the array, and lastly do binary search based on user input. close the file.
Write a program in c++ that reads x[4]. Then create the array y[6||6], such that the...
Write a program in c++ that reads x[4]. Then create the array y[6||6], such that the first quarter of y consists of the value of the first element of x. The second quarter of y consists of the value of the second element of x. The third quarter of y consists of the value of the third element of x. The fourth quarter of y consists of the value of the fourth element of x?
C++ programming language. Write a program that will read in id numbers and place them in...
C++ programming language. Write a program that will read in id numbers and place them in an array.The array is dynamically allocated large enough to hold the number of id numbers given by the user. The program will then input an id and call a function to search for that id in the array. It will print whether the id is in the array or not. Sample Run: Please input the number of id numbers to be read 4 Please...
Write a program that reads numbers from scanf1 (keyboard) and then sums them, stopping when 0...
Write a program that reads numbers from scanf1 (keyboard) and then sums them, stopping when 0 has been entered. Construct three versions of this program, using the while, do-while, and for loops.
Create a program that generates a file of random numbers, and then prints them in neat...
Create a program that generates a file of random numbers, and then prints them in neat fashion to another file, and also saves to that file the average and standard deviation of those numbers. I) First, you would need to generate a file of random numbers that consists of N random numbers (100 < N < 1000). Each random digit should be a real number (type double) between 0 and 50. This file and its digits would now serve as...
WITH using methods create an array with numbers
IN JAVA  Prompt 3:WITH using methods create an array with numbersint [] number = { 35, 68, 80, 34, 45, 79, 80};Display the numbers in the array using for loopDisplay the sumFind biggest element in the arrayDisplay the numers in the array with index numbersDisplay the numers using for loop in ascending order (sort)
Write a Java program that reads a list of integers into an array. The program should...
Write a Java program that reads a list of integers into an array. The program should read this array from the file “input.txt”. You may assume that there are fewer than 50 entries in the array. Your program determines how many entries there are. The output is a two-column list. The first column is the list of the distinct array elements; the second column is the number of occurrences of each element. The list should be sorted on entries in...
Write a C program that asks the user to enter 15 integer numbers and then store them in the array.
Write a C program that asks the user to enter 15 integer numbers and then store them in the array. Then, the program will find the second largest element in array and its index without sorting the array. For example, In this array {-55,-2,1, 2, -3, 0, 5, 9, 13, 1, 4, 3, 2, 1, 0}, the second largest element is 9 [found at index 7].
Ruby programming I'd like you to create the class Holiday as described below. An object of...
Ruby programming I'd like you to create the class Holiday as described below. An object of class Holiday represents a holiday during a calendar year. This class has three private member variables: - mName, a string, representing the name of the holiday - mDay, an integer, representing the day of the month of the holiday - mMonth, a string, representing the month the holiday is in Create this class in the file Holiday.rb. Put your main driver code in the...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT