# Assignment 1 - Roman Numeral Converter

### Summary

DUE DATE: 5:00pm Tuesday 15 January 2002 (extension announcement) 10% comp2004s.a1 automated testing and hand marking Write a program to convert integers to Roman numerals

### Detailed Description

• For this assignment you have to write a program which reads integers and outputs the corresponding Roman numerals.

• Roman numerals are an alternate system of representing integers. The following 7 symbols have the corresponding values:
SymbolValue
I1
V5
X10
L50
C100
D500
M1000
• These symbols are then arranged in various ways in order to represent all other integers.
• Each digit in an integer (in its base 10 representation) corresponds to a group of roman numeral symbols (or possibly none). These groups are written left to right without any spaces (in the same way as the digits of an integer are written left to right without any spaces). The rules for representing each digit are the same, except they involve different symbols, and thus each digit in an integer can be treated independantly of the others.
• For example, for digits in the "1"s (ones) column (ie. the rightmost digit of an integer), the following rules apply:
IntegerRoman numeral
1I
2II
3III
4IV
5V
6VI
7VII
8VIII
9IX
• Similar rules apply for all other digits, for example in the "10"s (tens) column (ie. the 2nd digit to the right of an integer):
IntegerRoman numeral
10X
20XX
30XXX
40XL
50L
60LX
70LXX
80LXXX
90XC
• Notice that this is the same as for 1 - 9, except that
• X (10) is used instead of I (1),
• L (50) instead of V (5), and
• C (100) instead of X (10).
• You then compose the various digits in order from left to right.
• For example, the integer 79 is "LXX" (70) followed by "IX" (9), ie. "LXXIX".
• For integers 4000 and over, just repeat as many M symbols as is neccessary.
• For example, 10000 is "MMMMMMMMMM".
• There is one exception to the above rules. The integer 4 is "IIII", not "IV" as one would expect. However, larger integers which happen to have 4 as their rightmost digit still use "IV". So 4 is "IIII", whereas 14 is "XIV" and not "XIIII", 24 is "XXIV" and not "XXIIII", and so on.

• Input is via standard input (cin).
• The first line contains a list of the symbols to be used in the conversion process.
• Normally this will be "IVXLCDM", but different symbols could be used, for example, "abcdefg". In this case, the Roman numerals output should use the symbols specified in the first line.
• For example, with the symbol set "abcdefg", the integer 1997 is "gegcebaa".
• Each symbol is a single non-whitespace character (ie. anything except space, newline or tab).
• If there are more or less than 7 symbols, then their values are as you would expect by extending the pattern of symbol values shown for Roman numerals.
• For example, if the list of symbols is "IVXLCDMKP", then the values of these symbols are:
SymbolValue
I1
V5
X10
L50
C100
D500
M1000
K5000
P10000
• There will always be at least 3 symbols.
• There will always be at most 19 symbols.
• The number of symbols will always be odd.
• There will be no spaces between any of the symbols.
• No symbol will appear more than once in the list of symbols (even though your code should be able to handle this).
• Situations other than those described above simply won't be tested, so it doesn't matter what your program does when the input doesn't match what's specified above. In other words, you can just assume that the input will be as described above, and you don't need to test that the input is valid.

• All subsequent lines contain exactly one integer which is to be converted into a Roman numeral. Since there is no way to represent the number 0 as a Roman numeral, you can assume that all input numbers are positive (ie. greater than 0).

• For each input integer you output the corresponding Roman numeral followed by a newline. You can output each Roman numeral "as you go" (ie. before reading the next integer), or you can read in all the integers and then output the Roman numerals after all the integers have been input. The machine marking doesn't care which way you do it. Although storing all the integers seems to be an unnecessary waste of memory.

• There may be many lines, each containing an integer. So you should make sure that your program loops correctly to read and convert all of the integers. If you only read and convert the first integer, chances are you'll get no machine marks.

• Each integer will be between 1 and ULONG_MAX. ULONG_MAX is the maximum number which can be stored in a variable of type unsigned long. On the ugrad systems, this is 2321 = 4294967295, but this number doesn't actually mean much on its own - the key idea is to use unsigned long variables whenever you might need to store a number which is this large.

• Sample input and output for comparison purposes is available here.

• Your submission must consist of a single file called roman.cc.
• The program will be compiled with the command

g++ -Wall -g -o roman roman.cc

• Your submission must be placed in a tar file. In order to do this, refer to the submission instructions page.
• Once you have created the tar file you need to submit it, which is also described on the submission instructions page.

### Common Questions

1. How many symbols will there be?
There will be an odd number of symbols between 3 and 19 (inclusive).

2. What if this isn't the case?
It will always be the case in the machine marking. Thus it doesn't matter what your code does if it isn't the case.

3. What if symbols are repeated in the symbol list, eg IVXLCDMIV?
That won't be tested, so you don't need to worry about it. However, that said, your code should probably still work, it's just that the output will be ambigious (ie. if you look at the output and see one of the repeated symbols, you won't necessarily know which value it represents).

4. What's the maximum integer which will be input?
Each integer will be between 1 and ULONG_MAX. ULONG_MAX is the maximum number which can be stored in a variable of type unsigned long. On the ugrad systems, this is 232-1 = 4294967295, but this number doesn't actually mean much on its own - the key idea is to use unsigned long variables whenever you might need to store a number which is this large.

5. My program reads and converts one integer and then exits. Is this okay?
No. Your program needs to read as many integers as there are in the input. If you only read and convert one integer, you'll probably get no machine marks. Have a look at the Week 2 Monday lecture for how you might go about doing that in main().

6. Okay, I've got the standard while (std::cin >> number) code. How do I stop the input when I'm testing my code?
If you're on Unix (eg. the Basser labs) or Cygwin, type Ctrl-D (on a blank line). If you're using some form of DOS/Windows compiler, try typing Ctrl-Z (on a blank line). In both cases, this causes end-of-file to be registered on std::cin, which makes the above while loop exit.

7. Does the machine marking read from files? If so, do we have to take our input from files?
In fact, the machine marking does read from files. However, it feeds these files into your program in such a way that your program still thinks that its input is being typed in by a human. Thus, you can read from std::cin as normal and not worry about reading from files in your program.

8. Should I output each Roman numeral as I read in the integer, or should I read all the integers first and then output all the Roman numerals?
The machine marking doesn't distinguish between these two cases. However there doesn't seem to be any real point in storing all the integers just to convert and output them later, unless you like wasting memory.