0

Roman to integer

https://code.sololearn.com/c9BqjLY8ui5I/?ref=app I write my code on my way, but it doesn't go correctly. Does anybody knows how to solve it? Sometimes it goes right, something not. It doesn't considerate the last one or two letters.

27th Dec 2021, 7:03 AM
TeaserCode
10 RĂ©ponses
+ 3
This I think should work 🙂 https://code.sololearn.com/c7jJfPI2nuWb
27th Dec 2021, 11:08 AM
Agnes Ahlberg
Agnes Ahlberg - avatar
+ 1
You can salvage most of your code the following way: 1) Make dig1 one element larger and set dig1[count]=0. The reason is that your loop either includes one element too many or drops the last one. By adding one element more and setting it to 0, it is involved in the comparison, and will always be smaller than its predecessor. 2) Have the loop run while "i<count", to process all digits 3) Lose the "visited" flag and its "if". Instead, replace "continue" with "x++" to increase x to two digits having been processed. 4) fix the brackets after removing the "if(visited)" Now, it should work 🙂
27th Dec 2021, 8:29 AM
Agnes Ahlberg
Agnes Ahlberg - avatar
+ 1
I use bool statement because I noticed during interation if next letter is bigger than current then bigger is used twice. First at the substraction with current and second when it is itereted normally. So bool skip the second way.
27th Dec 2021, 8:47 AM
TeaserCode
+ 1
Agnes, I have done as you said. But it is not possible to initialize variable-sized object. Nothing happens, last letter is still not in account. Can you make corrections?
27th Dec 2021, 10:56 AM
TeaserCode
+ 1
But it write down the right size of array when I print count. I consider the size when I am copying the numbers into array in switch statement. Seems strange.
27th Dec 2021, 1:27 PM
TeaserCode
0
I 'm going to test it if it is really possible. Thanks.
27th Dec 2021, 8:37 AM
TeaserCode
0
It will be, I'm sure 🙂🍀 Incidentally, the "visited" flag is not necessary, because there are only two logical paths. And "count[x] < count[x+1]" is the same as "visited". So, no need for the boolean flag. As a matter of fact, it is one of the problems you have now, because you forgot to reset the flag to "false". And the "continue" statement has you skip the output - so, that needs to go, too.
27th Dec 2021, 8:41 AM
Agnes Ahlberg
Agnes Ahlberg - avatar
0
Yes, but you can skip already when checking the digits for their numerical value 🙂
27th Dec 2021, 9:31 AM
Agnes Ahlberg
Agnes Ahlberg - avatar
0
Just noticed, you did not specify the correct size for count. I have added that now.
27th Dec 2021, 11:20 AM
Agnes Ahlberg
Agnes Ahlberg - avatar
0
The array does not adjust its size later. It is sized the moment it is created, and at that time, count is zero.
27th Dec 2021, 3:14 PM
Agnes Ahlberg
Agnes Ahlberg - avatar