atanasovetr

ArraySubstraction

Jan 22nd, 2020
252
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.76 KB | None | 0 0
  1. import java.util.Arrays;
  2. import java.util.Scanner;
  3. import java.io.*;
  4. public class ArraySubstraction {
  5.     public static void main(String[] args) {
  6.         Scanner scan = new Scanner(System.in);
  7.         System.out.print("How many numbers you want to substract: ");
  8.         int n = scan.nextInt();
  9.         double [] firstArray = filling(n,"first") ;
  10.         double [] secondArray = filling(n,"second") ;
  11.         double [] result = filling_result(firstArray, secondArray);
  12.         sort(result);
  13.  
  14.         System.out.println(Arrays.toString(result));
  15.     }
  16.  
  17.     public static double[] filling(int elementQuantity, String arrayOrder){
  18.         double[] array = new double[elementQuantity];
  19.         System.out.println(String.format("Enter the numbers from your %s array: ", arrayOrder));
  20.         for (int i = 0; i < elementQuantity; i++){
  21.             Scanner scan = new Scanner(System.in);
  22.             double number = scan.nextDouble();
  23.             array[i] = number;
  24.         }
  25.         return array;
  26.     }
  27.  
  28.     public static double[] filling_result(double[] firstArray, double[] secondArray){
  29.         System.out.println("This is the result from the substraction: ");
  30.         int length = firstArray.length;
  31.         double[] array = new double[length];
  32.         for (int i = 0; i < length; i++){
  33.             double number = firstArray[i] - secondArray[i];
  34.             array[i] = number;
  35.         }
  36.         return array;
  37.     }
  38.     public static void sort(double arr[]) {
  39.         int n = arr.length;
  40.         for (int i = 1; i < n; ++i) {
  41.             double key = arr[i];
  42.             int j = i - 1;
  43.  
  44.             while (j >= 0 && arr[j] > key) {
  45.                 arr[j + 1] = arr[j];
  46.                 j = j - 1;
  47.             }
  48.             arr[j + 1] = key;
  49.         }
  50.     }
  51. }
Advertisement
Add Comment
Please, Sign In to add comment