Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Arrays;
- import java.util.Scanner;
- import java.io.*;
- public class ArraySubstraction {
- public static void main(String[] args) {
- Scanner scan = new Scanner(System.in);
- System.out.print("How many numbers you want to substract: ");
- int n = scan.nextInt();
- double [] firstArray = filling(n,"first") ;
- double [] secondArray = filling(n,"second") ;
- double [] result = filling_result(firstArray, secondArray);
- sort(result);
- System.out.println(Arrays.toString(result));
- }
- public static double[] filling(int elementQuantity, String arrayOrder){
- double[] array = new double[elementQuantity];
- System.out.println(String.format("Enter the numbers from your %s array: ", arrayOrder));
- for (int i = 0; i < elementQuantity; i++){
- Scanner scan = new Scanner(System.in);
- double number = scan.nextDouble();
- array[i] = number;
- }
- return array;
- }
- public static double[] filling_result(double[] firstArray, double[] secondArray){
- System.out.println("This is the result from the substraction: ");
- int length = firstArray.length;
- double[] array = new double[length];
- for (int i = 0; i < length; i++){
- double number = firstArray[i] - secondArray[i];
- array[i] = number;
- }
- return array;
- }
- public static void sort(double arr[]) {
- int n = arr.length;
- for (int i = 1; i < n; ++i) {
- double key = arr[i];
- int j = i - 1;
- while (j >= 0 && arr[j] > key) {
- arr[j + 1] = arr[j];
- j = j - 1;
- }
- arr[j + 1] = key;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment