+ 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

3rd May 2019, 8:19 PM
Odwa Khefu
2 ответов
+ 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.
3rd May 2019, 8:53 PM
Denise Roßberg
Denise Roßberg - avatar
+ 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.
3rd May 2019, 8:30 PM
VV.