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

languages = [‘English’, ‘German’, ‘Ruby’]

languages.each do |lang|

puts 'I love ’ + lang + ‘!’

puts ‘Don’t you?’

end

puts ‘And let’s hear it for C++!’

puts ‘…’

I have the feeling it has something to do with that? But I wouldn’t know

how to write it out.

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

Ignore the first total = 0

Jayanth

# define average

class Array

def avg

return self.inject{|num, sum| sum + num} / self.size

end

# random numbers - 8 here

array = (1…8).map{|num| rand(10000)}

# print min max and average

puts “Min:” + array.min.to_s + " Max:" + array.max.to_s + " Avg:" +

array.avg.to_s

Blog: http://random8.zenunit.com/

Learn: http://sensei.zenunit.com/

Twitter: http://twitter.com/random8r

Thanks everyone just what I was looking for.

Ok after trying to write it this is what I got. I get a

28: syntax error, unexpected $end, expecting kEND

Exit code: 1 error though and I can’t fix it any help

thanks

nums = Array.new(8) { rand(10000) }

max = 0

min = 10001

nums.each do |num|

if num > max

num = max

end

nums.each do |num|

if min < num

num = min

end

nums.each do |num|

sum += num

end

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

nums = Array.new(8) { rand(10000) }

max = 0

min = 10001

nums.each do |num|

if num < min

min = num

end

if num > max

max = num

end

end

sum =

puts nums

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

After more fiddling i fixed the error (needed another end for the if

statement DUH) and it works but I can’t figure out sum if you do sum +=

nums it gives an error.

nums = Array.new(8) { rand(10000) }

max = 0

min = 10001

nums.each do |num|

if num < min

min = num

end

if num > max

max = num

end

end

/*sum += nums*/

sum = 0

puts nums

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

On Tue, May 5, 2009 at 13:46, Taylor L. [email protected] wrote:

After more fiddling i fixed the error (needed another end for the if

statement DUH) and it works but I can’t figure out sum if you do sum +=

nums it gives an error.

It’s easier for people to help you if you show the error you are seeing.

In this particular case, since you are not initializing sum, the first

time the program encounters:

sum += num

you are effectively doing:

sum = nil + num

Something like this:

nums = Array.new(8) { rand(10000) }

max = 0

min = 10001

sum = 0

Also, in this case it’s irrelevant, but you might wish to initialize

min and max like this instead:

min = max = nums[0]

Marcelo

Yea I fixed the program. Thanks for your help everyone.

Taylor L. 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.