Link to home
Start Free TrialLog in
Avatar of sonic2000
sonic2000

asked on

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.

Please help.
Avatar of Sjef Bosman
Sjef Bosman
Flag of France image

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.
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 */
Avatar of sonic2000
sonic2000

ASKER

all my individual digits are stored in arrays.
please read the question.
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.
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.
but it's really difficult to put into code..
Tried for many many days..
could anyone help out?
What have you got by now? I prefer pseudo-code at first, to see the algorithm. Later, we can switch to C if required.
I would like to close this question.
Answer not gotten from this question.
Objections?
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'.
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
Fine with me
ASKER CERTIFIED SOLUTION
Avatar of sonic2000
sonic2000

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial