C Question: What's wrong with my program? [2]

At vero eos et accusamus et iusto odio dignissimos ducimus qui blanditiis praesentium voluptatum deleniti atque corrupti quos dolores et quas molestias excepturi sint occaecati cupiditate non provident, similique sunt in culpa qui officia deserunt mollitia animi, id est laborum et dolorum fuga. Et harum quidem rerum facilis est et expedita distinctio. Nam libero tempore, cum soluta nobis est eligendi optio cumque nihil impedit quo minus id quod maxime placeat facere possimus, omnis voluptas assumenda est, omnis dolor repellendus. Itaque earum rerum hic tenetur a sapiente delectus, ut aut reiciendis voluptatibus maiores alias consequatur aut perferendis doloribus asperiores repellat.

Get our expert's

answer on brainly

SEE EXPERT ANSWER

Get your free account and access expert answers to this and thousands of other questions.

A community for students.

C Question: What's wrong with my program? [2]

Computer Science
See more answers at brainly.com
At vero eos et accusamus et iusto odio dignissimos ducimus qui blanditiis praesentium voluptatum deleniti atque corrupti quos dolores et quas molestias excepturi sint occaecati cupiditate non provident, similique sunt in culpa qui officia deserunt mollitia animi, id est laborum et dolorum fuga. Et harum quidem rerum facilis est et expedita distinctio. Nam libero tempore, cum soluta nobis est eligendi optio cumque nihil impedit quo minus id quod maxime placeat facere possimus, omnis voluptas assumenda est, omnis dolor repellendus. Itaque earum rerum hic tenetur a sapiente delectus, ut aut reiciendis voluptatibus maiores alias consequatur aut perferendis doloribus asperiores repellat.

Get this expert

answer on brainly

SEE EXPERT ANSWER

Get your free account and access expert answers to this and thousands of other questions

This is supposed to be a program that accepts an integer from 1 to 3,999 and converts it to Roman numeral form. Initially the values for the digits I got were correct, but when I added the switch sections of the code I get decimal numbers for the digits. Also, the switches themselves don't seem to be working well as the program can only choose between case 1 and default. I suspect that the error has something to do with how I dealt with the strings. #include int main (void) { int Num, Digit[4]; char *RThou[5], *RHund[5], *RTens[5], *ROnes[5]; // Digit[3]: Thousands digit | RThou[5]: Thousands digit in Roman numerals // Digit[2]: Hundreds digit | RHund[5]: Hundreds digit in Roman numerals // Digit[1]: Tens digit | RTens[5]: Tens digit in Roman numerals // Digit[0]: Ones digit | ROnes[5]: Ones digit in Roman numerals printf("Input an integer from 1 to 3,999: "); scanf("%d", &Num); if (Num < 1 || Num > 3999) printf("Invalid input.\n"); else { Digit[3] = Num/1000 % 10; Digit[2] = Num/100 % 10; Digit[1] = Num/10 % 10; Digit[0] = Num % 10; switch (Digit[3]) { case 1: RThou[5] = "M"; printf("M"); break; case 2: RThou[5] = "MM"; printf("MM"); break; case 3: RThou[5] = "MMM"; printf("MMM"); break; default: RThou[5] = ""; break; } switch (Digit[2]) { case 1: RHund[5] = "C"; break; case 2: RHund[5] = "CC"; break; case 3: RHund[5] = "CCC"; break; case 4: RHund[5] = "CD"; break; case 5: RHund[5] = "D"; break; case 6: RHund[5] = "DC"; break; case 7: RHund[5] = "DCC"; break; case 8: RHund[5] = "DCCC"; break; case 9: RHund[5] = "CM"; break; default: RHund[5] = ""; break; } switch (Digit[1]) { case 1: RTens[5] = "X"; break; case 2: RTens[5] = "XX"; break; case 3: RTens[5] = "XXX"; break; case 4: RTens[5] = "XL"; break; case 5: RTens[5] = "L"; break; case 6: RTens[5] = "LX"; break; case 7: RTens[5] = "LXX"; break; case 8: RTens[5] = "LXXX"; break; case 9: RTens[5] = "XC"; break; default: RTens[5] = ""; break; } switch (Digit[0]) { case 1: ROnes[5] = "I"; break; case 2: ROnes[5] = "II"; break; case 3: ROnes[5] = "III"; break; case 4: ROnes[5] = "IV"; break; case 5: ROnes[5] = "V"; break; case 6: ROnes[5] = "VI"; break; case 7: ROnes[5] = "VII"; break; case 8: ROnes[5] = "VIII"; break; case 9: ROnes[5] = "IX"; break; default: ROnes[5] = ""; break; } printf("%d %d %d %d\n", Digit[3], Digit[2], Digit[1], Digit[0]); printf("%c %c %c %c\n", *RThou[5], *RHund[5], *RTens[5], *ROnes[5]); } }
I don't know C but problem is NOT with switch statements printf("%c %c %c %c\n", *RThou[5], *RHund[5], *RTens[5], *ROnes[5]); maybe this prints ONLY 1 character?
You're accessing memory that's out of the boundaries of your array in ALL of your switch() statements. For example, you are writing RThou[5] even though RThou[] is a char* array of size 5; arrays in C (and other languages like C++, Java, Python, etc) are zero-indexed, which means you can only subscript an array from 0 to array_size-1: i.e. if I declare an array A[10], I can only use A[0], A[1], ..., A[9] and anything else would be undefined behavior.

Not the answer you are looking for?

Search for more explanations.

Ask your own question

Other answers:

that's what i thought, because always 1 char was written
but in others languages i would use String for it :P
in C, you could make good use of here
@Tomas.A: Yep, I realized soon enough that changing %c to %s should allow me to print more than 1 character. @agdgdgdgwngo: I'm afraid I'm not sure if I understand you. So if we imagine RThou[5] to look like this: [] [] [] [] [] 0 1 2 3 4 Does it mean in my switch statements, I'm trying to jam all those characters in a slot that does not exist? How can I make them fit into slots 0-4?
right. try dereferencing valid slots.
But if that's the case, how come it works for the first 2 digits, and fails for the last 2?
switch (Digit[3]) { case 1: RThou[5] = "M"; printf("M"); break; case 2: RThou[5] = "MM"; printf("MM"); break; case 3: RThou[5] = "MMM"; printf("MMM"); break; default: RThou[5] = ""; break; } you are using printf statements there.
Even without the printf statements, the program is able to identify the correct values for the thousands and hundreds digits. Just not the tens and ones digits.
Okay, it seems that changing Digit[0] to Digit[4] makes the program output the correct value for the ones digit.
digit[4] doesn't give you a segfault?
For some weird reason, no.
post the code on a site like http://ideone.com so we can debug it
Here: http://ideone.com/nK1OV
http://ideone.com/vaYtT works alright O.o
Ah. The tens digit will always output to 1. '3333' should not work.
http://ideone.com/2L3MA got rid of all the out of array bounds stuff
Hmm. Never knew you could just do away with declaring the number of slots in a string. Does that work for arrays of other data types?
I don't think so :(
If I *really* wanted to specify the number of slots in the strings, what should I have done?
you can do lots of stuff, like defining char arrays of specific sizes, malloc(), etc.
Another question: How come removing the array bounds made the code work?
It actually works even when you are storing your strings in places outside the bounds of your arrays, since you are operating on the stack.
Since you declared the thousands, hundreds, tens and ones consecutively, when you were going beyond the end of the thousands you could possibly have been putting the thousands value into the hundreds, then the hundreds values into the tens, and sooner or later putting the value into who knows what.

Not the answer you are looking for?

Search for more explanations.

Ask your own question