Results 1 to 11 of 11
4Likes
Thread: int array multiplication
 08202011, 12:12 AM #1Member
 Join Date
 Aug 2011
 Posts
 5
 Rep Power
 0

I hear a voice whispering "for loop" in my ear. What have you tried, and what happened when you tried it?
 08202011, 12:27 AM #3Member
 Join Date
 Aug 2011
 Posts
 95
 Rep Power
 0
Suppose you wrote a five digit number on a piece of paper, like say 27972. Suppose you wrote another five digit number underneath it  say 98374. Now if you only know how to multiply a one digit number by another one digit number (to get a possibly two digit result), do you suppose that you could multiply those two numbers, using pencil and paper, and get the correct result?
If you can do that, then you can probably figure out how to multiply arrays of int values  kind of like how the BigInteger class does it.
 08202011, 12:36 AM #4Member
 Join Date
 Aug 2011
 Posts
 5
 Rep Power
 0
I know I will need a for loop, my first thought would be a nested for loop were one element of one array was multiplied by all elements of the other,this would work if not for the carrying over of the numbers and the extra addition that comes with multiplying multidigit numbers
 08202011, 12:40 AM #5Member
 Join Date
 Aug 2011
 Posts
 5
 Rep Power
 0
perhaps something like this but accounting for carry overs and zeroes
for(int i=0;i<sizeofarray1;i++)
{
for(int j=0;j<sizeofarray2;j++)
{
product=product+(array1[i]*array2[j]);
}
}
I was also thinking of haveing a variable that provided the product of two digits is greater than 10 gets added to the next product, however this still does not help with all the carry over concerns, for instance when you do this by hand you put zeroes depending on which digit is being multiplied.
I also have to have the product be an array or else I risk overflow
here is what I have tried which did not work
int [] multiply(int array1[],int array2[])
{
int[] product=new int[2*size];
int carryover=0;
for(int i=0;i<size;i++)
{
for(int j=0;j<othersize;j++)
{
product[i]=(array1[i]*array2[j])+carryover;
if(product[i]>10)
{
carryover=product[i]/10;
product[i]=product[i]/(carryover*10);
}
}
}
return product;
}
It is pretty hard to wrap my head around this stuff, so any suggestions would be appreciatedLast edited by sternhagenr; 08202011 at 01:03 AM.
 08202011, 12:55 AM #6Member
 Join Date
 Aug 2011
 Posts
 47
 Rep Power
 0
I am not understanding the definition of the problem. Are you trying to do something similar to multiplying matrices, like this?
Matrix Multiplication: How to Multiply Two Matrices Together
if so, two for loops, one inside of the other should be ok for a two 2d arrays, no?Last edited by Willriker; 08202011 at 12:58 AM.
 08202011, 01:13 AM #7Member
 Join Date
 Aug 2011
 Posts
 5
 Rep Power
 0
no, just trying to multiply two 1 dimensional int arrays together treating them as if they are single large numbers
 08202011, 01:21 AM #8Member
 Join Date
 Aug 2011
 Posts
 95
 Rep Power
 0
It can help to go step by step through an example like How to Do Long Multiplication  wikiHow and see how it corresponds to your code. I'd say that 'array1' is the second line of digits, and that 'i' counts from right to left. And the 'product' array is the very last line  value = 24192 in the example at the link above.
So the 'product' array is twice the size of the other two arrays, but it's indexed using 'i', which is limited to half that size. This doesn't seem right.
I see one major problem with how 'carryover' is handled, and one or two more subtle problem(s).
P.S. Other than those issues, it looks pretty good. ;>
 08202011, 01:26 AM #9Member
 Join Date
 Aug 2011
 Posts
 5
 Rep Power
 0
thanks, I will try that
 08202011, 01:50 AM #10arrays together treating them as if they are single large numbers
If I have an array: {2,3,4} and multiplty that by another array: {7,8} what would results be?
There are many ways to look at this:
234*78
2+3+4*7+8
2*7 + 2*8 + 3*7 + 3*8 + 4*7 + 4*8
etc
What would you want the answer to be for the above two arrays?
 08202011, 02:41 AM #11Member
 Join Date
 Aug 2011
 Posts
 95
 Rep Power
 0
Similar Threads

I need fast multiplication of array???
By lulzim in forum Advanced JavaReplies: 1Last Post: 04052011, 06:30 AM 
need help with multiplication
By dakid2 in forum New To JavaReplies: 10Last Post: 03082011, 04:41 AM 
need help with this: multiplication table
By MsIceCold in forum New To JavaReplies: 7Last Post: 07132010, 02:31 PM 
Multiplication Table
By SwEeTAcTioN in forum New To JavaReplies: 4Last Post: 02242010, 05:11 AM 
Help with Multiplication
By phil028 in forum New To JavaReplies: 1Last Post: 12062007, 08:39 PM
Bookmarks