Results 1 to 10 of 10
  1. #1
    Spyderpig is offline Member
    Join Date
    Feb 2011
    Posts
    24
    Rep Power
    0

    Default Subtracting 2d Arrays

    Im trying to subract the array array2 from toReverse.... i know the code is messed up but im not sure how to do it. any help would be appreciated. thanks!




    Java Code:
    	int[][] toReverse = {
    			{11,5,5,5,5},
    			{3,7,8,9,10},
    			{1,2,3,4,5},
    			{1,2,3,4,5},
    			{1,2,3,4,5}
    			};
    	int[][] array2 = {
    			{1,2,3,4,5},
    			{6,7,8,9,10},
    			{11,12,13,14,15},
    			{1,2,3,4,5},
    			{6,7,8,9,10}
    			};
    	
    	
    	for (int i=0;i<toReverse.length;i++){
    		for (int k=0;k<array2.length;k++){
    	int[][]remainingCapacities[][]=toReverse[i][k]-array2[i][k];
    		}
    	}

  2. #2
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,642
    Rep Power
    7

    Default

    i know the code is messed up
    How is it messed up? The answer to this question will probably solve your problem.

    Create a new array that will store the results of the substraction, and assign the subtracted values to the appropriate indexes of the array.
    Java Code:
    int[][] subtracted = new int[][];//you must define the size based upon some criteria
    //loop over the arrays

  3. #3
    Spyderpig is offline Member
    Join Date
    Feb 2011
    Posts
    24
    Rep Power
    0

    Default

    toReverse[i][k]-array2[i][k];

    is messed up... i dont think i can subtract arrays like that can i?

  4. #4
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,642
    Rep Power
    7

    Default

    i dont think i can subtract arrays like that can i?
    Did you try it? What happened?

  5. #5
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,309
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by Spyderpig View Post
    toReverse[i][k]-array2[i][k];

    is messed up... i dont think i can subtract arrays like that can i?
    Subtracting a matrix b from matrix a is no different than subtracting a scalar b from a scalar a. You just need two loops to subtract each element of b from a and you have to keep a matrix ready to store the result; this is some sloppy pseudo code:

    Java Code:
    int[][] a = ...;
    int[][] b = ...;
    int[][] result = ...;
    
    // two nested loops here:
       result[i][j]= a[i][j]-b[i][j];
    that's all there is to it.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  6. #6
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,839
    Rep Power
    8

    Default

    Based on the OP's other post, I'd say the problem is that he doesn't know "how arrays work". But the real problem is his awful attitude!
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  7. #7
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,309
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by KevinWorkman View Post
    Based on the OP's other post, I'd say the problem is that he doesn't know "how arrays work". But the real problem is his awful attitude!
    Hm, I didn't read that thread (now I have) and you most certainly didn't deserve such replies from the OP. If only the OP 'confesses' that he doesn't know how arrays work there wouldn't be much of a problem.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  8. #8
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,642
    Rep Power
    7

    Default

    Quote Originally Posted by KevinWorkman View Post
    Based on the OP's other post, I'd say the problem is that he doesn't know "how arrays work". But the real problem is his awful attitude!
    Indeed. Doesn't help to bite the hand that feeds you

  9. #9
    Spyderpig is offline Member
    Join Date
    Feb 2011
    Posts
    24
    Rep Power
    0

    Default

    thanks JosAH, KevinWOrkman didnt give me any helpful advice so i got frustrated. I obviously dont know everything about array but i know the basics and what he was suggesting was absolutely useless.

  10. #10
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,188
    Rep Power
    19

    Default

    what he was suggesting was absolutely useless.
    Only in the sense of casting pearls before swine.

    db

Similar Threads

  1. store array of arrays in array of arrays
    By joost_m in forum New To Java
    Replies: 4
    Last Post: 04-19-2010, 10:32 AM
  2. Arrays.sort... why sorting all arrays in class?
    By innspiron in forum New To Java
    Replies: 6
    Last Post: 03-23-2010, 01:40 AM
  3. Subtracting Strings
    By ravian in forum New To Java
    Replies: 7
    Last Post: 10-08-2009, 06:26 PM
  4. Subtracting Strings
    By lazygun247 in forum New To Java
    Replies: 6
    Last Post: 03-29-2009, 10:05 PM
  5. 2D Arrays
    By Major90 in forum New To Java
    Replies: 6
    Last Post: 11-06-2008, 02:08 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
  •