Solved

# Division Help

Posted on 2004-08-27
280 Views
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.

0
Question by:sonic2000
[X]
###### Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

• Help others & share knowledge
• Earn cash & points
• 5
• 4
• 2
• +1

LVL 46

Expert Comment

ID: 11919733
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

LVL 55

Expert Comment

ID: 11922378
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 Comment

ID: 11923921
all my individual digits are stored in arrays.
0

LVL 46

Expert Comment

ID: 11924793
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

LVL 2

Expert Comment

ID: 11956086
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 Comment

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

LVL 46

Expert Comment

ID: 11961261
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 Comment

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

LVL 2

Expert Comment

ID: 11979492
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

LVL 46

Expert Comment

ID: 11979637
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 Comment

ID: 12002451
Fine with me
0

Accepted Solution

sonic2000 earned 0 total points
ID: 12013402
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

## Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

### Suggested Solutions

This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn hoâ€¦
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see soâ€¦
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.
The goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.
###### Suggested Courses
Course of the Month2 days, 20 hours left to enroll