Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Division Help

Posted on 2004-08-27
15
Medium Priority
?
290 Views
Last Modified: 2010-04-15
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.
0
Comment
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
  • Learn & ask questions
  • 5
  • 4
  • 2
  • +1
15 Comments
 
LVL 46

Expert Comment

by:Sjef Bosman
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

by:Jaime Olivares
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

by:sonic2000
ID: 11923921
all my individual digits are stored in arrays.
please read the question.
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 46

Expert Comment

by:Sjef Bosman
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

by:sneeuw_chan
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

by:sonic2000
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

by:Sjef Bosman
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

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

Expert Comment

by:sneeuw_chan
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

by:Sjef Bosman
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

by:sonic2000
ID: 12002451
Fine with me
0
 

Accepted Solution

by:
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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use for-loops in the C programming language.

670 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question