Java Code. Consider the code below and answer the questions from 4.1 -4.4 that f
ID: 671933 • Letter: J
Question
Java Code.
Consider the code below and answer the questions from 4.1 -4.4 that follow. What is the value of variables min and max on Line 6? How many times does the if statement on Line 8 holds true? Notice that the loop counter variable (t in the above code) starts with a value = 1. Do you expect change in the output of the program if i starts from 0? Explain why or why not? What change would you make in the code above if you want to find the minimum and maximum values in an integer array of any length?Explanation / Answer
min and max will become equal to 45 as nums[0]=45
if statement will be executed for 3 times for 23 ,-45 and -67
Doesnot change the output or the intent of the program beacuase first time when we used i=1 means that at first we have inistialised min and max to nums[0] so there is noneed of again comparing to nums[0].If we use i=0 we again compare it to nums[0] which makes the two ifs for min and max false.
public class HorizonPhones {
public static void main(String[] Args) {
int[] nums={45,23,-45,-67,233,56};
int min,max;
min=max=nums[0];
for(int i:nums){
if(i<min){
min=i;
}
if(i>max){
max=i;
}
}
System.out.println("Min and max: "+min+" "+max);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.