Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Enter the values for the first array, up to 10000 values, enter zero or a negative number to quit
- 3
- 3
- 5
- 6
- 8
- 9
- -1
- Enter the values for the second array, up to 10000 values, enter zero or a negative number to quit
- 3
- 4
- 5
- 6
- -5
- First Array:
- 3 3 5 6 8 9
- Second Array:
- 3 4 5 6
- Merged Array:
- 3 3 3 4 5 5 6 6 8 9
- Sample Run 2:
- Enter the values for the first array, up to 10000 values, enter zero or a negative number to quit
- 4
- 5
- 7
- 2
- -1
- Enter the values for the second array, up to 10000 values, enter zero or a negative number to quit
- 3
- 3
- 3
- 3
- 3
- 3
- 0
- First Array:
- 4 5 7 2
- Second Array:
- 3 3 3 3 3 3
- ERROR: Array not in correct order
- */
- import java.util.Scanner;
- import java.lang.Math;
- class Main{
- public static void main(String[] args)
- {
- Scanner scan = new Scanner (System.in);
- int input = 0;
- int length = 0;
- int length2 = 0;
- boolean flag = false;
- int[] a1 = new int[10000];
- int[] a2 = new int[10000];
- int[] b = new int[20000];
- //First Array
- System.out.println("Enter the values for the first array, up to 10000 values, enter zero or a negative number to quit");
- for(int i = 0; i < a1.length; i++)
- {
- input = scan.nextInt();
- if (input <= 0)
- break;
- else
- a1[i] = input;
- }
- //Second Array
- System.out.println("Enter the values for the second array, up to 10000 values, enter zero or a negative number to quit");
- for(int i = 0; i < a2.length; i++)
- {
- input = scan.nextInt();
- if (input <= 0)
- break;
- else
- a2[i] = input;
- }
- //Print Arrays
- System.out.println("First Array: ");
- for(int i = 0; i < a1.length; i++)
- {
- if(a1[i] != 0)
- System.out.print(a1[i] + " ");
- else
- {
- length = i;
- break;
- }
- }
- System.out.println();
- System.out.println();
- System.out.println("Second Array: ");
- for(int i = 0; i < a2.length; i++)
- {
- if(a2[i] != 0)
- System.out.print(a2[i] + " ");
- else
- {
- length2 = i;
- break;
- }
- }
- System.out.println();
- System.out.println();
- //Check if valid
- for(int i = 1; i < length; i++)
- {
- if(a1[i] >= a1[i-1])
- continue;
- else
- {
- flag = true;
- break;
- }
- }
- for(int i = 1; i < length2; i++)
- {
- if(a2[i] >= a2[i-1])
- continue;
- else
- {
- flag = true;
- break;
- }
- }
- int z = 0;
- int y = 0;
- if (flag == true)
- {
- System.out.print("ERROR: Array not in correct order");
- }
- //Merging
- else
- {
- for(int i = 0; i < 10000; i++)
- {
- if (a1[z] != 0 && a2[y] != 0)
- {
- if (a1[z] <= a2[y])
- {
- b[i] = a1[z];
- z++;
- }
- else if (a1[z] > a2[y])
- {
- b[i] = a2[y];
- y++;
- }
- }
- else if (a1[z] == 0 && a2[y] != 0)
- {
- b[i] = a2[y];
- y++;
- }
- else if (a1[z] != 0 && a2[y] == 0)
- {
- b[i] = a1[z];
- z++;
- }
- else if (a1[z] == 0 && a2[y] == 0)
- {
- break;
- }
- }
- //Print Merged Array
- System.out.println("Merged Array: ");
- for(int i = 0; i < a1.length; i++)
- {
- if(b[i] != 0)
- System.out.print(b[i] + " ");
- else
- {
- break;
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement