0

I am stuck at this part right now with adding the groups together. So like, Array1 A + Array2 A Array1 B + Array 2 B

This is my code:

import java.util.*;
import java.util.Arrays;
public class Math {
    public static final int ARRAY1 = 5;
    public static final int ARRAY2 = 5;
    
public static void main(String[] args) {
    int de;
    de = 1;
    System.out.println("Welcome! This program will assist you in\n 1) Adding groups of numbers\n 2) and subtracting a group of numbers\nPlease enter the number of which you need help with!");
    Scanner keyboard = new Scanner(System.in);
    op = keyboard.nextInt();
    if (op == 1) {
        System.out.println("****Addition****\nAwesome! Please insert all the numbers for your first group of numbers!");
        System.out.println("Enter "+ARRAY1+"");
        int[] array = new int[ARRAY1];
        for(int i=0;i<array.length;i++)
        {
            System.out.println("Enter value "+i);
            array[i] = keyboard.nextInt();
        }
        System.out.println("Great! Now please enter your "+ARRAY2+" other group numbers!");
        int[] array1 = new int[ARRAY2];
        for(int i=0;i<array.length;i++) {
            System.out.println("Enter value "+i);
            array1[i] = keyboard.nextInt();
        }
    
    }
        
    }
    
    
}
1
  • Please clarify your specific problem or provide additional details to highlight exactly what you need. As it's currently written, it's hard to tell exactly what you're asking. Commented Sep 17, 2021 at 9:22

2 Answers 2

1

Initialize the array of the same size and then loop over adding the elements one by one from the 2 input arrays.

int[] sum_array = new int[ARRAY1]; // initialize the output array
for(int i=0;i<array.length;i++) {
    sum_array[i] = array[i] + array1[i]; // adding the elements from two arrays
}
for(int i=0;i<array.length;i++) {
    System.out.print(sum_array[i] + " ");
}
System.out.println();
Sign up to request clarification or add additional context in comments.

Comments

0

The easiest way to solve this is to create another array, let's call it array3, with the same length as your 2 arrays. Then loop over the array3 and store the sum of array1[index] and array2[index] in the array3[index]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.