# Subtracting 2d Arrays

• 03-10-2011, 04:00 PM
Spyderpig
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!

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];                 }         }```
• 03-10-2011, 04:18 PM
doWhile
Quote:

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.
Code:

```int[][] subtracted = new int[][];//you must define the size based upon some criteria //loop over the arrays```
• 03-10-2011, 04:48 PM
Spyderpig
toReverse[i][k]-array2[i][k];

is messed up... i dont think i can subtract arrays like that can i?
• 03-10-2011, 05:28 PM
doWhile
Quote:

i dont think i can subtract arrays like that can i?
Did you try it? What happened?
• 03-10-2011, 08:03 PM
JosAH
Quote:

Originally Posted by Spyderpig
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:

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
• 03-10-2011, 08:07 PM
KevinWorkman
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!
• 03-10-2011, 08:11 PM
JosAH
Quote:

Originally Posted by KevinWorkman
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
• 03-10-2011, 09:15 PM
doWhile
Quote:

Originally Posted by KevinWorkman
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
• 03-10-2011, 11:25 PM
Spyderpig
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.
• 03-11-2011, 06:12 AM
DarrylBurke
Quote:

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

db