im new... so can i have some help

D

Daniel Kindler

im new to ruby...... and i need some help!
im trying to make a quick app that takes an array of #'s
puts them into a specific order ( i thik ill use the method "list" for
that?"
and gets the mean, median and mode of the array of numbers?
how would i get what the user enters and puts it in
this is what i got so far



array = {}
num = gets.to_i
while num != 000



*please note the "while num != 000" is for when the user enters 000, the
program knows when to stop adding numbers to the array, and to get the
mean median and mode



than u
 
G

Glen Holcomb

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

im new to ruby...... and i need some help!
im trying to make a quick app that takes an array of #'s
puts them into a specific order ( i thik ill use the method "list" for
that?"
and gets the mean, median and mode of the array of numbers?
how would i get what the user enters and puts it in
this is what i got so far



array = {}
num = gets.to_i
while num != 000



*please note the "while num != 000" is for when the user enters 000, the
program knows when to stop adding numbers to the array, and to get the
mean median and mode



than u
Is this a homework assignment? while num != 000 is not going to do what you
expect. It will catch 0, 00, 000, 0000, 00000... as to_i converts those to
0.

also you would want to write it as

while num != 000
num = gets.to_i
array << num
end
 
A

Axel Etzold

Dear Daniel.
im new to ruby...... and i need some help!

welcome to Ruby. You'll find help on this list :)
im trying to make a quick app that takes an array of #'s
puts them into a specific order ( i thik ill use the method "list" for
that?"

Use sort for that ( http://www.ruby-doc.org/core/classes/Array.html#M002208)
and gets the mean, median and mode of the array of numbers?

There's Array#inject for summing, median is the middle of the sorted Array,
and modes are elements that are equal to Array#max.

I'd suggest you go through Chris Pine's little tutorial for Ruby:

http://pine.fm/LearnToProgram/

or, if you have some love for comics ... and more time ...

http://poignantguide.net/ruby/

to get an impression of the language.
Also, there are many quizzes for Ruby with different solutions to a problem:

http://rubyquiz.com/

And of course, many excellent books, and readily written software packages (in your case,
search e.g., for the statistics2 package on the RAA (Ruby Application Archive).

Best regards,

Axel
 
D

Daniel Kindler

ok this is what i wrote so far



array = []
num = gets.to_i
while num != 000
num = gets.to_i
array << num
array.sort
end





why wont if list out the numbers?
 
D

Daniel Kindler

Daniel said:
ok this is what i wrote so far



array = []
num = gets.to_i
while num != 000
num = gets.to_i
array << num
array.sort
end





why wont if list out the numbers?

nvm i relized i didnt add the "puts"
but it comes out like this


2
5
6
8
9




how do i make it so its in one line?
 
G

Glen Holcomb

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

Daniel said:
ok this is what i wrote so far



array = []
num = gets.to_i
while num != 000
num = gets.to_i
array << num
array.sort
end





why wont if list out the numbers?

nvm i relized i didnt add the "puts"
but it comes out like this


2
5
6
8
9




how do i make it so its in one line?
If you want the array data as a string (i.e. one line) you can use the join
method:

puts array.join(' ') would print the numbers in the array separated by
spaces
puts array.join(',') would separate the elements with a comma

You can separate them with whatever you want, it can even be multiple
characters.
 
D

Daniel Kindler

ok, so here is my code




array = []
num = gets.to_i
while num != 0
num = gets.to_i
array << num
end
puts array.sort.join(',')



works well so far




Now, how would i take the numbers in the array, and add them......
 

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

Members online

Forum statistics

Threads
474,202
Messages
2,571,055
Members
47,658
Latest member
jaguar32

Latest Threads

Top