Individual: Write a Ruby Program Due Sep 10, 11:59 PM Not Submitted POINTS 20 Pr
ID: 3745105 • Letter: I
Question
Individual: Write a Ruby Program Due Sep 10, 11:59 PM Not Submitted POINTS 20 Project 3 Objectives: 4.2 Instructions Assignment Files Grading Write a Ruby program named formatfile.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, assignment1, assignment 2, assignment 3, assignment 4 John Adams, 90, 91, 99, 98 Paul Newman 90, 92,93,94 Mary Smith, 95,96,98,99 Be careful to follow the output format exactly, including spacing. The output of your program must look like the following: Student GPA John Adams 99.5 Compress your files into a ZIP file. Submit the ZIP file to the Assign
Explanation / Answer
1)Save the below script as fileformat.rb
#fileformat.rb
#inut and output file
input_file = 'input.csv'
output_file = 'output.csv'
in_file = File.new(input_file, 'r')
out_file = File.new(output_file,'w')
count = 0
print "Student GPA "
out_file.write("Student GPA ")
#looping through file
in_file.each_line(" ") do |row|
columns = row.split(",")
# skipping the header
if count == 0
count = count + 1
next
end
total = 0
# looping through array
for index in 1 ... columns.size
# adding marks to total
mark = columns[index].strip().to_i
total = total + mark
end
#getting avg
avg = total.to_f/4.0
name = columns[0]
# printing to console and writing to file
print "#{name} #{avg} "
out_file.write("#{name} #{avg} ")
end # end of script
#----------------------------------------------------------------------------------------
2)Create a file name input.csv and put the below content(in bold) in file and save also put the below file in same directory where you save the above the script
Student Name, assignment1, assignment 2, assignment 3, assignment 4
John Adams, 90, 91, 99, 98
Paul Newman, 90, 92,93,94
Mary Smith, 95,96,98,99
3) Run the script as ruby fileformat.rb
$:ruby fileformat.rb
Student GPA
John Adams 94.5
Paul Newman 92.25
Mary Smith 97.0
A file will also be generated in same directory namely output.csv