### Multiply two dynamic arrays

By using operator overloads i need to be able to * two dynamic arrays;

this is what i have so far
 ``1234567891011121314151617181920`` ``````MyInt operator* (const MyInt& x, const MyInt& y) { MyInt Temp; int carry = 0; if(x.currentSize > y.currentSize) { for( int i =0; i < y.currentSize; i++) for(int j = 0; j < x.currentSize; j++) { int k = ( y.digits[i] * x.digits[j] + carry); Temp.digits[j] = (k % 10); carry = (k /10); Temp.currentSize++; if(Temp.currentSize == Temp.maxSize) Temp.Grow(); } } return Temp; }``````

Could someone please help with the structure of this. My brain is currently fried and need a new set of eyes!
Last edited on
You forgot to ask a question ;)
*question added per LB
Are you trying to perform matrix multiplication? If not, could you show example x, y, and result matrices? It's important to know how you define 'multiply' in this case.
MyInt is a class for Dynamic int arrays. This class was built to accept any size integer and store it. So what i am trying to do here is multiply one int array by another int array.

example:

int 12345678 is stored in a array backwards like

[8][7][6][5][4][3][2][1]
0 1 2 3 4 5 6 7 <-- array positions
Last edited on
Ah, you're trying to make arbitrary precision. You've made the task a little hard by storing whole integers for each digit and doing it in bae 10 - have you considered a std::vector<bool> instead, to simulate arbitrary number of bits?
The easiest way to do it would be to imagine it just like long multiplication (that you may have done in school). Basically, go from the back end of the multiplicator, multiply that value to all the other value, and add it to a running total. Then, iterate one back from the end of the array, push an empty 0 onto the front of your array (multiply it by 10, like a binary shift), and repeat, adding onto the sum.

Also, you should really take LB's suggestion seriously, because I have done this in base 10 before (well, really base 10,000 - I was feeling storage space efficient at the time), and division / modulus just makes your head spin...
Last edited on
Thank you both! NT3 explanation helped me visualize it better, also LB im unfamiliar with vectors so i looked it up and its something i need to learn.
Topic archived. No new replies allowed.