Roman Numerals Anyone.,.,??
After reading this post, you are expected to understand and realize.
- What "HARDCODED" programs are
- and that You can make a "BETTER" algorithm than the one presented
Here is the code for printing the Roman Numeral equivalent of Integers from 11 - 39 only.
1. #include <iostream>
2.
3. using namespace std;
4.
5. int main ()
6. {
7. int numberToBeRomanized = 0;
8. int zeroToNine = 0;
9. int printX = 0; int countToX = 0;
10. cout << "::HARDCODED romanizer::" << endl;
11. cout << "romanizes numbers from 10 - 39 only" << endl;
12. for(;;)
13. {
14. cout << "Enter Number To Be Romanized: ";
15. cin >> numberToBeRomanized;
16.
17. if (numberToBeRomanized == 999)
18. break;
19.
20. printX = numberToBeRomanized / 10;
21.
22. while (countToX < printX)
23. {
24. cout << "X";
25. countToX++;
26. if (countToX == printX)
27. {
28. zeroToNine = numberToBeRomanized %= 10;
29. if (zeroToNine == 0)
30. cout << "" << endl;
31. if (zeroToNine == 1)
32. cout << "I" << endl;
33. if (zeroToNine == 2)
34. cout << "II" << endl;
35. if (zeroToNine == 3)
36. cout << "III" << endl;
37. if (zeroToNine == 4)
38. cout << "IV" << endl;
39. if (zeroToNine == 5)
40. cout << "V" << endl;
41. if (zeroToNine == 6)
42. cout << "VI" << endl;
43. if (zeroToNine == 7)
44. cout << "VII" << endl;
45. if (zeroToNine == 8)
46. cout << "VIII" << endl;
47. if (zeroToNine == 9)
48. cout << "IX" << endl;
49. } //end the if's
50. } //end while
51. countToX = 0;
52. } //end for
53. return 0;
54. }
Tracing the whole program flow is left as an exercise... However, important points to be highlighted are as follows.
- Notice Line 12. This is the "FOREVER" loop. Mainly because it loops forever. Until the programmer specifies a certain sentinel value, that makes it stop. In the program it was set to be 999 (Line 17). Try it out for yourself change the sentinel value from 999 to whatever integer you like.
- Notice also Line 26. This condition just checks whether it's already the last iteration. If it is, then it prints out roman numeral equivalents from 0 (blank space) to 9 ("IX"). This is based on the observation that Roman Numeral End digits always fall in between these values so it is but proper to check them during the last iteration.
- Line 51 as well. This line simply re-initializes the variable countToX to zero. The reason for this is line number 25, where we increment its value to get the number of X's to print. If we omit this code (try it), the program would correctly display the the Roman Numeral Equivalent of only the first number the user enters. Since during the next execution of the code it's value remains to be 1 or 2 or 3 depending on the first number you've entered.
- Line 28 also is important. In the sense that after printing your X's, it checks what value from zero to nine is to be printed. Say if the user inputs 27, the value of variable zeroToNine would be 7 because 27 % 10 = 7. The resulting value gets checked and the program prints the string values specified for each zero to nine value.
Line 11 would explain that. A program being hardcoded means that it could only address a portion of the whole problem it wishes to solve. Try running the program and inputting values from zero to nine.
- What happens?
- Why do you think it happened that way?
- could you guess the single line of code to be added to correct the error?
EXPANDING THE HARDCODE...
Attempting to make a program that would output roman numeral equivalents of integers that range from hundreds to thousands is actually a different thing. But if you want to follow through and edit the code above. Here's how to do it.
- Line 20. This is the line that continually divides the input integer by ten. Which makes sense since the program only tries to convert integers in the given range. Try tweaking the program and add a code block that would divide the input integer by hundreds. Make another that would do it by thousands.
- Line 22. Of course since you added two variables that would count how many are the number of hundreds and thousands the input number has, you should also add loops to process them.
- Plus, as it had been stated, it only accomodates numbers from 11 - 39. so you have a lot of if statements to place correctly output "L's" "M's" and "D's". X's have been taken care of.
Alrighty then that's good. Here are some of the suggested things to do.
- Make Variables that would hold the value of the letters in them. like int X = 10, I = 1, V = 5 or int C = 100. And as you loop through the digits (visit the article regarding strings) check whether the value to be outputted is greater than the previous value. If yes, Swap the two and output the greater. and the loop continues.
- Arrays would help. =]
nothing follows
0 <- post your comments here:
Post a Comment