'Dividing arbitrary large numbers stored in char arrays in C
A part of my home assignment requires dividing large integers that are so long I can only store them in character arrays. Both the divident and the divisor can be one to thousands of digits long, therefore the result of the division has to be stored in a char array too, because it can be very lengthy as well. I want to store one digit per index.
I'm trying to use repeated subtractions until the divident is smaller than the divisor and counting the turns. The worst case is when the divisor is 2, so I have to allocate 'length of divident / 2' amount of memory for the counter array.
I already have the subtraction, the getLength and the lengthCompare function implemented and they're functioning fine. Here is what I've done so far:
char *division(char *divident, char *divisor, int len_divident, int len_divisor)
{
if (divident == NULL || divisor == NULL)
{
return NULL;
}
char *cnt = (char*)malloc((len_divident/ 2) * sizeof(char));
char *res = subtraction(divident, divisor, len_divident, len_divisor);
int i = 0;
do
{
res = subtraction(res, divisor, getLength(res), len_divisor);
if (cnt[i] == 9)
{
i++;
}
cnt[i]++;
} while (lengthCompare(res, divisor) == 1 || lengthCompare(res, divisor) == 0);
cnt->digits[i + 1] = '\0';
return cnt;
}
The lengthCompare function returns 1 if the first parameter is longer then the second or 2 if the second one is longer and 0 if their length is equal.
This code doesn't work - every time I compile I get "ERROR - Not enough space" message. Edit: to be specific, it's an exception from the subtraction function: Unhandled exception thrown: write access violation. result was 0x1110112.
Please note that I'm a beginner to C and this is probably not the best method to do what I want but I couldn't think of better.
I much appreciate any cricitism and suggestion!
Edit: n
and a
renamed to divident and divisor.
Solution 1:[1]
The worst case is ... , so I have to allocate 'length of divident / 2' amount of memory for the counter array.
Not quite, the worst case is closer to a subtraction.
Code is not allocating enough memory.
char *cnt = (char*)malloc((len_divident / 2) * sizeof(char)); // Bad
After insuring the divisor, represents a value without leading zeros ...
while (len_divisor > 0 && divisor == '0') {
len_divisor--;
divisor++;
}
...., the space needed does not exceed:
// As it looks like code is using strings
#define SPACE_FOR NULL_CHARACTER 1
size_t length = 1 + SPACE_FOR NULL_CHARACTER;
if (len_dividend > _len_divisor) {
length = len_dividend - len_divisor + 1 + SPACE_FOR NULL_CHARACTER;
}
No need in C for the cast. If one wants to scale by the type of the pointer, better to use sizeof *pointer *
then * sizeof(type)
. It is easier to code right, review and maintain.
char *cnt = malloc(sizeof *cnt * length);
Robust code would check for allocation success.
if (cnt == NULL) {
Handle_OutOfMemory_Somehow();
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 |