Min max program

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
 
T

Taylor Lodge

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.
 
S

Srijayanth Sridhar

[Note: parts of this message were removed to make it a legal post.]

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
 
S

Srijayanth Sridhar

[Note: parts of this message were removed to make it a legal post.]

Ignore the first total = 0

Jayanth

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
 
B

Brian Candler

Taylor said:
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.
 
T

Taylor Lodge

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
 
T

Taylor Lodge

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
 
M

Marcelo

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,071
Latest member
MetabolicSolutionsKeto

Latest Threads

Top