Convert Decimal to Radix

Thread Solved

Join Date: Sep 2009
Posts: 17
Reputation: Kombat is an unknown quantity at this point 
Solved Threads: 0
Kombat Kombat is offline Offline
Newbie Poster

Convert Decimal to Radix

 
0
  #1
Sep 17th, 2009
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;
Reply With Quote Quick reply to this message  
Join Date: Sep 2009
Posts: 17
Reputation: Kombat is an unknown quantity at this point 
Solved Threads: 0
Kombat Kombat is offline Offline
Newbie Poster

Re: Convert Decimal to Radix

 
0
  #2
Sep 17th, 2009
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.
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 5,850
Reputation: Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute 
Solved Threads: 749
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

Re: Convert Decimal to Radix

 
0
  #3
Sep 17th, 2009
> That is how I get the binary.
That's how you do it for every other base as well.
Reply With Quote Quick reply to this message  
Join Date: Sep 2009
Posts: 17
Reputation: Kombat is an unknown quantity at this point 
Solved Threads: 0
Kombat Kombat is offline Offline
Newbie Poster

Re: Convert Decimal to Radix

 
0
  #4
Sep 17th, 2009
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.
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 5,850
Reputation: Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute 
Solved Threads: 749
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

Re: Convert Decimal to Radix

 
0
  #5
Sep 17th, 2009
Well it's good for bases < 10

char basechars[] = "0123456789";
and
printf("%c", basechars[r] );
Reply With Quote Quick reply to this message  
Join Date: Sep 2009
Posts: 17
Reputation: Kombat is an unknown quantity at this point 
Solved Threads: 0
Kombat Kombat is offline Offline
Newbie Poster

Re: Convert Decimal to Radix

 
0
  #6
Sep 17th, 2009
  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.
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 5,850
Reputation: Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute 
Solved Threads: 749
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

Re: Convert Decimal to Radix

 
0
  #7
Sep 17th, 2009
Store each char in another array, then print it out backwards?
Reply With Quote Quick reply to this message  
Join Date: Sep 2009
Posts: 17
Reputation: Kombat is an unknown quantity at this point 
Solved Threads: 0
Kombat Kombat is offline Offline
Newbie Poster

Re: Convert Decimal to Radix

 
0
  #8
Sep 17th, 2009
  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.
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 5,850
Reputation: Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute 
Solved Threads: 749
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

Re: Convert Decimal to Radix

 
0
  #9
Sep 17th, 2009
> basechars[r]
You're printing them, not storing them!
Reply With Quote Quick reply to this message  
Join Date: Sep 2009
Posts: 17
Reputation: Kombat is an unknown quantity at this point 
Solved Threads: 0
Kombat Kombat is offline Offline
Newbie Poster

Re: Convert Decimal to Radix

 
0
  #10
Sep 17th, 2009
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
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC