0

I am creating a small java program that does some vector math, and I'm getting stuck with addition and subtraction.

We have to make our own vector class, and all it contains is an Array with its values, so

Vector a = new Vector(1, 2, 3) //this would make a vector with [1, 2, 3]
Vector b = new Vector(4, 3) //this would make a vector with [4, 3]

I cannot do vector addition a + b with what I have now, because if I loop over every index in Vector a, I would get an out of bounds error with Vector b.

How do I make a new array that has all the values of vector b with 0's for the rest, so

//a is [1, 2, 3]
//b is [4, 3]
fix = [4, 3, 0]; //same as b but with trailing 0's so a.length == fix.length
R. Gillie
  • 1,283
  • 2
  • 12
  • 22
  • See https://stackoverflow.com/questions/5785745/make-copy-of-array-java/5785821 for many, many ways to copy arrays. – azurefrog Feb 14 '18 at 19:58
  • I wanted to use an ArrayList, and just check if they weren't the same length, make a new one, set equal, loop until it's filled with 0's, but my professor won't let us use them yet. – R. Gillie Feb 14 '18 at 19:58
  • By definition, vector addition/subtraction requires the same size. Consider throwing an exception instead of trying to "fix" bad/meaningless input. – Andrew S Feb 14 '18 at 19:59
  • Not really, if I have `3i + 4j` for one vector and `6j` for another, the resulting vector would be `3i + 10j`. It's just adding 3 + 0 and 4 + 6. – R. Gillie Feb 14 '18 at 20:03
  • `IntStream.range(0, Math.min(arr1.length, arr2.length)).map(i -> arr1[i] + arr2[i]).toArray()` – shmosel Feb 14 '18 at 20:04
  • @shmosel Wouldn't that trash the remaining values? The longest array length should be kept, not the shortest – phflack Feb 14 '18 at 20:12
  • @phflack Good point. – shmosel Feb 14 '18 at 20:13
  • This is a borderline duplicate of [Make copy of array Java](https://stackoverflow.com/q/5785745/5475891), note that [this answer](https://stackoverflow.com/a/5785754/5475891) works because new arrays are filled with 0s – phflack Feb 14 '18 at 20:33

3 Answers3

0

Pseudocode that fulfils the "don't use any library" requirement of your prof:

int maxlength = max(a.length, b.length)

Vector c = ... // create Vector of maxlength length

for (int i = 0; i < maxlength; i++)
{
  if (i < a.length)
  {
    c[i] += a[i];
  }
  if (i < b.length)
  {
    c[i] += b[i];
  }
}
Max Vollmer
  • 8,412
  • 9
  • 28
  • 43
0

get the length of Vector a , in your case it is 3 get the length of vector b, in your case it is 2

so number of 0 to put in Vector b 3-2=1

use for loop starting with index 2 that is length of Vector b. and loop until the length of Vector a

for(int i=b.length;i<a.length;i++){
    b[i]=0;
}
Roushan
  • 4,074
  • 3
  • 21
  • 38
0

I think you should be able to do this without help. (And I'm sure that's what your teacher wants!)

But here are a couple of hints. You say:

I cannot do vector addition a + b with what I have now, because if I loop over every index in Vector a, I would get an out of bounds error with Vector b.

Step #1: test for the case(s) where you would get the bounds error ... and only do the assignment if you wouldn't get a bounds error; e.g.

  // pseudo-code ... for illustration purposes only
  int len = /* calculate length in source array */
  for (int i = 0; i < len; i++) {
      int j = /* calculate index in target array */
      if (j < targetArray.length) { // test that j is "in bounds"
           targetArray[j] = ...
      }
  }

Step #2: once you have figured that out Step #1, you should be able to hoist the if test out of the loop body by incorporating it into the calculation of len. The Math.max(int, int) function will be useful.

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216