Advertisement
StefanTobler

Assignment 6

Nov 16th, 2016
577
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.75 KB | None | 0 0
  1. /*
  2.  
  3. Enter the values for the first array, up to 10000 values, enter zero or a negative number to quit
  4. 3
  5. 3
  6. 5
  7. 6
  8. 8
  9. 9
  10. -1
  11.  
  12. Enter the values for the second array, up to 10000 values, enter zero or a negative number to quit
  13. 3
  14. 4
  15. 5
  16. 6
  17. -5
  18.  
  19. First Array:
  20. 3 3 5 6 8 9
  21.  
  22. Second Array:
  23. 3 4 5 6
  24.  
  25. Merged Array:
  26. 3 3 3 4 5 5 6 6 8 9
  27. Sample Run 2:
  28. Enter the values for the first array, up to 10000 values, enter zero or a negative number to quit
  29. 4
  30. 5
  31. 7
  32. 2
  33. -1
  34.  
  35. Enter the values for the second array, up to 10000 values, enter zero or a negative number to quit
  36. 3
  37. 3
  38. 3
  39. 3
  40. 3
  41. 3
  42. 0
  43.  
  44.  
  45. First Array:
  46. 4 5 7 2
  47.  
  48. Second Array:
  49. 3 3 3 3 3 3
  50.  
  51. ERROR: Array not in correct order
  52.  
  53. */
  54. import java.util.Scanner;
  55. import java.lang.Math;
  56.  
  57. class Main{
  58.     public static void main(String[] args)
  59.      {
  60.       Scanner scan = new Scanner (System.in);
  61.       int input = 0;
  62.       int length = 0;
  63.       int length2 = 0;
  64.       boolean flag = false;
  65.       int[] a1 = new int[10000];
  66.       int[] a2 = new int[10000];
  67.       int[] b = new int[20000];
  68.      
  69.       //First Array
  70.       System.out.println("Enter the values for the first array, up to 10000 values, enter zero or a negative number to quit");
  71.       for(int i = 0; i < a1.length; i++)
  72.       {
  73.         input = scan.nextInt();
  74.         if (input <= 0)
  75.           break;
  76.         else
  77.           a1[i] = input;
  78.       }
  79.      
  80.       //Second Array
  81.       System.out.println("Enter the values for the second array, up to 10000 values, enter zero or a negative number to quit");
  82.       for(int i = 0; i < a2.length; i++)
  83.       {
  84.         input = scan.nextInt();
  85.         if (input <= 0)
  86.           break;
  87.         else
  88.           a2[i] = input;
  89.       }
  90.      
  91.       //Print Arrays
  92.       System.out.println("First Array: ");
  93.       for(int i = 0; i < a1.length; i++)
  94.       {
  95.         if(a1[i] != 0)
  96.         System.out.print(a1[i] + " ");
  97.         else
  98.         {
  99.           length = i;
  100.           break;
  101.         }
  102.       }
  103.      
  104.       System.out.println();
  105.       System.out.println();
  106.      
  107.       System.out.println("Second Array: ");
  108.       for(int i = 0; i < a2.length; i++)
  109.       {
  110.         if(a2[i] != 0)
  111.         System.out.print(a2[i] + " ");
  112.         else
  113.         {
  114.           length2 = i;
  115.           break;
  116.         }
  117.       }
  118.       System.out.println();
  119.       System.out.println();
  120.       //Check if valid
  121.       for(int i = 1; i < length; i++)
  122.       {
  123.         if(a1[i] >= a1[i-1])
  124.           continue;
  125.         else
  126.         {
  127.           flag = true;
  128.           break;
  129.         }
  130.       }
  131.       for(int i = 1; i < length2; i++)
  132.       {
  133.         if(a2[i] >= a2[i-1])
  134.           continue;
  135.         else
  136.         {
  137.           flag = true;
  138.           break;
  139.         }
  140.       }
  141.       int z = 0;
  142.       int y = 0;
  143.       if (flag == true)
  144.       {
  145.       System.out.print("ERROR: Array not in correct order");
  146.       }
  147.       //Merging
  148.       else
  149.       {
  150.       for(int i = 0; i < 10000; i++)
  151.       {
  152.         if (a1[z] != 0 && a2[y] != 0)
  153.         {
  154.         if (a1[z] <= a2[y])
  155.         {
  156.           b[i] = a1[z];
  157.           z++;
  158.         }
  159.         else if (a1[z] > a2[y])
  160.         {
  161.           b[i] = a2[y];
  162.           y++;
  163.         }
  164.         }
  165.         else if (a1[z] == 0 && a2[y] != 0)
  166.         {
  167.           b[i] = a2[y];
  168.           y++;
  169.         }
  170.         else if (a1[z] != 0 && a2[y] == 0)
  171.         {
  172.           b[i] = a1[z];
  173.           z++;
  174.         }
  175.         else if (a1[z] == 0 && a2[y] == 0)
  176.         {
  177.           break;
  178.         }
  179.       }
  180.       //Print Merged Array
  181.       System.out.println("Merged Array: ");
  182.       for(int i = 0; i < a1.length; i++)
  183.       {
  184.         if(b[i] != 0)
  185.         System.out.print(b[i] + " ");
  186.         else
  187.         {
  188.           break;
  189.         }
  190.       }
  191.       }
  192.      
  193.     }
  194. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement