On Nov 10, 2009, at 6:56 PM, Rick Barrett wrote:

> I have a homework assignment where I have to convert an inputted  
> integer
> into Roman Numerals. I was doing fine building upon the previous steps
> until the arrays came in. We have to use the format/guidelines the
> professors give us or we get points counted off. I've looked at so  
> many
> ways to do this program through Google, but none of the examples I've
> seen are even close to what the professor wrote out.

Well, as you might have guessed, most of us on the list don't want to  
*DO* your homework for you, but will give you a bit of guidance to  
help to find your own way.

>
> Basically, in my code I have to use the array $numerals and create  
> pairs
> with the integer and its corresponding Roman Numeral.
>
> What I can't figure out how to do is say something like...
>
> For each 10 of the inputted number put an X
>
> I was thinking something along the lines of dividing the inputted  
> number
> by the decimal and taking the remaining integer and multiplying the
> Roman Numeral by that integer.

That should work.

>
> Like 30/10 = 3 so "X" * 3 would print out XXX

Yeah, there you go.

>
> But I have no idea how to do that with the $numerals array.
>
> How do I get my program to say divide the inputted number by the  
> decimal
> part of the pair and then give the numeral part that many times?
>
> def roman_numeral(number)
>
> $numerals = [[10, "X"],
>      [5, "V"],
>      [1, "I"]]

I assume this is representative of the array your professor gives.

Since it is global, you should define it outside of your method.

>
> result = ""
>
>  $numerals.each() do |decimal, numeral|

this seems like a good start

so if decimal is greater than number, you need to use numeral based on  
the quotient you identified earlier.

quotient = number / decimal

>  result = new_variable

You want to tack on a new bit of roman numeral here, so look at  
String#<<

result << new_variable

and, before you loop to the next pair, you need to account for the  
fact that number has been partially expressed in numerals.

>  end
>
>  return result
> end
>
> puts "Enter a number to be converted to Roman Numerals."
> my_number = gets().chomp().to_i()
> puts roman_numeral(my_number)
> -- 
> Posted via http://www.ruby-forum.com/.
>


Think about how you might have explained the steps to an 8 year old.  
(knowing addition, subtraction, and ordering, but not division) Does  
that give you a way to think about the algorithm?

Good luck,

-Rob

P.S. I saw Gennady's post, but I'd argue that no one would really want  
to solve the problem that way. And Nik's way doesn't give any help  
with the non-decimal V, L, or D.

Rob Biedenharn		http://agileconsultingllc.com
Rob / AgileConsultingLLC.com