0
I need help, what does this program do?
public class Program { public static void main(String[] args) { int a[ ] = { 23, 45, 71, 12, 87, 66, 20, 33, 15, 69 }; int min = a[0]; for (int i = 1; i < a.length; i++) { if (a[i] < min) min = a[i]; } System.out.println("The minimum value is: " + min); } }
4 Réponses
+ 5
Initially, it has been assumed that the first value of the array is the minimum.
Next, the minimum value is compared to all other values one by one. If it finds any smaller value then the min variable is updated.
Let's trace the whole thing.
min = a[0]; // it means min = 23
Is 45 < 23? No. So do nothing
Is 71 < 23? No.
Is 12 < 23? Yes. Now, min = 12
Is 87 < 12? No.
Is 66 < 12? No.
... And it goes like this.
So, when the loop will be ended, the min variable will hold the value 12. Hope it helps :)
+ 1
It find minimum value of the array
+ 1
It searches for min value in the array.
+ 1
Thank you for the Answers!! 😇