In: Computer Science
The college IT department manager no longer wants to use spreadsheets to calculate grades. The manager has asked you to create a program that will input the teachers' files and output the students' grades.
Write a Ruby program named format file.rb, which can be run by typing ruby widgets.rb.
In your Ruby environment, the program must read an input file formatted in CSV format, named input.csv. Each record contains data about a student and their corresponding grades.
The data will look similar to the following:
Student Name, assignment 1, assignment 2, assignment 3, assignment 4
John Adams, 90, 91, 99, 98
Paul Newman, 90, 92, 93, 94
Mary Smith, 95, 96, 99
Be careful to follow the output format exactly, including spacing. The output of your program must look like the following:
Student Assignment Average
John Adams 94.5
Code:
require 'csv'
# Reading the userdata.csv file into an CSV Table object
student_records = CSV.read("input.csv")
# Removing the header from the CSV Table
student_records.shift
# Printing the output headers
print("Student Assignment Average\n")
# Iterating over student records
student_records.each do
|student_data|
# Storing the student name
student_name = student_data[0]
# Converting the student marks from string to int using map
student_marks = student_data[1,student_data.size].map {|val| val.to_i}
# Calculating the average by dividing the sum of the array with size of the array(type casted to float to get floating result)
student_average = student_marks.sum / student_marks.size.to_f
# Printing the name and the average rounded to 1 decimal point
print("#{student_name} #{'%.1f' % student_average}\n")
end
Code Screenshot:

input.csv contents:
Student Name, assignment 1, assignment 2, assignment 3, assignment 4 John Adams, 90, 91, 99, 98 Paul Newman, 90, 92, 93, 94 Mary Smith, 95, 96, 99 Steve Wozniak, 91, 75, 86, 92 Russell Norvig, 85, 92, 95, 95
Output:
