• C

# Division Help

I have got an integer array of size 100.
Example, store number 1234, I will store it in the integer array as
mynum[0] = 4;
mynum[1] = 3;
mynum[2] = 2;
mynum[3] = 1;
mynum[4] = 0;
mynum[5] = 0;
mynum[6] = 0;
...
mynum[99] = 0;

I have completed my addition, subtraction and multiplication functions whereby each accept 3 parameters of integer array each 100 size. The result of first_param <operator> second_param will be stored in the third_param. I am stucked in coming out an algorithm for the divison. The division function returns integer division and ignore the remainder.

###### Who is Participating?

Author Commented:
I shall post my solution here.

/*
* c will contain the integer division of a and b
* i.e. 10/3 = 3
*/
void divide_BigNum(int a[], int b[], int c[])
{
int i;
char dividend_string[MAX+1], divisor_string[MAX+1];
char c_string[MAX+1];
int divisor_length, position, dividend_length;
int part[MAX];
char part_string[MAX+1];
int count[MAX];
int temp1[MAX], temp2[MAX];
char char_string[2];

/* initialize c to 0 */
init_BigNum(c);

/* a == b */
if ( compare_BigNum(a, b) == 0 )
c[0] = 1;  /* 1 */
/* a < b */
else if ( compare_BigNum(a, b) == -1 )
return;    /* 0 */
/* a > b */
else
{
print_BigNum(a, dividend_string);
print_BigNum(b, divisor_string);
strcpy(c_string, "");

dividend_length = strlen(dividend_string);
divisor_length = strlen(divisor_string);
position = divisor_length - 1;

/* get part */
strcpy(part_string, "");
strncat(part_string, dividend_string, divisor_length);
string_to_BigNum(part_string, part);

while ( position <= dividend_length )
{
/* part >= b */
if ( compare_BigNum(part, b) >= 0 )
{
/* set count to default 1 */
init_BigNum(count);
count[0] = 1;

/* loop until the correct number to be put on quotient is found */
while ( 1 )
{
multiply_BigNum(count, b, temp1);
if ( compare_BigNum(temp1, part) == 1 )
{
count[0]--;
multiply_BigNum(count, b, temp1);
break;
}

count[0]++;
}

subtract_BigNum(part, temp1, temp2);
for ( i=0; i<MAX; i++ )
part[i] = temp2[i];
print_BigNum(part, part_string);
char_string[0] = dividend_string[position+1];
char_string[1] = '\0';
strcat(part_string, char_string);
string_to_BigNum(part_string, part);
char_string[0] = count[0] + '0';
char_string[1] = '\0';

if ( position + 1 <= dividend_length )
strcat(c_string, char_string);
}
/* part < b */
else
{
char_string[0] = dividend_string[position+1];
char_string[1] = '\0';
strcat(part_string, char_string);
string_to_BigNum(part_string, part);

if ( position + 1 <= dividend_length )
strcat(c_string, "0");
}

position++;
}

/* convert the c_string to c for return */
string_to_BigNum(c_string, c);
}
}

0

Groupware ConsultantCommented:
What's your algorithm, in pseudo-code? It must be very much like the primary school's division scheme, for it is essentially nothing more than a repeated subtraction, and that algorithm you already have.
0

Software ArchitectCommented:
to obtain remain, just use the modulo operator:

int a= 10;
int b= 3;
int c;
int d;

c = a / b;      /* c becomes 3  */
d = a % b;    /* d becomes 1. The modulo operator: % */

but you have to be careful with divisions, because of divide by zero. It will be better

c =(b==0) ? 0 : (a/b);      /* This will assign 0 the c if b = 0 */
0

Author Commented:
all my individual digits are stored in arrays.
0

Groupware ConsultantCommented:
I understood you perfectly, I think. You'd best mimic the primary school division method, like
12345 : 67
-67
564
-536
285
-268
17

So look for the leftmost non-zero digit, take as many digits as the divisor, check what happens if you subtract once, if the result is less than zero, take a digit more if any and start subtracting again. At least, that's how I would go about it.

Hope this helps.
0

Commented:
I agree with sjef.  Long division as taught in school is an easy way to do it.

Does it need to be very fast, by the way, or is this just a proof of concept type thing (or homework even) ?  If it needs to be fast, you're in a whole different ballpark.
0

Author Commented:
but it's really difficult to put into code..
Tried for many many days..
could anyone help out?
0

Groupware ConsultantCommented:
What have you got by now? I prefer pseudo-code at first, to see the algorithm. Later, we can switch to C if required.
0

Author Commented:
I would like to close this question.
Answer not gotten from this question.
Objections?
0

Commented:
If the answer you want is a direct algorithm for long division, I think most people are afraid it's a homework-like question, so they want you to show how far you've gotten and help you out in the area you're stuck.  In such a case, people won't simply give away the entire answer.  (Besides which, if you find out the answer yourself, with help, you learn from it, whereas if you just copy it from here, you've learned nothing.)

If you still want help with this, just start by writing down in words how long division works, (You know, how they taught you in school to divide with pen and paper).  If all you want is the algorithm, then the answer is 'don't write your own but use a library'.
0

Groupware ConsultantCommented:
sneeuw_chan,

Check the next question from sonic. He found the answer "himself" and intends to get a refund. We all tried to supply some indispensable info, and I'm sure he used it. I don't object a refund, but it would result in some negative feedback to his address.

Sjef
0

Author Commented:
Fine with me
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.