CmSc 150 Fundamentals of Computing I


Laboratory exercise 11: Arrays

Purpose: Practice array processing

Program development

Download the project Lab11-Arrays.zip in the folder CMSC150 and unzip it. Delete the zipped file. Rename the project so that its name starts with your first name.

Open Java IDE, open the project. The project contains two classes: ArraysLab and ArraysDriverLab - solution of Lab10.

Tasks

  1. Creating an array with unique elements in ascending order
    1. In the ArraysLab class implement a method to create an array in ascending order
    2. public static  void createArrayAscending(int[] array, int size, int upper, int increment)
      The “upper” parameter is used to create the first element in the array.
      Each next element in the array is made equal to the previous element plus a random number between 1 and “increment”.

    3. To test this method, In the ArraysDriverLab class add an option “test” to do the following:
      1. Ask the user to enter the size, the upper limit and the increment and read the entered values
      2. Instantiate an array with the given size
      3. Call the createArrayAscending method to create the array
      4. Print the created array.
  2. Creating an array with unique elements in random order
  3. The method createArrayAscending will create an array with unique elements, however ordered. To make the elements in random order, we need to shuffle them.

    1. In the ArraysLab class implement a method to shuffle the elements in an array.
    2. public static void shuffle(int[]array)

      To shuffle the elements, we choose randomly two elements (by choosing randomly two indexes) and swap them. This is repeated in a loop that runs a sufficient number of times, e.g. close to 1000.

      Hint: when randomly generating indexes, remember that the scope is from 0 to the size of the array.

    3. In the test option add a call to the shuffle method and print the elements of the shuffled array.
  4. Union of two arrays with unique elements
  5. The union arrayC of two arrays arrayA and arrayB contains all elements of arrayA and arrayB without repetition.

    1. In the ArraysLab class implement a method that given two arrays, constructs a third array that contains the union of the two arrays
    2. public static int findUnion(int[] arrayA, int[] arrayB, int[] arrayC)

      First, we copy all elements of arrayA into arrayC.
      Then we compare each element of arrayB with each element of arrayA, and if it is not there, we copy it into arrayC.

      The method will return the number of elements copied into arrayC.

      Algorithm in pseudocode (begin – end is used to indicate loop body):

      1.	Initialize k to 0
      2.	For all elements arrayA[i] in arrayA do
                begin
                   arrayC[k] ← arrayA[i]
                   k ← k+1
                end
      
      3.	For all elements arrayB[j] in arrayB do
                begin
                  Initialize a boolean variable found to false
                  For all elements arrayA[i] , while found is false do
                    begin
      	        if arrayB[j] is equal to arrayA[i]
      		   found ← true
                    end
      
                  if found is false
      	           arrayC[k] ← arrayB[j]
                         k ← k+1
                end
      4.	return k

      Hint: The header of a loop that says:

      For all elements arrayA[i] , while found is false do would be:
      for(i = 0; i < sizeA && !found; i++)
      where sizeA is the size of arrayA

    3. In the driver class, rename the option “test” to “union”
    4. Modify the case “union” to create the two arrays A and B with unique elements in random order, and then call the method findUnion to construct arrayC.

      Print how many elements have been copied into arrayC, and print the arrayC.

      Hint: Do not forget that the method findUnion returns a value that needs to be stored somewhere and then used when printing arrayC.

  6. Intersection of two arrays with unique elements.
  7. The intersection arrayC of two arrays arrayA and arrayB with unique elements contains only the elements that appear in both arrays.

    1. In the ArraysLab class implement a method that given two arrays constructs a third array that contains the intersection of the two arrays.
    2. public static int common(int[] arrayA, int[] arrayB, int[] arrayC)

      The algorithm roughly is the following:

      We compare each element of arrayA with the elements in arrayB. If we find it, we copy it into arrayC.

      Algorithm in pseudocode (begin – end is used to indicate loop body):

      1.	Initialize k to 0
      2.	For all elements arrayA[i] in arrayA do
                begin
                   Initialize a boolean variable found to false 
                   For all elements arrayB[j], while found is false do
                     begin
                       if arrayA[i] is equal to arrayB[j]
            	            arrayC[k] ← arrayB[j]
                          k ← k+1
                          found ← true
                end
              end
      3.	return k
    3. In the driver class add a new option “common elements” and a new case to handle this task. The necessary actions are similar to the “findUnion” case.

At the end of the lab zip the program folder and sent it to me even if the program is not completed. Do not forget to write your name in the title comment. Follow the rules for submitting assignments. Your grade will be reduced by 10% for each violation.

If the program is not completed, you have to complete it and send it by Thursday 03/30, 5 pm.


Back to CMSC 150 home page