+ 2
Already declared random numbers in an Array, now how do I print the Largest, Smallest, Average and Range in the array?
Help on Java
2 Respuestas
+ 3
I think you can do all in one loop:
int max = arr[0];
int min = arr[0];
int sum = arr[0];
for(int i = 1; i < arr.length; i++){
sum += arr[i];
if(max < arr[i]){
max = arr[i];
}
if(min > arr[i]){
min = arr[i];
}
}
int average = sum / arr.length;
--> print max, min, average
I don't know what you mean with range.
+ 2
You use 4 variables: Min, Max, Sum and Count, which you update through a FOR loop for the array. After the loop, you can calculate what you want.
If the array does not have a null element, the Count variable may be missing, being array.length.