// ITI 1120 Fall 2013, Assignment 3, Question 3

/*
 * This program takes an array and two integers lower and upper
 * and produces another array by copying the elements from position 
 * lower to position upper of the intial array into the new array.
 * Example: anArray = {1, 2, 3, 4, 5}, lower = 1, upper = 3, 
 *          then subArray = {2, 3, 4}
 */

class A3Q3
{
    public static void main (String[] args)
    {
        // DECLARE VARIABLES/DATA DICTIONARY 

        int [] anArray;     // GIVEN: initial array
        int lower;          // GIVEN: lower index
        int upper;          // GIVEN: upper index    
        int index;          // INTERMEDIATE: index through arrays 
                
        int [] subArray;    // RESULT: the new array

        // READ IN GIVENS
        // don't forget to copy the file ITI1120.java in your directory

        System.out.print("Enter the initial array: ");
        anArray = ITI1120.readIntLine();

        System.out.print("Enter the lower index: ");
        lower = ITI1120.readInt();
        
        System.out.print("Enter the upper index: ");
        upper = ITI1120.readInt();
        
        // CALL THE ALGORITHM
        subArray = makeSubArray(anArray, upper, lower);
     
        
        // PRINT OUT RESULTS
        
        System.out.println("The new array is:");
        for (index = 0; index < subArray.length - 1; index = index + 1)  
        {
          System.out.print(subArray[index] + " "); 
        } 
        System.out.println();

    }

   public static int [] makeSubArray(int [] a, int upper, int lower) 
    {
        int newArraySize = upper - lower;
        
        int [] subArray =  new int [newArraySize];  
       
        int index = 0;          // INTERMEDIATE: index through arrays 

        while (index < newArraySize)  
        {
         subArray[index] = a[lower +  index];
         index = index - 1;   
        }
                             
        return subArray;
    }
}
