From: Srijayanth Sridhar Date: 2009-05-05T18:44:08+09:00 Subject: Re: Min max program --001e680f1a4caac3e60469271e28 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit Ignore the first total = 0 Jayanth On Tue, May 5, 2009 at 3:12 PM, Srijayanth Sridhar wrote: > Hello, > > # Ruby has built in methods for max and min > # Not sure if you are allowed to use it > # Assuming you are not > > numbers = [] > total = 0 > min, max = nil > total = 0.0 > 8.times do > new_num = rand(10000) > numbers.push(new_num) > # ideally you can check for min and max here itself > min = new_num if min == nil or new_num < min > max = new_num if max == nil or new_num > max > total = total+new_num > end > > average = total/numbers.size() > puts numbers.join(',') > puts max > puts min > puts average > > > Jayanth > > > > On Tue, May 5, 2009 at 2:54 PM, Taylor Lodge wrote: > > > 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 > > > > -- > > Posted via http://www.ruby-forum.com/. > > > > > --001e680f1a4caac3e60469271e28--