Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

converting numbers to roman numerals

HI there, can anyone help me with a problem. I am in my first year in C++ programming class. How can i comvert a year between 1000 to 3000 in roman numerals. Please remember this is very new to me.
Here is two examples:
1989=MCMLXXXIX
1960=MCMLX
We have covered functions and are in the chapter of loops and flow control.
Thank you for your help

PAT

Comments

  • BriballBriball Posts: 265Member
    What we need to do is split the number into its parts. 1890 has 3 parts to it: 1000 + 800 + 90. 1963 gas 4: 1000 + 900 + 60 + 3. Since we are restricted to 1000-3000 we know we'll have 4 digits, with at least one "part" to convert.

    We could write a function responsible for converting any number "part" to its roman numeral equivelent.

    [code]
    BOOL ConvertNumeral(int num, char *numeral)
    {
    //cover the possibles for the first part
    if(num == 1000 && strlen(numeral) >= 1)
    strcpy(numeral, "M");
    else if(num == 2000 && strlen(numeral) >= 2)
    strcpy(numeral, "MM");
    else if(num == 3000 && strlen(numeral) >=3)
    strcpy(numeral, "MMM");

    //now for the possible second parts
    else if(num == 100 && strlen(numeral) >= 1)
    strcpy(numeral, "C");
    else if(num == 200 && strlen(numeral) >= 2)
    strcpy(numeral, "CC");
    else if(num == 300 && strlen(numeral >= 3)
    strcpy(numeral, "CCC");
    else if(num == 400 && strlen(numeral) >= 4)
    strcpy(numeral, "CCCC");
    else if(num == 500 && strlen(numeral >= 2)
    strcpy(numeral, "VC");
    ...//yada yada, you get the point here. Next you need to check the
    //tens digits, then the ones then you are done.

    //remember, the numeral string passed to this function should NOT
    //be the string storing the final answer, but a temp variable
    return TRUE if the roman numeral value was found, FALSE if not.
    }
    [/code]

    now convert the original number to a convertable form. We need to find its "parts." Do this by storing the parts in an array, and dividing the original until we get what we want.

    [code]
    int num = 1986, divisor = 1000;
    char roman[1024] = "";
    char numeral[16];

    for(int i = 0; i <= 3; i++){
    if(ConvertNumeral((num/divisor)*divisor, numeral))
    strcat(roman, numeral);

    num %= divisor;
    divisor /= 10;
    }
    [/code]

Sign In or Register to comment.