Exercise on Single Dimensional Arrays Declare an array reference variable arrayInt for an array of integers. Create the array of size 100, and assign it to arrayInt. (2 points) Write a method to populate the array with Random numbers between 1 to 25. Signature of the method is: populateArray( int[] ) which returns nothing. Call this method from main with arrayInt--> populateArray(arrayInt). (2 points) Write a method to print an array. Signature of the method is: printArray( int[] ) which returns nothing. Print maximum of ten(10) numbers on a line (see sample output below). Call this method from main with arrayInt--> printArray(arrayInt). Hint: Use the modulus operator. Any number n % 10 will return a value 0-9. (3 points) Write a method that finds the average of the array elements. Signature of the method is: findAverage( int[] ) which returns the average to the calling program.Call this method from main with arrayInt--> findAverage(arrayInt). github

Respuesta :

Answer:

//Import the Random class to allow for the generation of random numbers

import java.util.Random;

public class SingleDimensionalArray {

public static void main(String[] args) {

 // Declaration of an array reference variable for an array of integers

 int[] arrayInt;

 // Creating an array of size 100 and assigning it to arrayInt

 arrayInt = new int[100];

 // Call the method to populate array

 populateArray(arrayInt);

 // Call the method to print array elements with a maximum of ten numbers on a

 // line

 printArray(arrayInt);

 // Call the method to find the average of the elements in the array

 System.out.println("\nThe average of the array elements is " + findAverage(arrayInt));

}

// Method to populate array with random numbers between 1 and 25

// Since it is not specified, 1 and 25 are not inclusive

public static void populateArray(int[] arr) {

 // Create an object of the Random class to generate the random numbers

 Random random = new Random();

 // Create a loop that cycles 100 times beginning at n=0

 // At every cycle, generate a random number and add it to the array

 for (int n = 0; n < arr.length; n++) {

  arr[n] = random.nextInt(23) + 2;

 }

}

// Method to print the elements in an array

public static void printArray(int[] arr) {

 // Create a loop that cycles through the array.

 // At each cycle, the element in the array corresponding to the pointer (n) of

 // the array is printed.

 // There are going to be 100 elements, maximum of 10 per line.

 // The if statement checks if 10 elements are already printed on a line.

 // If that's the case, a new line is printed for the next set of 10 elements.

 for (int n = 0; n < arr.length; n++) {

  if ((n > 0) && (n % 10 == 0)) {

   System.out.println();

  }

  System.out.print(arr[n] + " ");

 }

}

// Method to calculate the average of the elements in the given array

public static double findAverage(int[] arr) {

 // An integer variable to hold the sum of the elements is declared and

 // initialized to zero.

 int sum = 0;

 // A loop to cycle through the array while cumulatively summing the elements is

 // created.

 for (int n = 0; n < arr.length; n++) {

  sum += arr[n];

 }

 // The average of the elements is calculated using the result from the sum

 // above. The value is being type-cast to get a floating point value of the

 // average. This is the right thing to do as the average of numbers is more

 // likely to be a floating point number.

 // The result of the average is stored in a double variable called 'average'.

 double average = (double) sum / arr.length;

 // The average of the elements is returned from the method

 return average;

}

}

Explanation:

Please download the file attached to this response for a proper formatting of the code. The code contains comments explaining every segment of the code. Carefully go through the comments in the code.

Note: The function printArray() prints all the elements in the array - 10 elements per line. Since there is no sample format specified, I have assumed that the elements are separated just by spaces.

Hope this helps!

Ver imagen stigawithfun
Q&A Education