| | |
Base conversion
Thread Solved |
•
•
Join Date: Nov 2004
Posts: 123
Reputation:
Solved Threads: 0
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
C Syntax (Toggle Plain Text)
#include<stdio.h> int main(void) { char num_string; int sourcebase; int targetbase; int temp do { printf("Enter a positive number/n"); scanf_s("%c",&num_string); printf("Enter the base of that number"); scanf_s("%d", &sourcebase); printf("Enter the destination base"); scanf_s("%d",&targetbase); if((sourcebase<2)||(sourcebase>10)){ printf ("You number is invalid!!!!!!!/n/n"); } } length= //some code to get string length for(i=0;i<length,i++) temp=num_string[i]*sourcebase //here should be corresponding index
You actually wanted num_string as char array( C style string) but have declared it as being char which will hold only one character.
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.
c Syntax (Toggle Plain Text)
char num_string = '\0' ; // wrong 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.
I don't accept change; I don't deserve to live.
•
•
Join Date: Nov 2004
Posts: 123
Reputation:
Solved Threads: 0
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
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
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:
c Syntax (Toggle Plain Text)
while( tmp_number ) { decimal += (tmp_number % 10) * pow( source_base, i ) ; tmp_number /= 10 ; ++i ; }
- 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.
c Syntax (Toggle Plain Text)
while( decimal ) { new_number[i] = decimal % target_base ; decimal /= target_base ; new_number = (int*) realloc( new_number, i + 2 ) ; ++i ; }
- In the end you would get your required number by traversing through the integer array.
I don't accept change; I don't deserve to live.
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.
I don't accept change; I don't deserve to live.
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.•
•
•
•
eg.
Initial value = 129
129 / 10 = 12
12 / 10 = 1
2 / 10 = 0 ( loop ends )
I don't accept change; I don't deserve to live.
Hmmm...
Dynamic memory when allocated using
To expand or to allocate more memory to the pointer which already has been allocated memory using
Consider this snippet:
For more info look here:
http://www.cplusplus.com/ref/cstdlib/realloc.html
Hope I have been helpful , bye.
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:
c Syntax (Toggle Plain Text)
// result stored in the dynamically allocated integer array with // one slot int* new_number = (int*) calloc( sizeof( int ), sizeof( int ) ) ; i = 0 ; while( decimal ) { new_number[i] = decimal % target_base ; decimal /= target_base ; new_number = (int*) realloc( new_number, i + 1 ) ; // we need more memory, which will be controlled by the counter i // hence while reallocing we use i as reference. So when i = 1 // allocate (2 + 1) = 3 slots and so on. ++i ; }
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.
I don't accept change; I don't deserve to live.
![]() |
Similar Threads
- base conversion (Java)
- Base converter (C++)
Other Threads in the C Forum
- Previous Thread: structure
- Next Thread: Help: Passing arrays between functions
| Thread Tools | Search this Thread |
* ansi api array arrays bash binarysearch calculate centimeter changingto char character convert copyanyfile copypdffile createcopyoffile createprocess() directory dynamic execv fflush file floatingpointvalidation fork forloop frequency function getlasterror getlogicaldrivestrin givemetehcodez grade graphics gtkgcurlcompiling gtkwinlinux hardware highest histogram homework i/o ide inches infiniteloop initialization input intmain() iso keyboard km license linked linkedlist linux list looping loopinsideloop. lowest matrix microsoft mysql oddnumber open opendocumentformat openwebfoundation pdf pointer pointers posix power program programming pyramidusingturboccodes read recursion recv recvblocked repetition reversing scanf scheduling segmentationfault send shape single socketprogramming stack standard strchr string suggestions test testautomation threads unix urboc user variable whythiscodecausesegmentationfault win32api windows.h windowsapi






