[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Min max program

Taylor Lodge

5/5/2009 9:25:00 AM

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-...attachment/3654/Sum_max_m...

--
Posted via http://www.ruby-....

10 Answers

Taylor Lodge

5/5/2009 9:27:00 AM

0

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.
--
Posted via http://www.ruby-....

Srijayanth Sridhar

5/5/2009 9:42:00 AM

0

[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



On Tue, May 5, 2009 at 2:54 PM, Taylor Lodge <ubermouse@live.com> 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-...attachment/3654/Sum_max_m...
>
> --
> Posted via http://www.ruby-....
>
>

Srijayanth Sridhar

5/5/2009 9:44:00 AM

0

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

Ignore the first total = 0

Jayanth

On Tue, May 5, 2009 at 3:12 PM, Srijayanth Sridhar <srijayanth@gmail.com>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 <ubermouse@live.com> 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-...attachment/3654/Sum_max_m...
> >
> > --
> > Posted via http://www.ruby-....
> >
> >
>

Julian Leviston

5/5/2009 9:51:00 AM

0

# 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.ze...
Learn: http://sensei.ze...
Twitter: http://twitter.co...

On 05/05/2009, at 7:24 PM, Taylor Lodge <ubermouse@live.com> 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-...attachment/3654/Sum_max_m...
>
> --
> Posted via http://www.ruby-....
>

Brian Candler

5/5/2009 10:18:00 AM

0

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

Taylor Lodge

5/5/2009 7:22:00 PM

0

Thanks everyone just what I was looking for.
--
Posted via http://www.ruby-....

Taylor Lodge

5/5/2009 7:37:00 PM

0

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

--
Posted via http://www.ruby-....

Taylor Lodge

5/5/2009 7:46:00 PM

0

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

--
Posted via http://www.ruby-....

Marcelo

5/5/2009 8:07:00 PM

0

On Tue, May 5, 2009 at 13:46, Taylor Lodge <ubermouse@live.com> 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

Taylor Lodge

5/5/2009 9:36:00 PM

0

Yea I fixed the program. Thanks for your help everyone.
--
Posted via http://www.ruby-....