Ask Question
24 June, 10:42

Code scramble: make the program sort the three numbers x, y and z into increasing order, so that x has the smallest value, y has the next smallest value, and z has the largest value. Drag and drop with your mouse to rearrange the lines.

+3
Answers (1)
  1. 24 June, 10:44
    0
    This question is taken from code scramble exercise which has scattered lines which are to be arranged by dragging and dropping with mouse to to get the desired output in which x has the smallest value, y has the next smallest value, and z has the largest value. So the correct arrangement is as following:

    tmp=max (x, y)

    x=min (x, y)

    y=tmp

    tmp=max (y, z)

    y=min (y, z)

    z=tmp

    tmp=max (x, y)

    x=min (x, y)

    y=tmp

    Explanation:

    So the flow works as following:

    First the maximum of x and y is taken and the result is stored in a temporary variable tmp which is used so that the original values do not get affected or modified by the operations performed by the functions. Lets say that x=30 y=15 and z=40

    Now the max () functions returns the maximum from the values of x and y. As x=30 and y=15 so value of x is stored in tmp which is 30 as 30>15

    Next the min () function finds the minimum from the values of x and y and stores the result in x. As the minimum value is 15 so this value is stored in x and now x=15

    y = tmp means now the value in tmp is copied to y, So y = 30

    Next the max () function finds the maximum value from the values of y and z. As y=30 and z=40 So the maximum value is 40 so tmp variable now contains the value 40.

    Next the min () function finds the minimum from the values of y and z and stores the result in y. As the minimum value is 30 so this value is stored in y which means y remains 30 so, y=30

    z = tmp means now the value in tmp is copied to z, So z = 40

    Now the max () functions returns the maximum from the values of x and y. After the operations of above functions the new value of x and y are: x=15 and y=30 so value of y is stored in tmp which is 30 as 30>15

    Next the min () function finds the minimum from the values of x and y and stores the result in x. As the minimum value is 15 so this value is stored in x and x remains: x=15

    y = tmp means now the value in tmp is copied to y, So y remains: y = 30

    So the final values of x, y and z are:

    x = 15 y = 30 z = 40

    If you want to make a program which sort the values in x, y and z then i am providing a simple Python program that serves the purpose:

    x = int (input ("enter first number: "))

    y = int (input ("enter second number: "))

    z = int (input ("enter third number: "))

    a1 = min (x, y, z)

    a3 = max (x, y, z)

    a2 = (x + y + z) - a1 - a3

    print ("x = ", a1,"y=", a2,"z=", a3)

    The program asks user to enter the value for x, y and z.

    Then the minimum value from the values of x, y and z is computed using min () function and the result is stored in a1. This will be the value of x. Then the maximum value from value of x, y and z is computed using max () function and the result is stored in a2. This will be the value of z. Next the statement: a2 = (x + y + z) - a1 - a3 works as following:

    Lets say the values of x = 30 y = 15 and z = 40 So a1 = 15 and a2 = 40

    a2 = 30 + 15 + 40 - 15 - 40

    a2 = 30

    This is the value of y

    So print ("x = ", a1,"y = ", a2,"z = ", a3) displays the following output:

    x = 15 y = 30 z = 40
Know the Answer?
Not Sure About the Answer?
Find an answer to your question 👍 “Code scramble: make the program sort the three numbers x, y and z into increasing order, so that x has the smallest value, y has the next ...” in 📗 Engineering if the answers seem to be not correct or there’s no answer. Try a smart search to find answers to similar questions.
Search for Other Answers