Bird
0
0
DSA Cprogramming~10 mins

Roman to Integer Conversion in DSA C - Execution Trace

Choose your learning style9 modes available
Concept Flow - Roman to Integer Conversion
Start with input Roman string
Initialize total = 0, index = 0
Check current and next Roman chars
If current < next: subtract current value
Else: add current value
Move index forward by 1
Repeat until end of string
Return total as integer
We scan the Roman numeral string from left to right, adding or subtracting values based on the order of symbols, until the entire string is processed.
Execution Sample
DSA C
int value(char c) {
  switch(c) {
    case 'I': return 1;
    case 'V': return 5;
    case 'X': return 10;
    case 'L': return 50;
    case 'C': return 100;
    case 'D': return 500;
    case 'M': return 1000;
    default: return 0;
  }
}

int romanToInt(char *s) {
  int total = 0, i = 0;
  while (s[i]) {
    int curr = value(s[i]);
    int next = value(s[i+1]);
    if (curr < next) total -= curr;
    else total += curr;
    i++;
  }
  return total;
}
This code converts a Roman numeral string to an integer by adding or subtracting symbol values while scanning the string.
Execution Table
StepOperationCurrent CharNext CharAdd or SubtractTotal After OperationVisual State
1Read charsMCAdd 10001000M(1000)
2Read charsCMSubtract 100900M(1000) - C(100)
3Read charsMXAdd 10001900M(1000) - C(100) + M(1000)
4Read charsXIAdd 101910M(1000) - C(100) + M(1000) + X(10)
5Read charsIVSubtract 11909M(1000) - C(100) + M(1000) + X(10) - I(1)
6Read charsVAdd 51914M(1000) - C(100) + M(1000) + X(10) - I(1) + V(5)
7End of stringStop1914Final total = 1914
💡 Reached end of string, no more characters to process.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3After Step 4After Step 5After Step 6Final
i (index)01234566
total0100090019001910190919141914
curr (value)-100010010001015-
next (value)-100100010150-
Key Moments - 3 Insights
Why do we subtract the current value when it is less than the next value?
Because in Roman numerals, a smaller value before a larger one means subtraction. For example, in step 2, 'C' (100) is before 'M' (1000), so we subtract 100 instead of adding it.
Why do we add the current value when it is greater or equal to the next value?
When the current Roman numeral is equal or larger than the next, it means normal addition. For example, in step 1, 'M' (1000) is before 'C' (100), so we add 1000.
What happens when we reach the last character with no next character?
We always add the value of the last character because there is no next character to compare. In step 6, 'V' (5) is the last character, so we add 5.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the total after step 3?
A1000
B900
C1900
D1910
💡 Hint
Check the 'Total After Operation' column in row for step 3.
At which step does the algorithm subtract a value?
AStep 1
BStep 2
CStep 4
DStep 6
💡 Hint
Look at the 'Add or Subtract' column for the word 'Subtract'.
If the input was 'III', how would the total change after step 2 compared to the current example?
AIt would be 2
BIt would be 3
CIt would be 1
DIt would be 0
💡 Hint
Consider that 'I' is 1 and no subtraction occurs in 'III'.
Concept Snapshot
Roman to Integer Conversion:
- Scan Roman string left to right
- Map each symbol to its value
- If current < next, subtract current
- Else, add current
- Continue until end
- Return total integer value
Full Transcript
This concept converts Roman numerals to integers by scanning the string from left to right. At each step, it compares the current Roman symbol's value with the next one. If the current value is less than the next, it subtracts it from the total; otherwise, it adds it. This process continues until the entire string is processed, resulting in the integer equivalent of the Roman numeral.