T
Taylor Lodge
I've just started using ruby for a programming/math class I'm doing at
school. Was asked to write a program that finds sum min max and avge of
a random set of numbers. Have managed to write that program but it is
overly complicated. What I was wondering is if I can store this
if
max < num1
max = num1
end
if
num1 < min
min = num1
end
(repeated 8 times)
code into something that's run once but read the variables from an array
so I don't have to keep typing it and adding it in if I feel like adding
more numbers.
If I haven't explained myself that well I'll clarify if you need it.
Thanks
Here's the full code
num1 = rand(10000)
num2 = rand(10000)
num3 = rand(10000)
num4 = rand(10000)
num5 = rand(10000)
num6 = rand(10000)
num7 = rand(10000)
num8 = rand(10000)
max = 0
min = 10001
puts num1
puts num2
puts num3
puts num4
puts num5
puts num6
puts num7
puts num8
if
max < num1
max = num1
end
if
max < num2
max = num2
end
if
max < num3
max = num3
end
if
max < num4
max = num4
end
if
max < num5
max = num5
end
if
max < num6
max = num6
end
if
max < num7
max = num7
end
if
max < num8
max = num8
end
if
num1 < min
min = num1
end
if
num2 < min
min = num2
end
if
num3 < min
min = num3
end
if
num4 < min
min = num4
end
if
num5 < min
min = num5
end
if
num6 < min
min = num6
end
if
num7 < min
min = num7
end
if
num8 < min
min = num8
end
sum = num1 + num2 + num3 + num4 + num5 + num6 + num7 + num8
avge = sum / 8s
puts
puts 'sum = ' + sum.to_s + '.'
puts
puts 'max = ' + max.to_s + '.'
puts
puts 'min = ' + min.to_s + '.'
puts
puts 'Average = ' + avge.to_s + '.'
puts
$end
Attachments:
http://www.ruby-forum.com/attachment/3654/Sum_max_min_avge.rb
school. Was asked to write a program that finds sum min max and avge of
a random set of numbers. Have managed to write that program but it is
overly complicated. What I was wondering is if I can store this
if
max < num1
max = num1
end
if
num1 < min
min = num1
end
(repeated 8 times)
code into something that's run once but read the variables from an array
so I don't have to keep typing it and adding it in if I feel like adding
more numbers.
If I haven't explained myself that well I'll clarify if you need it.
Thanks
Here's the full code
num1 = rand(10000)
num2 = rand(10000)
num3 = rand(10000)
num4 = rand(10000)
num5 = rand(10000)
num6 = rand(10000)
num7 = rand(10000)
num8 = rand(10000)
max = 0
min = 10001
puts num1
puts num2
puts num3
puts num4
puts num5
puts num6
puts num7
puts num8
if
max < num1
max = num1
end
if
max < num2
max = num2
end
if
max < num3
max = num3
end
if
max < num4
max = num4
end
if
max < num5
max = num5
end
if
max < num6
max = num6
end
if
max < num7
max = num7
end
if
max < num8
max = num8
end
if
num1 < min
min = num1
end
if
num2 < min
min = num2
end
if
num3 < min
min = num3
end
if
num4 < min
min = num4
end
if
num5 < min
min = num5
end
if
num6 < min
min = num6
end
if
num7 < min
min = num7
end
if
num8 < min
min = num8
end
sum = num1 + num2 + num3 + num4 + num5 + num6 + num7 + num8
avge = sum / 8s
puts
puts 'sum = ' + sum.to_s + '.'
puts
puts 'max = ' + max.to_s + '.'
puts
puts 'min = ' + min.to_s + '.'
puts
puts 'Average = ' + avge.to_s + '.'
puts
$end
Attachments:
http://www.ruby-forum.com/attachment/3654/Sum_max_min_avge.rb