Purpose Practice using arrays and array functions in a Java program Instructions
ID: 3685169 • Letter: P
Question
Purpose Practice using arrays and array functions in a Java program Instructions Create a Java program that performs the following tasks 1. 2. Declare an integer array capable of storing 15 values Generate 15 random numbers using the java.util.Random class The range of the random numbers should be between 0 and 1000 B. A. Store the random values in the array Display the contents of the array on a single line 3. A. Separate the values using commas and spaces 4. Perform the following array-based functions A. B. C. Determine the location of the minimum value in the array Determine the location of the maximum value in the array Determine the average value of the array 5. Display the results of the results of the array-based functions (see Example Output section) A. Display average result to two (2) decimal placesExplanation / Answer
/** Arrays.java **/
import java.util.*;
import java.util.Random;
import java.text.DecimalFormat;
public class Arrays{
public static void main(String[] args) {
Random r = new Random();
int low = 0;
int high = 1000;
int minvalue = 0;
int maxvalue = 0;
int minindex = 0;
int maxindex = 0;
double sum = 0.0;
int[] result = new int[15];
for (int i = 0; i < 15; i++) {
int number = r.nextInt(high-low) + low;
result[i] = number;
if(result[i] < result[minindex]) {
minindex = i; // find minimum
minvalue = result[i];
}
if(result[i] > result[maxindex]) {
maxindex = i; // find maximum
maxvalue = result[i];
}
sum = sum + result[i]; // sum of the elemets in array
}
System.out.println("Contents of the Array: ");
for (int i = 0; i < 15; i++) {
System.out.print(result[i] + ","); // priting the array
}
System.out.println(" ");
System.out.println("The minimum value "+ minvalue + " is located at index " + minindex);
System.out.println("The maximum value "+ maxvalue + " is located at index " + maxindex);
DecimalFormat df = new DecimalFormat("#.00");
String formatted = df.format(sum/15); // finding average to 2 decimal places
System.out.println("The average value of the array is " + formatted);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.