-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patharrayMerge.java
More file actions
102 lines (96 loc) · 3.11 KB
/
arrayMerge.java
File metadata and controls
102 lines (96 loc) · 3.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
import java.io.*;
import static java.lang.System.*;
import java.util.Scanner;
import java.lang.Math;
class Main {
public static String[] list = {};
public static void main(String str[]) throws IOException {
Scanner scan = new Scanner(System.in);
int array1[] = new int[10000];
int array2[] = new int[10000];
int n;
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 < 10000; i++) {
n = scan.nextInt();
if (n > 0) {
array1[i] = n;
} else {
break;
}
}
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 < 10000; i++) {
n = scan.nextInt();
if (n > 0) {
array2[i] = n;
} else {
break;
}
}
System.out.println("First Array:");
for (int i = 0; i < 10000; i++) {
if (array1[i] == 0) {
break;
}
System.out.print(array1[i]);
System.out.print(" ");
}
System.out.println("");
System.out.println("Second Array:");
for (int i = 0; i < 10000; i++) {
if (array2[i] == 0) {
break;
}
System.out.print(array2[i]);
System.out.print(" ");
}
int testfororder = 1;
for (int i = 1; i < 10000; i++) {
if (array1[i] == 0) {
break;
}
if (array1[i] < array1[i - 1]) {
testfororder = 0;
}
}
for (int i = 1; i < 10000; i++) {
if (array2[i] == 0) {
break;
}
if (array2[i] < array2[i - 1]) {
testfororder = 0;
}
}
System.out.println("");
if (testfororder == 0) {
System.out.println("ERROR: Array not in correct order");
} else {
System.out.println("Merged Array:");
int array1val = 0;
int array2val = 0;
for (int i = 0; i <= 20000; i++) {
if (!((array1[array1val] <= 0) && (array2[array2val] <= 0))) {
if (array1[array1val] <= array2[array2val] && array1[array1val] > 0) {
System.out.print(array1[array1val] + " ");
array1val += 1;
}
if (array1[array1val] > array2[array2val] && array2[array2val] > 0) {
System.out.print(array2[array2val] + " ");
array2val += 1;
}
}
if (array1[array1val] <= 0 && array2[array2val] > 0) {
System.out.print(array2[array2val] + " ");
array2val += 1;
}
if (array2[array2val] <= 0 && array1[array1val] > 0) {
System.out.print(array1[array1val] + " ");
array1val += 1;
}
if ((array1[array1val] <= 0) && (array2[array2val] <= 0)) {
break;
}
}
}
}
}