Pseudocode for mystry algorithm

input two integers a,b

initialize the value of x to a and the value of y to b

if x>y then set x to x-y

if x
repeat steps 3 & 4 until x = y

output x ( or y) and halt

what will this algorithm ouput in step 6 if a = 2437 and b = 875?

Respuesta :

Answer:

Initially if x = a = 2437 and y = 875,  condition (1) that is x>y is true and output would be x = 1562 with y = 87

Explanation:

According to pseudo code the code will run until x becomes equal to y. Initially if x = a = 2437 and y = 875,  condition (1) that is x>y is true. So next step would be x=x-y and output would be x = 1562 with y = 875. With initial values x = a = 2437 and y = 875, x becomes equal to y after 21 iterations and output is x=y=1

                 X        Y

  1.      2437         875
  2.      1562         875
  3.       687          875
  4.       687          188
  5.       499          188
  6.       311            188
  7.       123           188
  8.       123           65
  9.       58            65
  10.       58             7
  11.       51              7
  12.       44             7
  13.       37             7
  14.       30             7
  15.       23             7
  16.       16              7
  17.        9              7
  18.        2              7
  19.        2              5
  20.        2              3
  21.        2              1
  22.        1               1

Algorithms are just prototypes of the original program. The output of the algorithm in step 6 is 1.

The given algorithm performs a repeated operation, until the values of x and y are the same

Initially;

[tex]a = 2437 \\b = 875[/tex]

So, the initial values of x and y (as stated in step 2) are:

[tex]x = 2437 \\y = 875[/tex]

When x > y, the following operation is executed

[tex]x = x - y[/tex]

[tex]x = 2437 - 875[/tex]

[tex]x = 1562[/tex]

Next, the iteration checks if x = y.

This is false, so the iteration further checks if x > y

This is true, so the following operation is repeated.

[tex]x = x - y[/tex]

[tex]x = 1562 - 875[/tex]

[tex]x = 687[/tex]

Next, the iteration checks if x = y.

This is false, so the iteration checks if x > y

This is false, so the following operation is executed.

[tex]y = y -x[/tex]

[tex]y = 875-687[/tex]

[tex]y = 188[/tex]

The iteration is repeated until the values of x and y are the same (see attachment for the values of x and y, in each iterating process).

At the end of the iteration, the values of x and y is 1.

Hence, the output of the algorithm in step 6 is 1.

Read more about algorithms at:

https://brainly.com/question/17780739

Ver imagen MrRoyal
ACCESS MORE
ACCESS MORE
ACCESS MORE
ACCESS MORE