Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

5. Assume first and second are integer variables if (first>second && first > thi

ID: 3890919 • Letter: 5

Question

5. Assume first and second are integer variables if (first>second && first > third) largest. ".first); System.out.printf("%d else if (second > third) System.out.printf("%d is the is the largest. ".second); else System . out,printf("%d is the largest.",third); (A) What will be the output of the code segment when first 8, second = 7 and third- 8? (5 points (B) At least how many comparisons w be made when this code segment is executed? Give values for the variables first, second and third when this occurs. What would be the output of the code segment? [5 points] (C) At most how many comparisons will be made when this code segment is executed? Give values for the variables first, second and third when this occurs. What would be the output of the code segment? [5 points] Explain why the code segment is inefficient by giving values for the variables first second and third for which a Boolean expression is evaluated unnecessarily. [5 points] (D) (E) Calculate E, the average number of Boolean expressions evaluated per branch of (F) Write an optimized version of the code segment that does not evaluate a Boolean (G) Write a modified version of the code segment in 5.(F) by adding a counter variable this code segment. [5 points] numBoolExp Eval whose value will always be the number of Boolean expressions evaluated after the code segment is executed. [5 points] (H) Calculate the average number of Boolean expression evaluated per branch of the optimized version of the code segment that you wrote in 5.(F). (5 points]

Explanation / Answer

Question 5
A) first>second = true
first>third = false

hence control goes to second>third which is also false
finally else condition executes and prints :

" 8 is the largest "

Question 5
B)

Atleast one comparision is made if the values are like
first =8 second =7 third =6

then the first if condition is true and prints "8 is largest"

Question 5
C)

Atmost two comparisions are made as in solution for (A) bit.
values : first = 8 second = 7 third = 8

Question 5
D)

Suppose if you take solution for (A) bit , then both first and third are largest numbers having value 8 but the code gives only third as largest.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote