From: Brian Candler Date: 2009-05-05T19:18:03+09:00 Subject: Re: Min max program 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. Use this as a starting point: nums = Array.new(8) { rand(10000) } Or perhaps you would prefer this: nums = [] 8.times { nums << rand(10000) } Then you can iterate using nums.each do |num| .. do something with num end You can fold your existing code into that loop, e.g. sum += num if num < min min = num end if num > max max = num end HTH, Brian. -- Posted via http://www.ruby-forum.com/.