944,038 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Marked Solved
  • Views: 7102
  • C RSS
You are currently viewing page 1 of this multi-page discussion thread
Oct 30th, 2006
0

Base conversion

Expand Post »
I'm trying to find a way to convert a number in any given base to decimal and then to the target base. What i'm asking is how will I represent this procedure in the function for example 123 base 4 to decimal= 1x4^2+2x4^1+3x4^0=27. I was thinking of decrementing the raised power with a for statement.But i'm also using a for statement to access each number in the string array. heres my attempt

  1. #include<stdio.h>
  2.  
  3. int main(void)
  4.  
  5. {
  6. char num_string;
  7. int sourcebase;
  8. int targetbase;
  9. int temp
  10.  
  11. do {
  12. printf("Enter a positive number/n");
  13. scanf_s("%c",&num_string);
  14. printf("Enter the base of that number");
  15. scanf_s("%d", &sourcebase);
  16. printf("Enter the destination base");
  17. scanf_s("%d",&targetbase);
  18. if((sourcebase<2)||(sourcebase>10)){
  19. printf ("You number is invalid!!!!!!!/n/n");
  20. }
  21. }
  22. length= //some code to get string length
  23.  
  24. for(i=0;i<length,i++)
  25.  
  26. temp=num_string[i]*sourcebase //here should be corresponding index
Similar Threads
Reputation Points: 10
Solved Threads: 0
Junior Poster
boujibabe is offline Offline
123 posts
since Nov 2004
Oct 30th, 2006
0

Re: Base conversion

You actually wanted num_string as char array( C style string) but have declared it as being char which will hold only one character.

  1. char num_string = '\0' ; // wrong
  2. char num_string[BUFSIZ] = {'\0'} ; // correct, will now hold a number string

Also can you give a sample output your program is supposed to display, since it would help in clearing hte problem stmt which is not so apparent to me, seeing your imcomplete implementation.
Last edited by ~s.o.s~; Oct 30th, 2006 at 1:49 pm.
Super Moderator
Featured Poster
Reputation Points: 3241
Solved Threads: 720
Failure as a human
~s.o.s~ is offline Offline
8,873 posts
since Jun 2006
Oct 30th, 2006
0

Re: Base conversion

Ok the program is supposed to convert to the target base of a number given the soucre base and the number. I am using decimal as a segway between bases after I get to decimal I'll divide out using the target base.

Enter a positive number : 120
Enter the base of that number (2 to 10) : 3
Enter the target base (2 to 10) : 5
The number in the target base is : 30
Reputation Points: 10
Solved Threads: 0
Junior Poster
boujibabe is offline Offline
123 posts
since Nov 2004
Oct 30th, 2006
0

Re: Base conversion

Okay I will give you the algorithm, you just try to implement it.
  • Accept the number, the source base and the target base from the user.
  • convert the given "number" having base "source_base" into decimal using the formula:
  1. while( tmp_number )
  2. {
  3. decimal += (tmp_number % 10) * pow( source_base, i ) ;
  4. tmp_number /= 10 ;
  5. ++i ;
  6. }
  • Now we have the decimal equivalent of the number in "decimal"
  • Create a dynamic array uisng pointer to int which will hold the individual digits of the new number of the target base.
  • Using the given formula convert from decimal to your base.
  1. while( decimal )
  2. {
  3. new_number[i] = decimal % target_base ;
  4. decimal /= target_base ;
  5. new_number = (int*) realloc( new_number, i + 2 ) ;
  6. ++i ;
  7. }
  • In the end you would get your required number by traversing through the integer array.
Hope it helped, bye.
Super Moderator
Featured Poster
Reputation Points: 3241
Solved Threads: 720
Failure as a human
~s.o.s~ is offline Offline
8,873 posts
since Jun 2006
Oct 30th, 2006
0

Re: Base conversion

Does this negate the need for the for statement?How will it know which digit to multippy the base and exponent by?
Reputation Points: 10
Solved Threads: 0
Junior Poster
boujibabe is offline Offline
123 posts
since Nov 2004
Oct 30th, 2006
0

Re: Base conversion

while constructs and for constructs are both looping constructs -- you can use either one of them for looping purpose if thats what you wanted to ask.

decimal += (tmp_number % 10) * pow( source_base, i ) ;

Here decimal is the var which will hold the decimal value of the entered number with the base "source_base". To it we iteratively add the digit * source_base ^ i.

Here digit is obtained by doing tmp_number % 10 while i controls the exponent value.
Last edited by ~s.o.s~; Oct 30th, 2006 at 3:43 pm.
Super Moderator
Featured Poster
Reputation Points: 3241
Solved Threads: 720
Failure as a human
~s.o.s~ is offline Offline
8,873 posts
since Jun 2006
Oct 30th, 2006
0

Re: Base conversion

ok (last question i swear )so when we say while(temp_number) when are we telling the loop to terminate? shouldn't we say
while(temp_number%10 !=0)?
Last edited by boujibabe; Oct 30th, 2006 at 4:00 pm.
Reputation Points: 10
Solved Threads: 0
Junior Poster
boujibabe is offline Offline
123 posts
since Nov 2004
Oct 30th, 2006
0

Re: Base conversion

while (temp_number) means continue with the loop while temp_number is not 0. Inside the loop I am dividing the temp_number with 10 (temp_number /= 10 ) so the loop ends when this value reaches 0.

Quote ...
eg.
Initial value = 129
129 / 10 = 12
12 / 10 = 1
2 / 10 = 0 ( loop ends )
Hope it made matters a bit clear for you.
Super Moderator
Featured Poster
Reputation Points: 3241
Solved Threads: 720
Failure as a human
~s.o.s~ is offline Offline
8,873 posts
since Jun 2006
Oct 30th, 2006
0

Re: Base conversion

what is the realloc() doing here and why are we adding 2?I've never used it before
new_number = (int*) realloc( new_number, i + 2 ) ;
Reputation Points: 10
Solved Threads: 0
Junior Poster
boujibabe is offline Offline
123 posts
since Nov 2004
Oct 30th, 2006
0

Re: Base conversion

Hmmm... realloc is the memory management function which really makes the job of dynamic memory allocation actually "dynamic".

Dynamic memory when allocated using malloc, returns a pointer to the allocated peice of memory or memory block. But if the user wants to dynamically increase the size of the memory allocated to the same pointer, malloc wont do hte job.

To expand or to allocate more memory to the pointer which already has been allocated memory using malloc, we use realloc ( reallocation of memory ).

Consider this snippet:
  1. // result stored in the dynamically allocated integer array with
  2. // one slot
  3. int* new_number = (int*) calloc( sizeof( int ), sizeof( int ) ) ;
  4.  
  5. i = 0 ;
  6. while( decimal )
  7. {
  8. new_number[i] = decimal % target_base ;
  9. decimal /= target_base ;
  10.  
  11. new_number = (int*) realloc( new_number, i + 1 ) ;
  12. // we need more memory, which will be controlled by the counter i
  13. // hence while reallocing we use i as reference. So when i = 1
  14. // allocate (2 + 1) = 3 slots and so on.
  15.  
  16. ++i ;
  17. }

realloc takes two arguments, the first one being the pointer which has to be reallocated memory and the second one being the new size of the memory to be allocated.

For more info look here:
http://www.cplusplus.com/ref/cstdlib/realloc.html

Hope I have been helpful , bye.
Last edited by ~s.o.s~; Oct 30th, 2006 at 4:40 pm.
Super Moderator
Featured Poster
Reputation Points: 3241
Solved Threads: 720
Failure as a human
~s.o.s~ is offline Offline
8,873 posts
since Jun 2006

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: structure
Next Thread in C Forum Timeline: Help: Passing arrays between functions





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


Follow us on Twitter


© 2011 DaniWeb® LLC