sylvtronix
sylvtronix

Reputation: 5

Arrays-Java, sum of the elements in same positions with the array

I have an exercise and find out what went wrong. I will be grateful for help :)

Write the code that will have two arrays of type int and the return array greater (if one of the two arrays is greater) and the sum of the elements in same positions with the array.

public class TwoArrays {
    public static void main(String[] args) {
        int[] arrayA = {1, 5, 2, 6, 8};
        int[] arrayB = {4, 5, 7, 10, 7, 9, 7};
        int lengthA = arrayA.length;
        int lengthB = arrayB.length;
        int i=0;
        if (lengthA < lengthB) {
            for (arrayB[i] = 0; i < lengthB; i++) {
                arrayB[i] = arrayA[i] + arrayB[i];
                System.out.println(arrayB[i]);
            }

        } else if (lengthA > lengthB) {
            for (arrayA[i] = 0; i < lengthA; i++) {
                arrayA[i] = arrayA[i] + arrayB[i];
                System.out.println(arrayA[i]);
            }

        } else {
            for (arrayB[i] = 0; i < lengthB; i++) {
                arrayB[i] = arrayA[i] + arrayB[i];
                System.out.println(arrayB[i]);
            }
        }
    }
}

Upvotes: 0

Views: 406

Answers (4)

Akash R
Akash R

Reputation: 129

There are few corrections in your code.

  1. The index value used for iteration should start from 0.
  2. arrayB[i] = arrayA[i] + arrayB[i];
    arrayA[i] = arrayA[i] + arrayB[i];

In the above lines, there are chances that JVM can throw ArrayIndexOutOfBoundsException if the index value(i) exceeds the length of lengthA orlengthB. So it should be checked on every iteration. Once the index value exceeds the lengthA or lengthB, zero can used as default value to be added with arrayB[i]. So I have used ternary operator to check the index value on each iteration. 3. And No need of else if block. As we are using only 2 arrays just if and else who be sufficient in your case.

Modified code:

public class TwoArrays {
    public static void main(String[] args) {
        int[] arrayA = {1, 5, 2, 6, 8};
        int[] arrayB = {4, 5, 7, 10, 7, 9, 7};
        int lengthA = arrayA.length;
        int lengthB = arrayB.length;

        if (lengthA < lengthB) {
            for (int i = 0; i < lengthB; i++) {
                int temp = i < lengthA ? arrayA[i] : 0; // used ternary operator here
                arrayB[i] = arrayB[i] +  temp;   
                System.out.println(arrayB[i]);
            }

        } else {
            for (int i = 0; i < lengthA; i++) {
                int temp = i < lengthB ? arrayB[i] : 0; // used ternary operator here
                arrayA[i] = arrayA[i] + temp;   
                System.out.println(arrayA[i]);
            }
        }
    }
}

Upvotes: 0

Nowhere Man
Nowhere Man

Reputation: 19545

A bigger array should be selected (or the last one) and the elements should be added until the end of the shorter array.

public static int[] joinArrays(int[] a, int[] b) {
    int[] shorter, bigger;
    if (b.length < a.length) {
        shorter = b;
        bigger = a;
    } else {
        shorter = a;
        bigger = b;
    }
    for (int i = 0; i < shorter.length; i++) {
        bigger[i] += shorter[i];
    }
    return bigger;
}

Test:

int[] arrayA = {1, 5, 2, 6, 8};
int[] arrayB = {4, 5, 7, 10, 7, 9, 7};
System.out.println(Arrays.toString(joinArrays(
    new int[]{1, 5, 2,  6, 8}, 
    new int[]{4, 5, 7, 10, 7, 9, 7}
)));

Output:

[5, 10, 9, 16, 15, 9, 7]

Upvotes: 0

LuckyLakshoo
LuckyLakshoo

Reputation: 16

Try to check the condition of the first For-Loop again. If you try to access an array at a position, which is larger than the array length you will get a java.lang.ArrayIndexOutOfBoundsException. I suggest you should try to Debug this Code if you have trouble to understand it.

Upvotes: 0

Romain Pr&#233;vost
Romain Pr&#233;vost

Reputation: 543

It should be

for (int i = 0; i < lengthB; i++)

When you use

for (arrayB[i] = 0; i < lengthB; i++)

You set the first element of your array to 0, not the i index variable like you probably want to.

Also, you should do something like finding which array is larger, then using a variable like bigArray = arrayA, smallArray = arrayB. Then, you only have to write a single for Loop.

Upvotes: 1

Related Questions