Date: Tue, 05 Nov 1996 20:51:53 GMT Server: NCSA/1.5 Content-type: text/html Last-modified: Sat, 07 Sep 1996 23:25:47 GMT Content-length: 5417 a1pgm.html

CS354, Fall 1996

Program 1, due Monday September 16, before 5pm.

Purpose: To learn how to use the SAL/MAL simulator, to learn basic SAL instructions, to learn how to code simple procedure calls in assembly language, to learn about problems involving the representation of information.

Estimated time-to-complete: 3 - 5 hours.

Program description: Write a SAL program that reads an integer and prints its value in Roman numeral notation.

Use the subtractive representation, for example, write 4 as IV, not as IIII. The seven Roman numerals are: I = 1, V = 5, X = 10, L = 50, C = 100, D = 500 and M = 1000. Use upper case letters in your output. Within a number in Roman numeral notation, letters with higher values should be written to the left of those with lower values, except for pairs that use the subtractive representation - this rule leads to a unique representation of the number in the Roman numeral notation.

NOTE - The subtractive representation was not used by the Romans. It was introduced about 200 years ago in order to shorten the strings of Roman numerals. The "Arabic" notation for numbers was actually created and discovered by the Hindus and was brought to Europe by the Arabs. It began being used in about 1275 by European scholars.

Algorithm: To convert a number from the standard Arabic notation to the Roman notation you need to do the following steps.

(1) determine the number of thousands, hundreds, tens and units in the number

(2) for each thousand, write an M (1,000,000 would give 1000 M's, we will use smaller numbers)

(3) write the hundreds, if any, using letters C, D and M, as:

C, CC, CCC, CD, D, DC, DCC, DCCC, CM

(4) write the tens, if any, using letters X, L and C, as:

X, XX, XXX, XL, L, LX, LXX, LXXX, XC

(5) write the units, if any, using letters I, V and X, as:

I, II, III, IV, V, VI, VII, VIII, IX

Example of how the algorithm works: 1986 is MCMLXXXVI since 1986 is

1000

+

900

+

80

+

6

M

 

CM

 

LXXX

 

VI

1

thousands

 

9

hundreds

 

8

tens

 

6

units

Notice that the form of the hundreds, tens and units is the same, but that different letters are used in the three cases. This means that a single procedure can be used to write the hundreds, tens and units.

Specific requirements for your program:

(1) Write a main program that reads an integer, breaks it into hundreds, tens and units, and, with the help of a procedure called "convert," prints the corresponding value in Roman numeral notation.

(2) Procedure "convert" should have four input parameters: an integer from 1 through 9 and three characters. The integer would be the number of hundreds, tens or units and the characters would be the characters corresponding to hundreds, tens or units. The procedure should print the corresponding sequence of one to four Roman numerals. The procedure should have no output parameters. If, for example, you pass your procedure 8, X, L, and C, it should print LXXX. If you pass it 6, I, V, and X it should print VI.

(3) You may assume that your input contains no errors, that is, the prompted input will be a single positive integer. Your labeled output should follow on the next line. Your program should process a single integer and then quit. A sample run would look like:

Enter a positive integer: 1986 <- you enter the "1896"
In Roman numerals: MCMLXXXVI <- the machine responds

Here are two more sample runs:

Enter a positive integer: 8
In Roman numerals: VIII
 
Enter a positive integer: 4067
In Roman numerals: MMMMLXVII

Handing in your program: You must "hand in" your program (the SAL source code) to the computer directly by running the script

handin1 program1.s

just once, where "program1.s" is the name of the file containing your SAL source code. No printouts will be turned in. The TA/Grader will run your program several times using different test data.