import java.util.Scanner; import java.io.File; import java.io.IOException; import java.util.*; public class Assign1Soln { // Read a sequence of integers from a file into an array. // The first integer of the file tells the number n of integers in the // sequence. The remaining n integers in the file are the sequence. // Each integer in the file is separated from the next by one // or more white-space characters (spaces or newlines). // The method returns an array that is allocated to have size n // and holds the n-integer sequence. If there is an IO exception, // which can happen if the file doesn't exist or doesn't adhere // to the formatting requirements, the method prints an error message // and halts the program with status 1. public static int[] readIn(String fName) { int[] res = {}; try { Scanner scan = new Scanner(new File(fName)); int size; size = scan.nextInt(); res = new int[size]; // loop through numbers in input file and sum them for(int i=0; i soFar) soFar = A[counter]; return soFar; } // Menu option 6. Find the sum of a section of an array. // precondition: 0 <= i <= j < A.length; // postcondition: The sum of elements in A[i..j] has been returned public static int arraySectSum(int [] A, int i, int j) { int sum=0; for (int counter = i; counter<=j; counter++) sum = sum + A[counter]; return sum; } // Menu option 7. Swap 2 elements of an array. // Preconditions: 0 <= i <= j < A.length // Postconditions: the contents of A[i] and A[j] have been // swapped public static void swap (int A[], int i, int j) { int temp = A[i]; A[i] = A[j]; A[j] = temp; } // Menu option 8. Reverse a section of an array. // preconditions: 0 <= i <= j < A.length; // postconditions: The order of elements in A[i..j] has been reversed // // Hint: You should make calls to 'swap' from within the method. public static void arrSectReverse (int [] A, int i, int j) { int counter1 = i; int counter2 = j; while (counter1 < counter2) swap(A, counter1++, counter2--); return; } // Menu option 9. Concatinate 2 arrays. // Preconditions: 0 < size1 <= A1.length; 0 < size2 <= A2.length // Postcondition: An array of length size1 + size2 has been allocated, // Its contents are the concatenation A1[0..size1-1]A2[0..size2-1], // and a reference to this array has been returned public static int [] concatArrs(int [] A1, int size1, int[] A2, int size2) { int size = size1 + size2; //size of the new array int[] A = new int[size]; // Allocate new array A // copy A1 into first part for (int counter=0; counter= 1 // postcondition: A 2-d dimension x dimension array has been // allocated. For each row i from 0 to dimension-1, // column j from 0 to dimension -1, the element in row i, // column j has been filled in with the product i*j // A reference to this array has been returned public static int [] [] multTable(int dimension) { int[][] A = new int[dimension][dimension]; for (int i=0; i