This is for java This is the starting code: Exercise 10-1 Use a one-dimensional
ID: 3704961 • Letter: T
Question
This is for java
This is the starting code:
Exercise 10-1 Use a one-dimensional array In this exercise, you can get some practice using one-dimensional arrays.. 1. Open the project named ch10 exl_ArrayTest in the extra_ex_starts directory. Then, open the Main class. 2. Create a one-dimensional array of 99 double values. Then, use a form loop to add a random number from 0 to 100 to each element in the array. To do that, you can call the random method of the Math class and multiply it by 100 like this: Math.random() * 100 3. Use an enhanced for loop to sum the values in the array. Then, calculate the average value and print that value to the console like this: Average: 50.9526671843517 4. Use the sort method of the Arrays class to sort the values in the array, and print the median value (the 50th value) to the console like this: Median: 52.183692 91650803 5. Print the 9th value of the array to the console and every 9th value after that like this: Position: 9 Position: 18 8.927702403161032 14.053128749806076 Position: 99 97.471670293184822Explanation / Answer
Main.java
public class Main {
public static void main(String[] args) {
double d[] = new double[99];
for(int i=0;i<d.length;i++) {
d[i]=Math.random()*100;
}
double sum = 0;
for(int i=0;i<d.length;i++) {
sum+=d[i];
}
System.out.println("Average: "+sum/d.length);
System.out.println("Median: "+d[d.length/2]);
for(int i=8;i<d.length;i+=9){
System.out.println("Position "+(i+1)+": "+d[i]);
}
}
}
Output:
Average: 49.98305856196076
Median: 27.930172202060668
Position 9: 40.437789937201764
Position 18: 3.18920673561196
Position 27: 52.2695181461773
Position 36: 22.980766015452225
Position 45: 63.412793370756326
Position 54: 24.92914090382622
Position 63: 51.20722288632121
Position 72: 99.34428090020492
Position 81: 63.80348927371301
Position 90: 26.56355845097985
Position 99: 53.2042982888202
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.