#### Question Details

##### [solution] » Please provide Accurate answers to the questions using examples

Brief item decscription

Step-by-step solution file

Item details:

More:

• 1.The gcd() function, de ned in a class Euclid, takes two nonnegative integer arguments and return the greatest common divisor of the two integers.

public class Euclid {

public static int gcd(int p, int q) { if (q == 0) return p;

return gcd(q, p % q);

}

}

a. Write an overloaded function gcd() that takes three non negative integer arguments and return the greatest common divisor of the three integers. Assume that the function is in the same class Euclid as the two-argument version above.

Hint: Use the identity gcd (p; q; r) = gcd(gcd( p; q); r). For example, gcd(504; 4116; 4410) = gcd(gcd(504; 4116); 4410) = gcd(84; 4410) = 42.

b. Give the signature of a function that takes as an argument an array of non negative integers and returns the greatest common divisor of those integers. Do not implement the function.

• 2.Consider the following TOY code fragment.

20: 2AAB     R[A] <- R[A] - R[B]

21: DA20if (R[A]  > 0) pc <- 20

22: CA24if (R[A] == 0) pc <- 24

23: 1AAB      R[A] <- R[A] + R[B]

24: 0000halt

i. Suppose that just before the code fragment is executed, R[A] stores the value 001A and R[B] stores the value 0008. What are the values of R[A] and R[B] upon termination?

R[A]:

R[B]:

ii. Suppose that just before the code fragment is executed, R[A] stores the value 5EAB and R[B] stores the value 0010. What are the values of R[A] and R[B] upon termination?

R[A]:

R[B]:

iii. Give a one-line Java statement that corresponds to the TOY code fragment above, assuming that R[A] and R[B] contain positive integers and a and b are the corresponding Java int variables.

• 3. Consider the following Java program.

public class Mystery {

public static void main(String[] args) { int curr = StdIn.readInt(); StdOut.print(curr + " ");

int prev = curr;

while (!StdIn.isEmpty()) { curr = StdIn.readInt();

StdOut.print((prev + curr) / 2 + " "); prev = curr;

}

StdOut.println();

}

}

Assume the contents of the  input.txt are given below.

% more input.txt

2    4     6     8     10     12     8       2

i. What is the result of the following command? Circle your answer.

java Mystery < input.txt

ii. What is the result of the following command? Circle your answer.

java Mystery < input.txt | java Mystery

STATUS
QUALITY
Approved

This question was answered on: Feb 21, 2020

Solution~000492207.zip (18.37 KB)