Bird
0
0
DSA Cprogramming~10 mins

Roman to Integer Conversion in DSA C - Interactive Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to return the integer value of the Roman numeral 'I'.

DSA C
int romanToInt(char * s) {
    if (s[0] == 'I') {
        return [1];
    }
    return 0;
}
Drag options to blanks, or click blank then click option'
A1
B0
C10
D5
Attempts:
3 left
💡 Hint
Common Mistakes
Returning 0 instead of 1 for 'I'.
Confusing 'I' with 'V' or 'X'.
2fill in blank
medium

Complete the code to get the integer value of a single Roman numeral character.

DSA C
int value(char r) {
    switch(r) {
        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 [1];
    }
}
Drag options to blanks, or click blank then click option'
A-1
B1
C0
D1000
Attempts:
3 left
💡 Hint
Common Mistakes
Returning -1 which may cause incorrect calculations.
Returning 1 which is a valid numeral value and may cause confusion.
3fill in blank
hard

Fix the error in the loop condition to correctly iterate over the string until the end.

DSA C
int romanToInt(char * s) {
    int total = 0;
    int i = 0;
    while (s[i] [1] '\0') {
        total += value(s[i]);
        i++;
    }
    return total;
}
Drag options to blanks, or click blank then click option'
A!=
B==
C<
D>
Attempts:
3 left
💡 Hint
Common Mistakes
Using '==' which stops immediately if first char is not '\0'.
Using '<' or '>' which are invalid for char comparison here.
4fill in blank
hard

Fill both blanks to correctly subtract when a smaller numeral precedes a larger one.

DSA C
int romanToInt(char * s) {
    int total = 0;
    int i = 0;
    while (s[i] != '\0') {
        int s1 = value(s[i]);
        if (s[i + 1] != '\0') {
            int s2 = value(s[i + 1]);
            if (s1 [1] s2) {
                total += s2 - s1;
                i += [2];
            } else {
                total += s1;
                i++;
            }
        } else {
            total += s1;
            i++;
        }
    }
    return total;
}
Drag options to blanks, or click blank then click option'
A<
B2
C>
D1
Attempts:
3 left
💡 Hint
Common Mistakes
Using '>' instead of '<' for comparison.
Incrementing i by 1 instead of 2 after subtraction.
5fill in blank
hard

Fill all three blanks to create a dictionary-like structure mapping Roman numerals to integers using arrays.

DSA C
typedef struct {
    char symbol;
    int value;
} RomanMap;

RomanMap map[] = {
    {'I', [1],
    {'V', [2],
    {'X', [3]
};
Drag options to blanks, or click blank then click option'
A1
B5
C10
D50
Attempts:
3 left
💡 Hint
Common Mistakes
Mixing up values for 'V' and 'X'.
Using incorrect integer values.