943,936 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Marked Solved
  • Views: 2450
  • C RSS
You are currently viewing page 2 of this multi-page discussion thread; Jump to the first page
Sep 17th, 2009
0

Re: Convert Decimal to Radix

result[numchars++] = basechars[r];

When you're done, print from numchars-1 back down to 0 (one char at a time).
Team Colleague
Reputation Points: 5862
Solved Threads: 950
Posting Sage
Salem is offline Offline
7,164 posts
since Dec 2005
Sep 17th, 2009
0

Re: Convert Decimal to Radix

when I add the result[i++] the program crashes, I think because it keeps incrementing infinity
Reputation Points: 10
Solved Threads: 0
Light Poster
Kombat is offline Offline
39 posts
since Sep 2009
Sep 17th, 2009
3

Re: Convert Decimal to Radix

Post your code!
Did you initialise i ?
Team Colleague
Reputation Points: 5862
Solved Threads: 950
Posting Sage
Salem is offline Offline
7,164 posts
since Dec 2005
Sep 17th, 2009
0

Re: Convert Decimal to Radix

  1. #include <stdio.h>
  2. #define SIZE 25
  3. int main(void) {
  4. int d, b;
  5.  
  6.  
  7. printf("Enter decimal and base: ");
  8. scanf("%d", &d);
  9. scanf("%d", &b);
  10.  
  11. if(b<2){
  12. printf("Your base is too low! \n");
  13. }
  14. else if (b >16){
  15. printf("Your base is too high! \n");
  16. }
  17. else if(d==0){
  18. printf("%d \n", 0);
  19. }
  20. else{
  21.  
  22. while (d!=0){
  23. char temp[SIZE];
  24. char result[SIZE];
  25. int i, r, n ;
  26. r = d%b;
  27. d = d/b;
  28. char basechars[] = "0123456789ABCDEF";
  29. temp[i++] = basechars[r];
  30. result[n++] = temp[--i];
  31.  
  32. printf("%c", result);
  33. }
  34. }
  35. system("PAUSE");
  36.  
  37. }
Last edited by Kombat; Sep 17th, 2009 at 6:16 pm.
Reputation Points: 10
Solved Threads: 0
Light Poster
Kombat is offline Offline
39 posts
since Sep 2009
Sep 17th, 2009
0

Re: Convert Decimal to Radix

You're making this more difficult than it needs to be. Look at this code snippet:
  1. while (d != 0) {
  2. r = d % b;
  3. d = d / b;
  4. result[len++] = basechars[r];
  5. }
  6.  
  7. for (i = len-1; i >= 0; --i) {
  8. printf("%c", result[i]);
  9. }
Last edited by yellowSnow; Sep 17th, 2009 at 8:26 pm.
Reputation Points: 651
Solved Threads: 35
Posting Whiz in Training
yellowSnow is offline Offline
201 posts
since Jul 2009
Sep 17th, 2009
0

Re: Convert Decimal to Radix

Wow thanks it works however it is showing a lot of weird symbols along with the answer.
Reputation Points: 10
Solved Threads: 0
Light Poster
Kombat is offline Offline
39 posts
since Sep 2009
Sep 17th, 2009
0

Re: Convert Decimal to Radix

Click to Expand / Collapse  Quote originally posted by Kombat ...
Wow thanks it works however it is showing a lot of weird symbols along with the answer.
Post your full code.
And don't thank me - Salem is the one you should be thanking.
Reputation Points: 651
Solved Threads: 35
Posting Whiz in Training
yellowSnow is offline Offline
201 posts
since Jul 2009
Sep 17th, 2009
0

Re: Convert Decimal to Radix

I pmed you it
Last edited by Kombat; Sep 17th, 2009 at 8:47 pm.
Reputation Points: 10
Solved Threads: 0
Light Poster
Kombat is offline Offline
39 posts
since Sep 2009
Sep 17th, 2009
1

Re: Convert Decimal to Radix

Initialize k to zero.
Also, move your declarations for r and basechars outside of the loop.
Reputation Points: 651
Solved Threads: 35
Posting Whiz in Training
yellowSnow is offline Offline
201 posts
since Jul 2009
Sep 17th, 2009
0

Re: Convert Decimal to Radix

the moving r and basechars made it fail but initializing the k to zero did the trick.

Thanks very much to you and many thanks to salem
Reputation Points: 10
Solved Threads: 0
Light Poster
Kombat is offline Offline
39 posts
since Sep 2009

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: Previous declaration of ___ was here
Next Thread in C Forum Timeline: c prgrams





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC