Question

a ruby program problem

****************************************************************************

Ruby These short language exercises are intended to help you to explore new languages with differing paradigms and syntax. In this assignment you will complete a Ruby program. I will give you help on where and what to look for in Ruby to assist you. Write a Ruby program to solve the following problem: Open a file that has a list of scores between 0 and 100. There may be as many as 100 scores in the file. The file is plain text and each score is on a separate line. Count the number of each score you find from 0 to 100. After you have counted all the scores, then print out a histogram using 10 buckets of size 10, that is, 0-9, 10-19,..., and 90-100. Then the histogram will print a for each score in the bucket. Example output Count17, Average 72.00, StdDev-15.07 0 . 10 . 20. 30. 40. 50 . 60 . 70 . 80.90.100 The input file scores.txt for this output looks like this 85 72 63 65 92 78 71 73 85 87 29 79 89 72 67 So, you read one score per line. Now you must use Ruby objects to solve this problem (you can do it without but solving the problem is not the point of this exercise). Create a class Histogram with the following methods: initialize(lowScore, hiscore, bucketsize) addScores (file) addScore(score) printWhere lowscore is the lowest score possible, hiscore is the highest possible, and bucketsize is the size of each bucket for the histogram. Using these will allow you to calculate the low and high limits of each bucket for building the histogram. file is the file name for a file that looks like the input above. score is just a single score from a test. The addscores can make use of the addscore method to read in the file scores into the object. The print method requires no parameters and just prints the statistics and histogram as above. You may add other methods as needed for your solution. I didnt need anymore since I kept a count, a sum and a sum of squares as I entered the scores so that I could easily compute the statistics. I used a Ruby array to keep the count in each bucket, which is flexible and dynamic. Explore Ruby and see what you can do to solve this program. Finally, here is what my program looks like after creating the Histogram class main h-Histogram.new h.addScores(scores.txt) h.print

0 0
Add a comment Improve this question Transcribed image text
Answer #1

compute.rb

# Note I have changed the name of the function "print" to "prints" because I have used

# ruby's print function inside prints function.

class Histogram

@lowScore # lowest possible Score in scores

@hiScore # highest possible score in scores

@bucketSize # size of each bucket

@count # count of each bucket

@Count # number of scores

@Average # average of scores

@StdDev # standard deviation of scores

def initialize(lowScore=0,hiScore=100,bucketSize=10) # initializer function

@lowScore = lowScore

@hiScore = hiScore

@bucketSize = bucketSize

@count = Array.new((hiScore-lowScore)/bucketSize){0}

@Count = 0

end

def addScores(file) # add Score function

total = 0

File.open("scores.txt").each do |score|

addScore(score.to_i) # count of each bucket

@Count += 1 # count of the number of scores

total += score.to_i # sum of scores

end

@Average = total.to_f/@Count # average of score

tmp = 0

File.open("scores.txt").each do |score|

tmp += ((score.to_i-@Average)**2/@Count.to_f)

end

@StdDev = tmp**0.5 # standard deviation of score

end

def addScore(score) # fnction to increament the count of each bucket

i = (score-@lowScore)/@bucketSize

@count[i] += 1

end

def prints # function to print

max = @count.max

puts "Count = %s, Average = %s, StdDev = %s"%[@Count,@Average.round(2),@StdDev.round(2)]

while max>0

for s in @count

if s>=max

print "%3s%1s"%["*"," "]

else

print "%4s"%[" "]

end

end

puts ""

max = max -1

end

i = @lowScore

while i<@hiScore

print "%3s%1s"%[i,"."]

i = i+@bucketSize

end

print @hiScore

end

end

h = Histogram.new

h.addScores("scores.txt")

h.prints

