A community for students.
Here's the question you clicked on:
 0 viewing
anonymous
 5 years ago
C Question: What's wrong with my program? [2]
anonymous
 5 years ago
C Question: What's wrong with my program? [2]

This Question is Closed

anonymous
 5 years ago
Best ResponseYou've already chosen the best response.0This 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 <stdio.h> 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]); } }

anonymous
 5 years ago
Best ResponseYou've already chosen the best response.0I 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?

anonymous
 5 years ago
Best ResponseYou've already chosen the best response.0You'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 zeroindexed, which means you can only subscript an array from 0 to array_size1: 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.

anonymous
 5 years ago
Best ResponseYou've already chosen the best response.0that's what i thought, because always 1 char was written

anonymous
 5 years ago
Best ResponseYou've already chosen the best response.0but in others languages i would use String for it :P

anonymous
 5 years ago
Best ResponseYou've already chosen the best response.0in C, you could make good use of <string.h> here

anonymous
 5 years ago
Best ResponseYou've already chosen the best response.0@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 04?

anonymous
 5 years ago
Best ResponseYou've already chosen the best response.0right. try dereferencing valid slots.

anonymous
 5 years ago
Best ResponseYou've already chosen the best response.0But if that's the case, how come it works for the first 2 digits, and fails for the last 2?

anonymous
 5 years ago
Best ResponseYou've already chosen the best response.0switch (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.

anonymous
 5 years ago
Best ResponseYou've already chosen the best response.0Even 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.

anonymous
 5 years ago
Best ResponseYou've already chosen the best response.0Okay, it seems that changing Digit[0] to Digit[4] makes the program output the correct value for the ones digit.

anonymous
 5 years ago
Best ResponseYou've already chosen the best response.0digit[4] doesn't give you a segfault?

anonymous
 5 years ago
Best ResponseYou've already chosen the best response.0For some weird reason, no.

anonymous
 5 years ago
Best ResponseYou've already chosen the best response.0post the code on a site like http://ideone.com so we can debug it

anonymous
 5 years ago
Best ResponseYou've already chosen the best response.0Here: http://ideone.com/nK1OV

anonymous
 5 years ago
Best ResponseYou've already chosen the best response.0http://ideone.com/vaYtT works alright O.o

anonymous
 5 years ago
Best ResponseYou've already chosen the best response.0Ah. The tens digit will always output to 1. '3333' should not work.

anonymous
 5 years ago
Best ResponseYou've already chosen the best response.0http://ideone.com/2L3MA got rid of all the out of array bounds stuff

anonymous
 5 years ago
Best ResponseYou've already chosen the best response.0Hmm. 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?

anonymous
 5 years ago
Best ResponseYou've already chosen the best response.0If I *really* wanted to specify the number of slots in the strings, what should I have done?

anonymous
 5 years ago
Best ResponseYou've already chosen the best response.0you can do lots of stuff, like defining char arrays of specific sizes, malloc(), etc.

anonymous
 5 years ago
Best ResponseYou've already chosen the best response.0Another question: How come removing the array bounds made the code work?

anonymous
 5 years ago
Best ResponseYou've already chosen the best response.0It actually works even when you are storing your strings in places outside the bounds of your arrays, since you are operating on the stack.

rsmith6559
 5 years ago
Best ResponseYou've already chosen the best response.1Since 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.
Ask your own question
Sign UpFind more explanations on OpenStudy
Your question is ready. Sign up for free to start getting answers.
spraguer
(Moderator)
5
→ View Detailed Profile
is replying to Can someone tell me what button the professor is hitting...
23
 Teamwork 19 Teammate
 Problem Solving 19 Hero
 Engagement 19 Mad Hatter
 You have blocked this person.
 ✔ You're a fan Checking fan status...
Thanks for being so helpful in mathematics. If you are getting quality help, make sure you spread the word about OpenStudy.