Advertisement
Guest User

Untitled

a guest
Nov 22nd, 2017
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.00 KB | None | 0 0
  1. import java.util.Scanner;
  2. public class ProgProj06_mc {
  3. public static void main(String[] args) {
  4. Scanner keyboard = new Scanner(System.in);
  5. int arr1 [] = new int [10000];
  6. int arr2 [] = new int [10000];
  7. int arr3 [] = new int [20000];
  8. int a = 0, b = 0, num;
  9.  
  10. System.out.println("Enter the values for the first array, up to 10000 values, enter a negative number to quit");
  11. num = keyboard.nextInt();
  12. while(num > 0) {
  13. arr1[a++] = num;
  14. num = keyboard.nextInt(); }
  15.  
  16. System.out.println("Enter the values for the second array, up to 10000 values, enter a negative number to quit");
  17. num = keyboard.nextInt();
  18. while(num > 0) {
  19. arr2[b++] = num;
  20. num = keyboard.nextInt(); }
  21.  
  22. for(int count = 0; count < a - 1; count++) {
  23. if(arr1[count] > arr1[count+1]) {
  24. System.out.println("ERROR: Array not in correct order");
  25. return; }}
  26.  
  27. for(int count = 0; count < b - 1; count++) {
  28. if(arr2[count] > arr2[count+1]) {
  29. System.out.println("ERROR: Array not in correct order");
  30. return; }}
  31.  
  32. System.out.println("First Array:");
  33. for(int count = 0; count < a; count++)
  34. System.out.print(arr1[count] + " ");
  35.  
  36. System.out.println("\nSecond Array:");
  37. for(int count = 0; count < b; count++)
  38. System.out.print(arr2[count] + " ");
  39.  
  40. int i1 = 0, i2 = 0, i3 = 0;
  41.  
  42. while(arr1[i1] >= 0 || arr2[i2] >= 0)
  43. {
  44. if(arr2[i2] < 0 || (arr1[i1] >= 0 && arr1[i1] < arr2[i2]))
  45. arr3[i3++] = arr1[i1++];
  46. else
  47. arr3[i3++] = arr2[i2++];
  48. }
  49. System.out.println("Merged Array:");
  50. for(int count = 0; count < i3; count++)
  51. System.out.print(arr3[i3] + " ");
  52.  
  53. }
  54. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement