CS1101C Lab 2 (Even Week)

Roman Numerals Again

The deadline for this lab question is Thursday 09 March 2006, 23:59:59 hours.

The name of your C program file must be called arabic.c, files with any other name will not be marked.

Preliminary

Roman numerals are used frequently to indicate dates, in particular the year (for example, MMVI represents the year 2006). There are two main differences between roman and arabic numerals.

Your task for this lab assignment is to convert a roman numeral to its arabic equivalent. To assist you with character input, a sample code readchar.c is provided below that requests from the user a roman numeral (in fact it can be anything you type) and prints out the characters one at a time. It is up to you whether you wish to use the code; it is optional.

#include <stdio.h>

int main(void)
{
     char ch;

     printf("Enter roman numerals: ");
     do
     {
         scanf("%c", &ch);

         if (ch != '\n')
         {
             printf("Printing one character at a time: %c\n", ch);
         }
     } while (ch != '\n');

     return 0;
}

A sample output is shown below.

$ gcc -Wall readchar.c -o readchar
$ readchar
Enter roman numerals: MMVI
Printing one character : M
Printing one character : M
Printing one character : V
Printing one character : I
$

Notice that we perform explicit checks for the newline character. Why do you think this is needed?

Roman Numeral Conversion

The conversion is specified using the following cases.

The Task

Write a program arabic.c that requests a roman numeral from the user. The input can either be lowercase, uppercase, or a mix of both. A sample run of the program is shown below. User input is denoted in bold.


$ gcc -Wall arabic.c -o arabic
$ arabic
Enter roman numerals : i
The year in arabic is 1.

$ arabic
Enter roman numerals : MMVI
The year in arabic is 2006.

$ arabic
Enter roman numerals : McMxCvIiI
The year in arabic is 1998.

$ arabic
Enter roman numerals : MMMMMMMMMCMXCIX
The year in arabic is 9999.

$ arabic
Enter roman numerals : mmmmmmmmmdccclxxxviii
The year in arabic is 9888.

$

Points to Note


This document, index.html, has been accessed 9 times since 25-Jun-24 11:57:13 +08. This is the 1st time it has been accessed today.

A total of 7 different hosts have accessed this document in the last 391 days; your host, 216.73.216.149, has accessed it 1 times.

If you're interested, complete statistics for this document are also available, including breakdowns by top-level domain, host name, and date.