0

I'm supposed to add two numbers, in the form of an array,

example

839 as an array, {9,3,8}

2039 {9,3,0,2}

The reason it is backward is because we're required to start from the one's place. I just can't figure what the size of the array should be.

public static int[] add(int [] x, int[] y){

     int[] z = new int [x.length];

     int j=0,
         hold,
         i;

     for(i=0; i<=z.length-1;i++) {

        z[i]=(x[i]+y[i]+j)%10;
        j=(x[i]+y[i]+j)/10;

     }

This is what I have currently, but the size of the array is the problem. I feel like I've tried everything but idk.

nimi0112
  • 2,065
  • 1
  • 18
  • 32
Fluxia
  • 19
  • 1

3 Answers3

0

It's not actually possible to determine the exact size of the result without performing the addition; all you really know for sure upfront is that it will be either Math.max(x.length, y.length) or Math.max(x.length, y.length) + 1.

So, I think there are three approaches:

  • You can always allocate Math.max(x.length, y.length) + 1, and accept that sometimes there will be an extra leading zero.
    • But in this case, in order to avoid adding more and more zeroes with every single addition, you'll probably want to pre-count the number of leading zeroes in x and y, and discount those from your total. This ends up being unnecessarily complicated IMHO.
  • You can do the addition twice: once to determine the length so you can create the array, and once to actually populate it with the result.
  • You can initially allocate Math.max(x.length, y.length) + 1 and do the addition, and then if the most-significant-digit turned out to be zero, you can then allocate Math.max(x.length, y.length) and copy all but that zero. (You may want to use java.lang.System.arraycopy(...) for this.)
ruakh
  • 175,680
  • 26
  • 273
  • 307
  • Unfortunately my professor is very anal and isn't letting us us Math. Otherwise I would've been done already, is there another way to do it? – Fluxia Apr 18 '18 at 04:49
  • @Fluxia: I'm not saying you need to use `Math`. I know Java, and you know Java, so `Math.max(x.length, y.length)` is just a convenient way for me to tell you "either `x.length` or `y.length`, whichever is greater". It's trivial for you to implement that using an `if` statement, or using the ternary operator `... ? ... : ...` if you're familiar with it. – ruakh Apr 18 '18 at 04:51
0

If you use an Integer object instead of a primitive int you can do the following.

Integer x = 1234;
x.toString().length();
4b0
  • 21,981
  • 30
  • 95
  • 142
0

What about this one?

public static int[] add(int[] x, int[] y) {
    int[] z = new int[x.length + y.length];
    int i = 0;

    for (int j = 0; i < x.length || i < y.length || j > 0; i++) {
        int sum = (i < x.length ? x[i] : 0) + (i < y.length ? y[i] : 0) + j;
        z[i] = sum % 10;
        j = sum / 10;
    }

//    return Arrays.copyOf(z, i);

    int[] res = new int[i];
    System.arraycopy(z, 0, res, 0, res.length);

    return res;
}
Oleg Cherednik
  • 17,377
  • 4
  • 21
  • 35