how to approach this problem?

does anyone know how to approach this problem?

One of the principal stumbling blocks in the early development of mathematics was the notation used for numbers before the “Arabic” or “decimal” number system was introduced, since some of the older number systems were quite unsuited for arithmetic. As an example, the Roman system of numbers makes use of the symbols M (with value 1000), D (value 500), C (value 100), L (value 50), X (value 10), V (value 5), and I (value 1).

In the “old Roman” or “Classical” system a number is a sequence of M’s, D’s, C’s, L’s, X’s, V’s, and 1’s in that order, the value of a number just being the sum of the values of the individual symbols.

Write a program that will convert a classical Roman numeral to a decimal number.


if they input the roman numeral (eg MMIX) would we declare ints M I X and give them set values? SOrry! i'm not sure how to do this problem.
Let's suppose.
Suppose that the inputted roman number is valid.
Suppose that there is no need for subtraction. I mean, it will not appear IX, XL, CD, etc

In that case you could assign a value to every symbol, and just add them together to form the final value.
Do it in paper first. Pay attention to what you do, and pass it to the code.

I wait your feedback

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>
using namespace std;
int decimal (int a=0, int b=0, int c=0, int d=0, int e=0, int f=0, int g=0)
{
 int dec;
 dec = a+b+c+d+e+f+g;
 return dec;
}
int main()
{
 int M=1000=a, D=500=b, C=100=c, L=50=d, X=10=e, V=5=f, I=1=g;
 cout << "Insert a Classical Roman Numeral and I will output its decimal equivalent.\n";
 cout << "Insert the Roman number: \n";
 cin >> a, b, c, d, e, f, g;

 cout << "Your number is " << dec(a,b,c,d,e,f,g) << ".";

 system ("pause");
}


I have this code written so far... its just a hunch D: but i keep getting out 1000...
in functions there is no need to give them a value, along with forgetting to define a value for your int inside the function, so
1
2
3
4
5
6
int decimal (int a=0, int b=0, int c=0, int d=0, int e=0, int f=0, int g=0)
{
 int dec;
 dec = a+b+c+d+e+f+g;
 return dec;
}

needs some revising, along with getting rid of that system call. that is a bad habit to get into.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>
using namespace std;
int decimal (int a, int b, int c, int d, int e, int f, int g)
{
 int dec=0;
 dec = a+b+c+d+e+f+g;
 return dec;
}
int main()
{
 int M=1000=a, D=500=b, C=100=c, L=50=d, X=10=e, V=5=f, I=1=g;
 cout << "Insert a Classical Roman Numeral and I will output its decimal equivalent.\n";
 cout << "Insert the Roman number: \n";
 cin >> a, b, c, d, e, f, g;

 cout << "Your number is " << dec(a,b,c,d,e,f,g) << ".";

 return 0;
}


is this hopefully better? what do you mean by defining the value for the int inside the function?
when u are saying
int decimal(int a, int b, int c, int d, int e, int f, int g)
you are saying the ints are the formal arguments of the functions, meaning that in order to call the function you need to insert values into each of required formal arguments in order to get the actual arguments of the function, in which is is able to run the function, setting the formal arguments value will not change the actual arguments value but is completely useless. and u set int dec = 0; so it will now work. visual for formal and actual value:

int dec ( int formal){
return formal;}
int main(){
int actual = 0;
std::cout << dec(actual);
return 0;
}
the output is 0, now if i set
int dec( int formal = 10){
return formal;}
the output will still be 0.

call to function(actual is 10)
|| ||
use of function(formal is 10)

cin >> a, b, c, d, e, f, g; That will not read several values.
cin>>a>>b>>c>>d>>e>>f>>g; is to read several values.
However you shouldn't do that.

The input is 'one' roman number. Because of the symbols you'll need to read it as a string
1
2
std::string roman;
cin>>roman;


Now you need to traverse the string. You check every letter, you look up its value, and you add them.

int M=1000=a; ¿what do you think that you are doing there?
might i suggest a char array instead of a string for this? it is simpler to evaluate each individual char that way.
¿how is it simpler? The syntax is exactly the same.
i find it easier for this to move with
for(int i = 0; i < lengthofarray; i++){
array[i] = stuff you want to do to it;
}
i just find that simpler than using a for this program. although string are generally better.
1
2
3
4
5
6
7
8
9
string s ;

// do stuff with s

for (int i=0; i < s.length(); ++i)
{
    if ( s[i] == 'M' )
        // do stuff.
}


Illustrate how the array is simpler?

its just user preference, it is just a suggestion on ways to do things NOT an argument on which is better....
1
2
3
4
5
6
7
8
9
    for(int i = 0; i < 10; i++){
       /*  if(array[i] == 'm') //do stuff;
        if(array[i] == 'd')//do stuff ; 
        if(array[i] == 'c') //do stuff;
        if(array[i] == 'l') //do stuff;
        if(array[i] == 'x') //do stuff;
        if(array[i] == 'v') //do stuff;
        if(array[i] == 'i') //do stuff; */ 
    }

both work fine, i just like arrays more when evaluating individual characters.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include <iostream>
using namespace std;
int main()
{
 int M=1000, D=500, C=100, L=50, X=10, V=5, I=1;
 string roman;
 cout << "Insert a Classical Roman Numeral and I will output its decimal equivalent.\n";
 cout << "Insert the Roman number: \n";
 cin >> roman;
 int i=roman.length();
 for(int n=0; n < roman.length();n++)
 {
 if (roman[i]=='M')
    {cout << M;}
 if (roman[i]=='D')
	{cout << D;}
 if (roman[i]=='C')
	{cout << C;}
 if (roman[i]=='L')
    {cout << L;}
 if (roman[i]=='X')
	{cout << X;}
 if (roman[i]=='I')
	{cout << I;}
 }

 return 0;
}


would this give me the right number if they only inputted one letter?

i get some error that code.. i dont know why. :\

 
Roman.cpp(24): warning C4018: '<' : signed/unsigned mismatch

Roman.cpp(22): error C2678: binary '>>' : no operator found which takes a left-hand operand of type 'std::istream' (or there is no acceptable conversion)
1
2
3
4
5
6
7
#include <string>

// ...

    for ( unsigned n=0; n<roman.length(); n++)

// ... 


int i=roman.length();

This is wrong. Valid indexes for roman are 0 to roman.length()-1;

Use n to index the string.
you need a variable to be the total of the roman numerals, so it would be
if( the letter is another letter) add respective value to a variable;
and so on. similar to the code i posted up top. you can do this in several different ways. using both strings and arrays. but throwing the value of the letter to cout will just output the value, not add them up.
you also need to include the string library
#include<string>
if you are using strings.
Last edited on
Topic archived. No new replies allowed.