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 secondk first > thir

ID: 3870317 • Letter: 5

Question

5. Assume first and second are integer variables. if (first secondk first > third) System-out.print f("%d else if (second third) largest.", first); is the System . out.print f("%d is the largest. ", second); else System.out printf("id is the largest.",third) (A) what will be the output of the code segment when first = 8, second = 7 and third = 87(5 points] (B) At least 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 (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) exression unnecessarily. [5 points num Bool Expr 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 Duncan:CSC 1350:F 17 3

Explanation / Answer

A. The else part will work and the output will be "8 is the largest."

B. At least two comparisons will be made when this code segment is executed i.e. when the first if statement will result true. In this case, first should be larger than second and third, for example, first = 10, second = 8, third = 9. In this case, the output would be from the first if statement, "10 is the largest."

C. At most three comparisons will be made when this code segment is executed. In this case, either else if or the else statement will run. This can occur when first = 8, second = 10, third = 9. In this case, the output would be from the else-if body, "10 is the largest."

D. When more than one variable will have the same value, the code will work inefficiently. For example, when first = third = 8 and second = 7, the code will execute else if statement unnecessarily. The code is inefficient in this case because it may happen that more than one variable will hold the largest value but the code is not checking for equality.

E. The average number of Boolean expressions evaluated per branch of this code segment, E = (Total number of comparisions) / (number of branches) = 3 / 3 = 1

F. The optimized code may be,

            if(first >= second && first >= third)  

          System.out.printf("%d is the largest.", first);

     else if(second >= third)

          System.out.printf("%d is the largest.", second);

     else

          System.out.printf("%d is the largest.", third);

G. The modified code can be:

            numBoolExprEval = 0;

     if(first >= second && first >= third)   {

          numBoolExprEval += 2;

          System.out.printf("%d is the largest.", first);

     }

     else if(second >= third){

          numBoolExprEval++;

          System.out.printf("%d is the largest.", second);

     }

     else

          System.out.printf("%d is the largest.", third);

H. The average number of Boolean expressions evaluated per branch of this modified code segment, E = (Total number of comparisions) / (number of branches) = 3 / 3 = 1.

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