1Create a onedimensional array of 99 double values Then use
1-Create a one-dimensional array of 99 double values. Then, use a for loop to add a random number from 0 to 100 to each element in the array. To do that, use the random method of the Math class to get a double value between 0.0 and 1.0 and multiply it by 100
like this
Math.random() * 100
2-Use an enhanced for loop to sum the values in the array. Then, calculate the average value and print that value on the console like this
Average: 50.9526671843517
3-Use the sort method of the Arrays class to sort the values in the array, and print the median value (the 50th value) on the console
like this
Median: 52.18369291650803
-Print the 9th value of the array on the console and every 9th value after that.
like this
position: 9 8.927702403161032
position: 18 14.0531287498060076
...
position: 99 22.471670293184822
Solution
Hi, Please find my implementation.
Please let me know in case of any issue.
import java.util.Arrays;
public class DoubleRandom {
public static void main(String[] args) {
// creating array
double[] arr = new double[99];
for(int i=0; i<99; i++){
arr[i] = Math.random() * 100;
}
double sum = 0;
for(double d : arr){
sum += d;
}
System.out.println(\"Average : \"+sum/99.0);
Arrays.sort(arr);
System.out.println(\"Median: \"+arr[49]); // 50th value
int index = 8; // 9th value index
while(index < 99){
System.out.println(\"position: \"+(index+ 1)+\" \"+arr[index]);
index = index + 9;
}
}
}
/*
Sample run:
Average : 49.75761742464862
Median: 49.962842815728436
position: 9 8.766757546030624
position: 18 17.262110835701115
position: 27 20.865192789633745
position: 36 29.89303631952237
position: 45 43.084534673882814
position: 54 55.62839129243991
position: 63 66.13908523713565
position: 72 77.36692081739585
position: 81 83.77114421253107
position: 90 88.62289971975
position: 99 99.78514216085563
*/


