Reputation: 3332
So I have this code here, that (for now) just turns a base1 number in to a base 10 number
/**
* This function is supposed to convert 'number' from base1 to a number in base2.
* It isn't fully implemented yet, and I've only converted base1 to a base 10 number.
*/
void base1_base2(int base1, int base2, int number) {
int num, place = 0;
int rem = number;
int i;
for (i = 0; i < num_digits(number); i++) {
int mod = rem % 10;
rem = floor(rem / 10);
int powerResult = pow(base1, place++);
num = num + mod * powerResult;
}
int base10_num = num;
printf("The number %i(base%i) in base 10 is: %i\n", number, base1, base10_num);
}
And this calculates the correct base10 number, i.e. the function call base1_convert(2, 5, 100) would return 4, as expected:
The number 100(base2) in base 10 is: 4
The problem arises when I add this code directly beneath the last printf in that function:
int base2_num = 0;
printf("The number %i(base%i) is: %d\n", number, base1, base2_num);
If the above code is added, it completely changes the result of the first printf, making it return:
The number 100(base2) in base 10 is: 4196784.
The number 100(base2) is: 0
I can not for the life of me figure out why this is happening. I'm guessing it has to do with some pointers, but I haven't really used any, and I have no idea why adding a seemingly independent variable int base2_num = 0;
would change all of that.
Here is the complete modified code after the above lines are added.
void base1_base2(int base1, int base2, int number) {
int num, place = 0;
int rem = number;
int i;
for (i = 0; i < num_digits(number); i++) {
int mod = rem % 10;
rem = floor(rem / 10);
int powerResult = pow(base1, place++);
num = num + mod * powerResult;
}
int base10_num = num;
printf("The number %i(base%i) in base 10 is: %i\n", number, base1, base10_num);
int base2_num = 0;
printf("The number %i(base%i) is: %d\n", number, base1, base2_num);
return;
}
Edit: And here is the only other function I used, num_digits. It returns the number of digits in an integer.
int num_digits(int integer) {
char int_string[100];
int str_length;
sprintf(int_string, "%i", integer);
str_length = strlen(int_string);
return str_length;
}
And here is where I call the function:
int main() {
printf("Hello World\n");
base1_base2(2, 5, 100);
return 0;
}
Upvotes: 3
Views: 1448
Reputation: 4366
Just care to write :
int num = 0, place = 0;
And you will be surely done.
That's why I recommend declaring one variable by line.
Upvotes: 7
Reputation: 328594
As you, I don't see what is going on here.
If this was 1990, I'd say that you use 32bit int values and printf()
expects 16bit ints but that can't be true in this case: If there was a width mismatch, then all the other numbers would be wrong, too. So it would print either
The number 0(base100)
or
The number 100(base0)
depending on the endianess of your platform.
Since the problem isn't in your code, it must be elsewhere. Suggestions:
Try to get rid of num_digits()
. See the comments below.
Add more printf()
s to see how all the values change
Run the code in a debugger
Turn off all optimization flags of your compiler. Maybe one of the triggers a bug.
Enable all the warning flags that your compiler supports.
Some comments:
num_digits(number)
is a pretty expensive function; don't call it in a loop (i.e. N times). Cache the result in a local variable instead. Or simply stop when rem
is 0 (i.e. replace the for
loop with while(0 != rem)
).
The name base1
is bad. Use inputBase
instead because that communicates intent.
There is no need to use floor()
; integer division always rounds down.
Upvotes: 2