For questions 3 - 6: Consider the following algorithm to determine which of 3 in
ID: 3879272 • Letter: F
Question
For questions 3 - 6: Consider the following algorithm to determine which of 3 input values is the largest:
Step Operation 1 Get 3 integers from the user, call them x, y and z 2 if (x > y) and (x > z). then output the value of x as the largest number and STOP 3 if (y > x) and (y > z). then output the value of y as the largest number and STOP 4 if (z > x) and (z > y). then output the value of z as the largest number and STOP 5 Stop 3-5 (1 point each) 3) Walk through the algorithm for the input values x = 2, y = 3 and z = 5. What is the output? 4) Walk through the algorithm for the input values x = 5, y = 3 and z = 3. What is the output? 5) Walk through the algorithm for the input values x = 3, y = 5 and z = 5. What is the output? 6) (2 points) Fix the algorithm so that it correctly outputs the largest value for all cases.
Explanation / Answer
According to given algorithm
if(x>y) and (x>z)
then
x is largest number
stop
if(y>x) and (y>z)
then
y is largest
stop
if(z>x) and(z>y)
then
z is largest
stop
suppose x=2, y=3, z=5;
then first it checks( x>y) and (x>z), here condition is false. then it checks for (y>x) and (y>z) condition again false
then it checks for (z>x) and (z>y) condition is true
so output became "z is argest"
then,
x=5 , y=3, z=3;
then first it checks( x>y) and (x>z), here condition is true. then according to algoritham it STOP
and output become " X is largest"
then
x=3, y=5, z=5
it checks (x>y) and(x>z) condition is false
then it checks for( y>x) and(y>z) here confusion occurs for compiler because y=z thats why run time error occured
algorithm that shows correct output
if(x>=y)and (x>=z)
then x is greater
if(y>=x)and(y>=z)
then y is greater
if(z>=x)and(z>=y)
then z is greater
stop
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.