import java.util.Scanner; import java.io.File; import java.io.IOException; import java.util.*; public class Assign1 { // 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= 1 // postcondition: A 2-d dimension x dimension array has been // allocated. For each row i from 0 through dimension-1, //, column j from 0 through 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 return new int[1][1]; } // Menu option 18. Print the 2-D array. public static void print2dArr(int [] [] A, int rows, int columns) { // preconditions: 0 < rows <= A.length. For each row i from // 0 through rows-1, 0 < columns <= A[i].length // postcondition: print A for the number of rows and columns // specified. Put each row on a seperate line. In each row, // put a space between each element. } public static void main(String[] args) { // The following line creates a Scanner object called Input whose // methods hand us inputs that the user types into the keyboard. For // instance, consider the following line of code that we can // now put in our program: // int i = Input.nextInt(); // The line scans through zero or more spaces and newlines, followed by // an integer, and assigns the integer to i. // See pages 43-44, which includes a list of other methods, such // as nextDouble, that a scanner object has available. Scanner Input = new Scanner(System.in); int choice = -1; // Records the option number the user selected // from the menu int [] Array1; int arr1Size; int [] Array2; int arr2Size; int [] Array3; int arr3Size; System.out.println("The first element of the file should be the size n"); System.out.println("The next n elements are the values that get filled"); System.out.println("into the array\n\n"); System.out.println("Name of file for Array1: "); String filename = Input.next(); System.out.print ("\n" + filename + "\n\n\n"); Array1 = readIn(filename); arr1Size = Array1.length; System.out.println("Name of file for Array2: "); filename = Input.next(); System.out.print ("\n" + filename + "\n\n\n"); Array2 = readIn(filename); arr2Size = Array2.length; do { System.out.println("\n ---------------------------------------"); System.out.println(" MENU"); System.out.println("0. Exit"); System.out.println("1. Read a new set of values into Array1[]"); System.out.println("2. Read a new set of values into Array2[]"); System.out.println("3. printArrFor on Array1[]"); System.out.println("4. printArrDo on Array2[]"); System.out.println("5. 'arrayMax' on Array1"); System.out.println("6. 'arraySectSum' on Array1"); System.out.println("7. 'swap' on Array1"); System.out.println("8. 'arraySectReverse' on Array1"); System.out.println("9. 'concatArrs' on Array1, Array2"); System.out.println("10. 'interleaveArrs' on Array1, Array2"); System.out.println("11. 'merge' on Array1, Array2"); System.out.println("12. 'orderedPairs' on Array1"); System.out.println("13. 'orderedTriples' on Array1"); System.out.println("14. 'orderedPairsNoRepeats' on Array1"); System.out.println("15. 'orderedTriplesNoRepeats' on Array1"); System.out.println("16. 'twoSubsets' on Array1"); System.out.println("17. 'threeSubsets' on Array1"); System.out.println("18. 'multTable' and 'print2dArr'"); System.out.print("\n Which? "); choice = Input.nextInt(); System.out.println(choice); if (choice == 1){ System.out.println("Name of file for Array1: "); // fill code in here. Don't forget to update arr1Size. } else if (choice == 2){ System.out.println("Name of file for Array2: "); // fill code in here. Don't forget to update arr2Size. } else if (choice == 3) printArr(Array1, arr1Size); //change to call to printArrFor else if (choice == 4) printArr(Array2, arr2Size); // change to call to printArrDo else if (choice == 5) System.out.println(arrayMax(Array1, arr1Size)); else if (choice == 6){ System.out.println("Enter start index"); int start = Input.nextInt(); System.out.println("Enter end index"); int end = Input.nextInt(); System.out.println("Sum="+arraySectSum(Array1,start,end)); } else if (choice == 7){ System.out.println("Enter first index"); int start = Input.nextInt(); System.out.println("Enter second index"); int end = Input.nextInt(); swap(Array1, start, end); } else if (choice == 8){ System.out.println("Enter start index"); int start = Input.nextInt(); System.out.println("Enter end index"); int end = Input.nextInt(); arrSectReverse(Array1,start,end); } else if (choice == 9) printArrFor(concatArrs(Array1,arr1Size,Array2,arr2Size),arr1Size+arr2Size); else if (choice == 10) printArrFor(interleave(Array1,arr1Size,Array2,arr2Size),arr1Size+arr2Size); else if (choice == 11) printArrFor(merge(Array1,arr1Size,Array2,arr2Size),arr1Size+arr2Size); else if (choice == 12){ int count = orderedPairs(Array1, arr1Size); System.out.println("Count="+count); } else if (choice == 13){ int count = orderedTriples(Array1, arr1Size); System.out.println("Count="+count); } else if (choice == 14){ int count = orderedPairsNoRepeats(Array1, arr1Size); System.out.println("Count="+count); } else if (choice == 15){ int count = orderedTriplesNoRepeats(Array1, arr1Size); System.out.println("Count="+count); } else if (choice == 16){ int count = twoSubsets(Array1, arr1Size); System.out.println("Count="+count); } else if (choice == 17){ int count = threeSubsets(Array1, arr1Size); System.out.println("Count="+count); } else if (choice == 18){ System.out.println("Enter size"); int size = Input.nextInt(); int [][] table = multTable(size); System.out.println("Enter rows"); int rows = Input.nextInt(); System.out.println("Enter columns"); int col = Input.nextInt(); print2dArr(table,rows,col); } } while (choice != 0); } }