Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

How can I change the end of the range to a variable so that the csv file can have as many entries as the user

How can I change the end of the range to a variable so that the csv file can have as many entries as the user wants without modifying the code. Here is my ruby code:

require 'csv'

puts "Student Assignment Average"

CSV.foreach("input.csv", col_sep: ',', row_sep: :auto, headers: true) do |row| #Establishing our local variables for this loop total_grade = 0 name = "" #Loop through rows with a range of 1..4 specifying how many columns starting at 1 and ending in 4 for grades (1 .. 4).each do |column_index| #Add a check for nil rows, if row is nil then skip this iteration if row[column_index] == nil next end #Grade equals current row index and converting it to integer grade = row[column_index].strip.to_i total_grade += grade #Name is set from row[0] the first column in the row name = row[0] end #Avg = total_grade/4.0 converting it to a float at the same time avg = total_grade/4.0 #Put name and average to output with a tab separating the columns puts "#{name}\t#{avg}" #End of loop, now it repeats for the remainder of the rows until done end

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Refactoring Databases Evolutionary Database Design

Authors: Scott Ambler, Pramod Sadalage

1st Edition

0321774515, 978-0321774514

More Books

Students also viewed these Databases questions

Question

manageremployee relationship deteriorating over time;

Answered: 1 week ago