Add a comment
Know the answer?
Add Answer to:
a ruby program problem **************************************************************************** Ruby These short language exercises are intended to help you to...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • CIS 22A C++ Project Exam Statistics Here is what your program will do: first it welcomes...

    CIS 22A C++ Project Exam Statistics Here is what your program will do: first it welcomes the user and displays the purpose of the program. It then prompts the user to enter the name of an input file (such as scores.txt). Assume the file contains the scores of the final exams; each score is preceded by a 5 characters student id. Create the input file: copy and paste the following data into a new text file named scores.txt DH232 89...

  • What to submit: your answers to exercises 2. Write a Java program to perform the following...

    What to submit: your answers to exercises 2. Write a Java program to perform the following tasks: The program should ask the user for the name of an input file and the name of an output file. It should then open the input file as a text file (if the input file does not exist it should throw an exception) and read the contents line by line. It should also open the output file as a text file and write...

  • please use the c language Assignment 12 The program to write in this assignment is a...

    please use the c language Assignment 12 The program to write in this assignment is a program that calculates score statistics and manages the grades of the students. First, the student list and are given in a file named "student.dat" as in the following: 이성우 77 홍길동 88 scores 201 1710086 2012700091 This program reads the input file "student.dat" and keeps this data in a linear linked list. Each node must be a struct which contains the following: student id...

  • Implement a Java program using simple console input & output and logical control structures such that...

    Implement a Java program using simple console input & output and logical control structures such that the program prompts the user to enter 5 integer scores between 0 to 100 as inputs and does the following tasks For each input score, the program determines whether the corresponding score maps to a pass or a fail. If the input score is greater than or equal to 60, then it should print “Pass”, otherwise, it should print “Fail”. After the 5 integer...

  • Write python code on computer, No handwritten code You will implement a program which asks the...

    Write python code on computer, No handwritten code You will implement a program which asks the user to enter scores of different courses in different semesters. The program should read the input and output some simple statistics 1. An example input string by the user is given below. Fal12016-coursel:82, course2:80,course3:85;Spring2018- course4:82;Fall2018-course5:85, course6:50, course7:78 Info from different semesters are separated by a ";", courses in each semester are separated by a,and score and corresponding course is separated by a, information of...

  • Java programming help My program wont run. could someone tell me why. everything seems correct to...

    Java programming help My program wont run. could someone tell me why. everything seems correct to me... giving me the error: Exception in thread "main" java.lang.NumberFormatException: For input string: "Stud" at java.base/java.lang.NumberFormatException.forInputString(Unknown Source) at java.base/java.lang.Integer.parseInt(Unknown Source) at java.base/java.lang.Integer.parseInt(Unknown Source) at Util.readFile(Util.java:35) at Driver.main(Driver.java:8) _________________________________________________________________________________________________________________________ Program Prompt: Write a program to perform statistical analysis of scores for a class of students.The class may have up to 40 students.There are five quizzes during the term. Each student is identified by a four-digit...

  • Use your knowledge of algorithm development to create a program using RAPTOR to solve the problem...

    Use your knowledge of algorithm development to create a program using RAPTOR to solve the problem below. Input a list of student names (in to array student[]) and testing scores stored in parallel arrays. Each student have 3 testing scores ranging from 0 to 100 (store into parallel array score1[], score2[], score3[]). Output average score for each student and print out what is his final grade. If average score <60, got F grade. If 60<=average score<70, got D grade. If...

  • ANY HELP PLEASE. PLEASE READ THE WHOLE INSTRUCTION THANK YOU Write a program GS.java that will...

    ANY HELP PLEASE. PLEASE READ THE WHOLE INSTRUCTION THANK YOU Write a program GS.java that will be responsible for reading in the names and grades for a group of students, then reporting some statistics about those grades. The statistics to be gathered are the number of scores entered, the highest score and the name(s) of the student(s) who earned that score, the lowest score and the name(s) of the student(s) who earned that score, and the average score for the...

  • Please I need help in C language, I am trying to modify the code per the...

    Please I need help in C language, I am trying to modify the code per the below instructions, but I am getting errors. Can't fgure it out. The attempted code modification and data file is privided below, after the instructions. Thank you. Instructions:                  1.      First, add a function named printMsg that displays a message, a greeting, or an introduction to the program for the user. Add a statement that calls that function from the main function when your program starts....

  • This is a java homework for my java class. Write a program to perform statistical analysis...

    This is a java homework for my java class. Write a program to perform statistical analysis of scores for a class of students.The class may have up to 40 students.There are five quizzes during the term. Each student is identified by a four-digit student ID number. The program is to print the student scores and calculate and print the statistics for each quiz. The output is in the same order as the input; no sorting is needed. The input is...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT