user1757703
user1757703

Reputation: 3015

Java - Adding contents of two unequal arrays

Lets say array one [2/3, 0, -1, 0, 7/2] and array two [0, 0, -2/3, 1, 0, 0] so I want my result array to be [0, 2/3, -2/3, 0, 0, 7/2]. The result array length will be the max length between the two arrays. How can I do this in Java?

Pretty much I want the specific index locations to add each other however I don't know how to do this with unequal arrays.

Edit: It adds the locations and anything that is unmatched is left untouched in the largest array. [0, 0, -2/3, 1, 0, 0] has location 0, 1, 2, 3, 4, 5 and array [2/3, 0, -1, 0, 7/2] has locations that coincide with the larger array as 1, 2, 3, 4, 5 so I want the same location values to be added and placed into the resultant array. I created a new resultant array and set it equal to the largest array so all that has to be done is the adding of similar location values.

Upvotes: 0

Views: 1755

Answers (4)

Jose
Jose

Reputation: 340

Here is an elaborate and easy to understand way that I've devised:

What it does it it adds the last elements of the arrays together and moves backwards from there; if one array ends before the other, it just substitutes the value of the non-existent element with zero, then adds them:

public class ArrayAddition
{
public static void main(String[] args)
{
    double array1[] = {2./3, 0, -1, 0, 7./2}; // first array
    double array2[] = {0, 0, -2./3, 1, 0, 0}; // second array
    int length = Math.max(array1.length, array2.length); // length of longest array
    double newArray[] = new double[length]; // result must be length of longest array

    int index1 = array1.length - 1; // last element of first array
    int index2 = array2.length - 1; // last element of second array
    int indexRes = length - 1;      // result will be placed in last spot of result

    for (int i = length -1; i >= 0; i--) // adds elements of two arrays together bckwrd
    {

        double val1, val2;     // value holders for array elements

        try  // try to get value of the array 1 at certain position
        {
            val1 = array1[index1];
        }
        catch(ArrayIndexOutOfBoundsException e)  // if empty, make it zero
        {
          val1 = 0;
        }

        try   // try to get value of array 2 at certain position
        {
            val2 = array2[index2];
        }
        catch(ArrayIndexOutOfBoundsException e) // if empty make it zero
        {
            val2 = 0;
        }

        newArray[indexRes] = val1 + val2; // array[?] result is val1 + val 2
        index1--;  // decrement to the next lower value
        index2 --; // decrement to the next lower value
        indexRes--; // go the next lower spot


    }

    for (int i = 0; i < newArray.length; i ++)  // this loop prints out the results
        System.out.println(newArray[i]);

}

}

You need to enter your values as doubles or the answers will be incorrect (2./3 instead of 2/3)

0.0
0.6666666666666666
-0.6666666666666666
0.0 
0.0
3.5

Answers will be in decimal form, for obvious reasons (if answer is 2 / 3, it actually divides 2 by 3, still the correct answer, you can convert it back)

Hopefully this helps! :)

Upvotes: 2

Naddy
Naddy

Reputation: 2674

import java.util.Scanner;

public class ArrayAdd {
    public static void main(String args[]) {
        Scanner a = new Scanner(System.in);
        int m = a.nextInt();// First array's size
        int n = a.nextInt();// Second array's size
        int arr1[] = new int[m];
        int arr2[] = new int[n];
        for (int i = 0; i < m; i++) {
            arr1[i] = a.nextInt();
        }
        for (int i = 0; i < n; i++) {
            arr2[i] = a.nextInt();
        }
        a.close();
        if (m < n) {
            int difference = n - m;
            int arr3[] = new int[n];
            for (int i = 0; i < n; i++) {
                if (i < difference) {
                    arr3[i] = arr2[i];
                } else {
                    arr3[i] = arr1[i-difference] + arr2[i];
                }
                System.out.println(arr3[i]);
            }
        } else {
            int difference = m - n;
            int arr3[] = new int[m];
            for (int i = 0; i < m; i++) {
                if (i < difference) {
                    arr3[i] = arr1[i];
                } else {
                    arr3[i] = arr1[i] + arr2[i-difference];
                }
                System.out.println(arr3[i]);
            }
        }

    }
}

Upvotes: 0

Jay Nebhwani
Jay Nebhwani

Reputation: 976

This should do it. Note that this code is missing the declarations of the array variables.

if (array1.length > array2.length)
  array3 = addArrays(array1, array2);
else
  array3 = addArrays(array2, array1);


int [] addArrays(longArray, shortArray) {
  int index;
  for (index = 0; index < longArray.length - shortArray.length; index++) {
    array3[index] = longArray[index] + 0;
  }
  for (int i = 0; i < shortArray.length; i++, index++) {
    array3[index] = longArray[index] + shortArray[i];
  }
  return array3;
}

Upvotes: 0

ug_
ug_

Reputation: 11440

Go through your arrays starting at the end and add the 2 values putting them into a new array with the size of the largest array.

int a = arrayA.length-1;
int b = arrayB.length-1;

double [] result = new double[Math.max(arrayA.length, arrayB.length)];
double sum = 0;
while(a >= 0 || b >= 0) {
    if(a>=0) sum+=arrayA[a];
    if(b>=0) sum+=arrayB[b];

    result[Math.max(a, b)] = sum;
    sum = 0;
    a--;
    b--;
}

Upvotes: 0

Related Questions