Using Ruby Im having trouble writing this code for this prog
Using Ruby I\'m having trouble writing this code for this program
Write a program that will provide the following information (displayed on the screen):
1) The total number of purchase orders.
2) The Number of Purchase Orders over 1,500.
3) The average amount of Purchase Orders over 1,500
Present you output in some Tabular format. Something similar to:
Total Number of Purchase Orders: xxxx
Number of Purchase Orders over 1500: xxxx
Average of PO’s over 1500: xxxx.xx
I would attach the .csv file with the Purchase orders, but since Chegg doesn\'t support document uploading and since the file is so big copying over the text isn\'t an option either.
I have attached the code I currently have, but I think trying to work with the file as its own external source is making the project a bit more complicated than necessary. An alternative is to import the file into either an array or hash, and then assign variables to the quantities you need to answer the project\'s questions.
The file I have has two columns -- the PO# and the PO amount.
This slides into a hash very nicely, since the PO#\'s are consecutive (and therefore unique).
The PO# for each row can be the pair\'s key, and the PO amount can be the value.
Would this be possible to implement in my original code, or do I need to write a new program basically?
Code I want to use:
puts result
po.each {|key, value|
if value.to_f>1500
order_over_sum+=value.to_f
orderover+=1
end
}
-- I tried using the code below, but I keep getting this error and I\'m unsure of how to fix it --
Code I currently have:
# code to initialize variables
purchase_order=0
orderover1500=0
orderover1500_Sum=0
# code to read CSV file and convert the values as numeric
CSV.foreach(\'Purchase Orders.csv\', converters: :numeric) do |row|
# code to count orders
purchase_order=purchase_order+1
# code to get orders over 1500
if row[0]>1500
# code to coun the orders over 1500
orderover1500=orderover1500+1
# code to sum the orders over 1500
orderover1500_Sum=orderover1500_Sum+row[0]
end
end
# Code to display the result
result=\" Total Number of purchase order: %d\" %purchase_order
puts result
result=\"Number of Purchase Orders over 1500: %d\" %orderover1500
puts result
# code to compute average
avg=orderover1500_Sum / orderover1500
# Code to display the result
result=\"Average of PO’s over 1500: %.2f\" %avg
attached picture:
C: Users Alex Desktop assignment 3.rb C: Users Alex Desktop assignment rb:49 invalid multibyte char (UTF-8 C: Users Alex Desktop assignment rb:49 syntax error unexpected tIDENTIFIER, e xpecting end-of-input result \"Average of PO?s over 1500: 2.2f\" zaugSolution
#!/usr/bin/ruby
class Assignment3
@@purchase_order=0
@@Number orderover1500=0
@@Number orderover1500_Sum=0
# code to read CSV file and convert the values as numeric
#require \'csv\'
#order = CSV.read(\'PurchaseOrders.csv\')
# code to read CSV file and convert the values as numeric
CSV.foreach(\'Purchase Orders.csv\' , converters: :numeric) do |row|
# code to count orders
purchase_order=purchase_order+1
# code to get orders over 1500
if row[0]>1500
# code to coun the orders over 1500
orderover1500=orderover1500+1
# code to sum the orders over 1500
orderover1500_Sum=orderover1500_Sum+row[0]
end
end
# Code to display the result
T_no_PO=\" Total Number of purchase order: %d\", purchase_order
printf T_no_PO
PO_over=\"Number of Purchase Orders over 1500: %d\",orderover1500
printf PO_over
# code to compute average
avg=orderover1500_Sum / orderover1500
result_avg=\"Average of PO\\ \'s over 1500: %.2f\", avg
printf result_avg
end


