In this whiteboard video, Professor Wool highlights the challenges, benefits and trade-offs of utilizing zero-touch automation for security policy change management. Watch and Learn!

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.

Please help.

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.

Please help.

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 */

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.

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.

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'.

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.

All Courses

From novice to tech pro — start learning today.

/*

* 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_stri

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_stri

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_stri

if ( position + 1 <= dividend_length )

strcat(c_string, "0");

}

position++;

}

/* convert the c_string to c for return */

string_to_BigNum(c_string,

}

}