Converting base 10 to Roman Numerals

  • Thread starter C++ hates me
  • Start date
  • Tags
    Base
In summary, the conversation is about a question posted on Physics Forums regarding a program in C++ that converts 4 digit arabic numbers into roman numerals. The user clarifies that they do not need an answer as they have already found one, but they want to understand how to write the program in C++. They then provide the code they found and ask for an explanation of certain lines. The conversation ends with the user thanking the forum for their help and asking for the thread to be moved to the appropriate board.
  • #1
C++ hates me
2
0
Hello Physics Forums,

Before i get started asking questions i wanted to clarify that i don't need/want a answer, i have an answer after about an hour of searching.

The thing is i understand what needs to be done but i do not understand how to do it in C++.
I am just hoping to learn from you guys.

Homework Statement



The problem is: I must write a program in C++ that converts a 4 digit arabic number into roman numerals. I must have a loop that allows the user to repeat the calc. however many times they wish ( I already know how.)

The answer that i found was this:

#include <stdafx.h>
#include <iostream>
using namespace std;
int main()
{
1 char l[] = "MDCLXVI";
2 int r,d[]={9,5,4,1};

3 cout << "Enter a positive integer to convert to Roman numerals:\n";
4 cin >> r;

5 do
6 {
7 int p=100;
8 for(; r>=1000; r-=1000)
9 cout << l[0];
10 for (int i=1; i<4; i++,p/=10)
11 {
12 for(; r >= p*d[0]; r -= p*d[0])
13 cout << l[2*i] << l[i*2-2];
14 for(; r >= p*d[1]; r -= p*d[1])
15 cout << l[2*i-1];
16 for(; r >= p*d[2]; r -= p*d[2])
17 cout << l[2*i]<<l[i*2-1];
18 for(; r >= p; r -= p)
19 cout << l[2*i];
}
cout << "\n\nEnter the next number, or -1 to quit:\n";
cin >> r;
} while (r>=0);

cout << "\n\nHave a nice day.";

return 0;
}

**I give all credit to the user gnome for the above program, i merely copied it :(**

The problem is i don't want to turn it in and get a good grade because it isn't my work.
I really want to be able to do it on my own, i just do not understand C++ well enough to write this program.
Can anyone explain to me in ordinary terms what the following lines of code are doing:
1, 2, 7, 8, 10, 12-19 :)

Thank you all for your guidance and patience.
Sean

[edit] I see now that i posted in introductory physics, if an admin could move it to computer sci, engineering boards i would appreciate it!
 
Last edited:
Physics news on Phys.org
  • #2
Run through the above program manually - on paper. Every time a variable is defined, write it down and put its value in.
 
Last edited:
  • #3
Ok i will do that. In line 7 when he sets the int p value = to 100 why does he do it? Does it help convert from base 10 to roman numerals?
Line 10 is the one i don't really understand and i take it, its pretty important.
 
  • #4
C++ hates me said:
Ok i will do that. In line 7 when he sets the int p value = to 100 why does he do it? Does it help convert from base 10 to roman numerals?
Line 10 is the one i don't really understand and i take it, its pretty important.
It is a counter for digit placeholders. It starts off at 100, then in line 10, where the i loop increments, it is divided by 10. So:
when i=1, p=100
when i=2, p=10
when i=3, p=1
when i=4, the loop exits

Presumably, it is going through the 100's column, then the 10's column, then the 1's column.
 

What is the history behind Roman numerals?

Roman numerals were developed by the ancient Romans in the 3rd century BC. They were originally used for counting and recording numbers on trade goods, but later became the standard system in the Roman Empire for official documents and inscriptions. The system is based on seven letters: I, V, X, L, C, D, and M, with each letter representing a different value.

How do you convert base 10 numbers to Roman numerals?

To convert base 10 numbers to Roman numerals, you will need to follow a set of rules. First, divide the number into its place values (ones, tens, hundreds, etc.). Then, use the appropriate Roman numerals for each place value, starting with the largest value and working down. Finally, combine the Roman numerals together to form the complete number. For example, to convert 45 to Roman numerals, you would use XLV (40 + 5).

What are the main differences between base 10 and Roman numerals?

The main difference between base 10 and Roman numerals is the symbols used to represent numbers. Base 10 uses the digits 0-9, while Roman numerals use a combination of letters. Another difference is the way numbers are written. In base 10, the value of each digit is determined by its position in the number. In Roman numerals, the value of each letter is determined by its placement and combination with other letters.

Are there any limitations to using Roman numerals?

Yes, there are limitations to using Roman numerals. They are primarily used for counting and writing small numbers, as the system becomes complex and cumbersome for larger numbers. Additionally, Roman numerals do not have a concept of zero, making calculations and algebraic equations difficult. They are also not a positional system, which means that the same letters can represent different values depending on their placement in the number.

Why are Roman numerals still used today?

Despite their limitations, Roman numerals are still used today in various contexts. They are commonly seen in the names of monarchs and popes, on clock faces, and in movie credits. They are also used in formal documents, such as legal contracts, to add a sense of tradition and formality. Additionally, Roman numerals are still used in some mathematical notation and for decorative purposes in design and typography.

Similar threads

  • Introductory Physics Homework Help
Replies
1
Views
84
  • Introductory Physics Homework Help
Replies
17
Views
373
  • Introductory Physics Homework Help
Replies
3
Views
111
  • Introductory Physics Homework Help
Replies
5
Views
456
  • Programming and Computer Science
Replies
12
Views
1K
  • Introductory Physics Homework Help
Replies
4
Views
806
  • Introductory Physics Homework Help
Replies
3
Views
997
  • Introductory Physics Homework Help
Replies
13
Views
1K
  • Introductory Physics Homework Help
Replies
11
Views
1K
  • Introductory Physics Homework Help
Replies
1
Views
869
Back
Top