Results 1 to 11 of 11
Like Tree4Likes
  • 1 Post By Fubarable
  • 1 Post By sternhagenr
  • 1 Post By sternhagenr
  • 1 Post By Norm

Thread: int array multiplication

  1. #1
    sternhagenr is offline Member
    Join Date
    Aug 2011
    Posts
    5
    Rep Power
    0

    Default int array multiplication

    does anybody know a way to multiply two int arrays together as if they were two large numbers?

    I cannot use the big integer class for this

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    I hear a voice whispering "for loop" in my ear. What have you tried, and what happened when you tried it?
    JeffGrigg likes this.

  3. #3
    JeffGrigg is offline Member
    Join Date
    Aug 2011
    Posts
    95
    Rep Power
    0

    Default

    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.

  4. #4
    sternhagenr is offline Member
    Join Date
    Aug 2011
    Posts
    5
    Rep Power
    0

    Default

    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 multi-digit numbers

  5. #5
    sternhagenr is offline Member
    Join Date
    Aug 2011
    Posts
    5
    Rep Power
    0

    Default

    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 appreciated
    Last edited by sternhagenr; 08-20-2011 at 12:03 AM.
    JeffGrigg likes this.

  6. #6
    Willriker is offline Member
    Join Date
    Aug 2011
    Posts
    47
    Rep Power
    0

    Default

    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; 08-19-2011 at 11:58 PM.

  7. #7
    sternhagenr is offline Member
    Join Date
    Aug 2011
    Posts
    5
    Rep Power
    0

    Default

    no, just trying to multiply two 1 dimensional int arrays together treating them as if they are single large numbers
    JeffGrigg likes this.

  8. #8
    JeffGrigg is offline Member
    Join Date
    Aug 2011
    Posts
    95
    Rep Power
    0

    Default

    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. ;->

  9. #9
    sternhagenr is offline Member
    Join Date
    Aug 2011
    Posts
    5
    Rep Power
    0

    Default

    thanks, I will try that

  10. #10
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,308
    Rep Power
    25

    Default

    arrays together treating them as if they are single large numbers
    How does that work?
    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?
    JeffGrigg likes this.

  11. #11
    JeffGrigg is offline Member
    Join Date
    Aug 2011
    Posts
    95
    Rep Power
    0

    Default

    Quote Originally Posted by Norm View Post
    How does that work?
    If I have an array: {2,3,4} and multiplty that by another array: {7,8} what would results be?
    ...
    What would you want the answer to be for the above two arrays?
    An excellent question, of course.

    What sternhagenr's code seems to be doing is most like "87 * 432 = ?"

    Putting some thought into what is wanted could be wise.

Similar Threads

  1. I need fast multiplication of array???
    By lulzim in forum Advanced Java
    Replies: 1
    Last Post: 04-05-2011, 05:30 AM
  2. need help with multiplication
    By dakid2 in forum New To Java
    Replies: 10
    Last Post: 03-08-2011, 03:41 AM
  3. need help with this: multiplication table
    By MsIceCold in forum New To Java
    Replies: 7
    Last Post: 07-13-2010, 01:31 PM
  4. Multiplication Table
    By SwEeTAcTioN in forum New To Java
    Replies: 4
    Last Post: 02-24-2010, 04:11 AM
  5. Help with Multiplication
    By phil028 in forum New To Java
    Replies: 1
    Last Post: 12-06-2007, 07:39 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •