switch ( i ) { case 513: j = 1; break; case 44: j = 2; break; case 97: j = 3; br
ID: 3615305 • Letter: S
Question
switch ( i ) { case 513: j = 1; break; case 44: j = 2; break; case 97: j = 3; break; case 1896: j = 4; break; case 351: j = 5; break; case 10: j = 6; break; case -545: j = 7; break; case 32: j = 8; break; case 816: j = 9; break; case -902: j = 10; break; case 491: j = 11; break; case 956: j = 12; break; case -243: j = 13; break; case -773: j = 14; break; case 84: j = 15; break; case 390: j = 16; break;(a) How many comparisons would be performed, on average, iflinear search were used to implement this switch statement? (Assumethat i always matches one of the case values listed and that eachvalue is equally likely.) A "comparison" involves testing whetherthe value of i matches one of the case value.
(b) Repeat part (a) using binary search instead of linearsearch. Assume that a single comparison can test whether i is lessthan, equal to, or greater than a particular case value.
(c) Would it make sense to use a jump table to implement thisswitch statement? Why or Why not? case 513: j = 1; break; case 44: j = 2; break; case 97: j = 3; break; case 1896: j = 4; break; case 351: j = 5; break; case 10: j = 6; break; case -545: j = 7; break; case 32: j = 8; break; case 816: j = 9; break; case -902: j = 10; break; case 491: j = 11; break; case 956: j = 12; break; case -243: j = 13; break; case -773: j = 14; break; case 84: j = 15; break; case 390: j = 16; break;
(a) How many comparisons would be performed, on average, iflinear search were used to implement this switch statement? (Assumethat i always matches one of the case values listed and that eachvalue is equally likely.) A "comparison" involves testing whetherthe value of i matches one of the case value.
(b) Repeat part (a) using binary search instead of linearsearch. Assume that a single comparison can test whether i is lessthan, equal to, or greater than a particular case value.
(c) Would it make sense to use a jump table to implement thisswitch statement? Why or Why not?
Explanation / Answer
Help please
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.