Advertisement
cxgalicia

Assignment 6

Nov 28th, 2014
207
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.47 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. class Main{
  4.   public static void main(String[] args){
  5.    
  6.     Scanner scan = new Scanner(System.in);
  7.    
  8.     int[] first = new int[10000];
  9.     int[] second = new int[10000];
  10.     int cfirst = 0;
  11.     int csecond = 0;
  12.    
  13.     //gets values for first array
  14.     System.out.println("Enter the values for the first array, up to 10000 values, enter a negative number to quit");
  15.     for (int i = 0 ; i < first.length; i++ ) {
  16.      
  17.       first[i] = scan.nextInt();
  18.       cfirst++;
  19.       if(first[i] < 0){
  20.         first [i] = 0;
  21.         break;
  22.       }
  23.     }
  24.     //gets values for second array
  25.     System.out.println("Enter the values for the second array, up to 10000 values, enter a negative number to quit");
  26.     for (int m = 0 ; m < second.length; m++ ) {
  27.      
  28.       second[m] = scan.nextInt();
  29.       csecond++;
  30.       if(second[m] < 0){
  31.         second [m] = 0;
  32.         break;
  33.       }
  34.     }
  35.     //prints first array
  36.     System.out.println("First Array:");
  37.     for(int j=0; j<cfirst-1 ; j++){
  38.       System.out.print(first[j] + " ");
  39.     }
  40.     //prints second array
  41.     System.out.println(" \nSecond Array:");
  42.     for(int k=0; k<csecond-1; k++){
  43.       System.out.print(second[k] + " ");
  44.     }
  45.    
  46.     //code to check is first  && second are ordered
  47.     boolean firstorder = true;
  48.     for (int o = 1; o < cfirst-1; o++) {
  49.       if (first[o] < first[o - 1]){
  50.         firstorder = false;
  51.       }
  52.     }
  53.     boolean secondorder = true;
  54.     for (int f = 1; f < csecond-1; f++) {
  55.       if (second[f] < second[f - 1]){
  56.         secondorder = false;
  57.       }
  58.     }
  59.    
  60.     int[] merged = new int[cfirst-1 + csecond-1];
  61.     if(secondorder && firstorder){
  62.    
  63.    
  64.     int q = 0, w = 0, e = 0;
  65.    
  66.    
  67.    
  68.     while (q < cfirst-1 && w < csecond-1) {
  69.       merged[e] = Math.min(first[q],second[w]);
  70.       if(first[q] >= second[w]) w++;
  71.       else q++;
  72.       e++;
  73.     }
  74.     while(w<csecond-1 || q<cfirst-1){
  75.       if (w >= csecond) merged[e] = first[q];
  76.       else merged[e] = second[w];
  77.       q++;
  78.       w++;
  79.       e++;
  80.     }
  81.    
  82.     }
  83.    
  84.    
  85.     //returns the merged array || returns error
  86.     if(!firstorder || !secondorder){
  87.       System.out.println("\nERROR: Array not in correct order");
  88.     }else if(firstorder && secondorder){
  89.    
  90.         System.out.println("\nMerged Array");
  91.       for(int l=0; l<(cfirst-1 + csecond-1); l++){
  92.         System.out.print(merged[l] + " ");
  93.       }
  94.     }
  95.    
  96.    
  97.   }
  98. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement