943,652 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Marked Solved
  • Views: 2446
  • C RSS
You are currently viewing page 1 of this multi-page discussion thread
Sep 17th, 2009
0

Convert Decimal to Radix

Expand Post »
You are required to write a C program that accepts two decimal integers, say d and r. You
may assume that the first decimal integer d is nonnegative and that the second decimal integer r
which represents the radix, will be one of 2, 3, 4, . . ., 15, 16. The program should convert the first
decimal integer d into its representation in radix r and print the result.

I seriously am clueless on how to start. What I want to do is take a decimal divide by a base record the remainder and keep doing so with the decimal until the decimal is 0 and record the remainders. That is how I get the binary.

This is what I got so far its absolutely nothing

  1. #include <stdio.h>
  2.  
  3. int main(void){
  4.  
  5. int d, r, temp;
  6. temp = d = r = 0;
  7. temp = d/r;
Similar Threads
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

fixed it to this
  1. #include <stdio.h>
  2. int main(void){
  3.  
  4. int d, r, temp;
  5. d = r = 0;
  6.  
  7.  
  8. for(temp = d; temp !=0; temp%r);
  9. }
Last edited by Kombat; Sep 17th, 2009 at 1:41 am.
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

> That is how I get the binary.
That's how you do it for every other base as well.
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

I've been working on this for a while and got this code. It is a pretty good start. But not sure where to go from here

  1. #include <stdio.h>
  2. int main(void) {
  3.  
  4. int d, b, c;
  5.  
  6. printf("please enter decimal and base");
  7. scanf("%d\n", &d);
  8. scanf("%d", &b);
  9.  
  10. while(d!=0){
  11. int r;
  12. r = d%b;
  13. d = d/b;
  14. printf("\n%d", c);
  15. }
  16.  
  17. }

I do not want it to print in reverse
Last edited by Kombat; Sep 17th, 2009 at 1:17 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

Well it's good for bases < 10

char basechars[] = "0123456789";
and
printf("%c", basechars[r] );
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. int main(void) {
  3.  
  4. int d, b, c;
  5.  
  6. printf("Enter decimal and base: ");
  7. scanf("%d", &d);
  8. scanf("%d", &b);
  9.  
  10. if(b<2){
  11. printf("Your base is too low! \n");
  12. }
  13. else{
  14.  
  15. while(d!=0){
  16. int r;
  17. r = d%b;
  18. d = d/b;
  19. char basechars[] = "0123456789ABCDEF";
  20. printf("%c", basechars[r]);
  21.  
  22. }
  23. }

Thanks, however it is still printing the result in reverse:

Example: 25 3 it prints 122 when answer should be 221
Last edited by Kombat; Sep 17th, 2009 at 1:57 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

Store each char in another array, then print it out backwards?
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. int main(void) {
  3.  
  4. int d, b, c;
  5.  
  6. printf("Enter decimal and base: ");
  7. scanf("%d", &d);
  8. scanf("%d", &b);
  9.  
  10. if(b<2){
  11. printf("Your base is too low! \n");
  12. }
  13. else{
  14.  
  15. while(d!=0){
  16. int r;
  17. r = d%b;
  18. d = d/b;
  19. char basechars[] = "0123456789ABCDEF";
  20. printf("%c", basechars[r]);
  21.  
  22. }
  23. }
  24. system("PAUSE");
  25. }

I have the char's stored in an array however the conversion is being printed out backwards.
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

> basechars[r]
You're printing them, not storing them!
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

I am not sure how to do that. I tried this

  1. int r;
  2. char i;
  3. char result[ARRAY_SIZE];
  4. r = d%b;
  5. d = d/b;
  6. char basechars[] = "0123456789ABCDEF";
  7. basechars[r] = i;
  8. printf("%c", result[i]);

but it doesn't work
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