Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

1. Create a 1-dimensional array with n elements; get the size of the array as us

ID: 3657689 • Letter: 1

Question

1. Create a 1-dimensional array with n elements; get the size of the array as user input (validate!), max size should be 10 (declared as class constant). Your program should use modularity. Have methods to: Take input from the user the elements of a 1-dimensional array. Print a 1-dimensional array. Compute the average value/1-dimensional array. Display the difference between each value/1-dimensional array and the average. Display all elements with odd subscripts. Display all elements with even subscripts. Compute the sum of all elements with odd subscripts. Compute the sum of all elements with even subscripts. Generate output similar to the sample output below. SAMPLE RUN: Do you want to start(Y/N): y Enter array size: 7 Now enter 7 values: 56.7 78.9 23.5 111.5 114.7 99.99 88.88 The array elements are: 56.70 78.90 23.50 111.50 114.70 99.99 88.88 The average value = 82.02 Index Value Value - avg =========================== 0 56.70 -25.32 1 78.90 -3.12 2 23.50 -58.52 3 111.50 29.48 4 114.70 32.68 5 99.99 17.97 6 88.88 6.86 The elements with even subscripts are: Index Value ============= 0 56.70 2 23.50 4 114.70 6 88.88 The sum of the elements with even subscripts = 283.78 The elements with odd subscripts are: Index Value ============= 1 78.90 3 111.50 5 99.99 The sum of the elements with odd subscripts = 290.39 Do you want to continue(Y/N): y Enter array size: -4 ERROR! Should be positive and < 10. REENTER: 10 Now enter 10 values: 112 34.5 77.88 55.77 44.7 33.6 117.89 36.78 119.69 56.12 The array elements are: 112.00 34.50 77.88 55.77 44.70 33.60 117.89 36.78 119.69 56.12 The average value = 68.89 Index Value Value - avg =========================== 0 112.00 43.11 1 34.50 -34.39 2 77.88 8.99 3 55.77 -13.12 4 44.70 -24.19 5 33.60 -35.29 6 117.89 49.00 7 36.78 -32.11 8 119.69 50.80 9 56.12 -12.77 The elements with even subscripts are: Index Value ============= 0 112.00 2 77.88 4 44.70 6 117.89 8 119.69 The sum of the elements with even subscripts = 472.16 The elements with odd subscripts are: Index Value ============= 1 34.50 3 55.77 5 33.60 7 36.78 9 56.12 The sum of the elements with odd subscripts = 216.77 Do you want to continue(Y/N): n

Explanation / Answer

package Cramster; import java.util.Scanner; public class DimensionalArray { private static double[] array; private static Scanner console = new Scanner(System.in); public static void main(String[] args) { while(true) { System.out.printf("Do you want to start (Y/N) : "); String choice = console.next(); if(choice.equalsIgnoreCase("N")) { System.out.printf("Terminating Program"); console.close(); System.exit(0); } else if(choice.equalsIgnoreCase("y")) { int size = 0; System.out.print("Enter Array Size : "); while(true) { size = console.nextInt(); if(size 0) break; else System.out.print("ERROR! Should be positive and < 10. ReEnter : "); } getInput(size); print(); System.out.printf("The average value is : %.2f ", average()); displaydifference(); displayOdd(); displayEven(); sumOdd(); sumEven(); } else { System.out.println("Wrong Input. Try Again"); } } } public static void getInput(int size) { array = new double[size]; System.out.printf("Now Enter %d values : ",size); for(int i=0; i