Quantcast
Channel: Programming Forums
Viewing all articles
Browse latest Browse all 51036

Multiplying Two Custom Big Integers in Arrays

$
0
0
So I understand that I'm supposed to use the "elementary" algorithm to multiply these two arrays but I keep getting hung up on the actual code. My code is rusty this is my first programming class I've had in a year but I've constructed, compared, and added the two but this one is just really stumping me. This is what I have so far:
public BigInt times(BigInt other) { //multiply two BigInts
         BigInt result = new BigInt();
         BigInt product = new BigInt();
         int carryOver = 0;
      
         result.size = 0; 
         for(int i = 0; i < other.size; i++) {
            carryOver = 0; //makes sure the carry is 0 before it enters the loop for the second time
            if(i > 0) {
               for(int d = 0; d <= i; d++) 
                  result.digit[i-d] = 0;
            }
            for(int x = 0; x < this.size; x++) {
               if(i == 0) { //only increments size the first time through the loop
                  result.size++;
               }
               if(i > 0) {
                  result.digit[i+x] = ((other.digit[i] * this.digit[x]) + carryOver) % 10; //mulitply operation
                  carryOver = (other.digit[i] * this.digit[x]) / 10; 
               } 
               else { 
                  result.digit[x] = ((other.digit[i] * this.digit[x]) + carryOver) % 10; //mulitply operation
                  carryOver = (other.digit[i] * this.digit[x]) / 10; 
               }
               if(x == this.size-1 && carryOver > 0) { //if a carry remains and all the mulitplication has been done
                  result.digit[result.size] = carryOver; //set the last digit to the carry
                  result.size++;
               } 
            }
            for(int x = result.size; x >= 0; x--) {
              
            }
            
            product = product.plus(result);
         }
      
         return product;
      }



And it does not work. It only works if I'm multiplying by 1 or a factor of 10.

Viewing all articles
Browse latest Browse all 51036

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>