Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

My working code: #include <iostream> using namespace std; int GCD(int p, int q);

ID: 3741210 • Letter: M

Question

My working code:

#include <iostream>

using namespace std;

int GCD(int p, int q);

void main()

{

int p, q;

cout << "Enter two positive integers. First: ";

cin >> p;

cout << "Second: ";

cin >> q;

cout << "GCD of " << p << " and " << q << " is: " << GCD(p, q) << endl;

}

int GCD(int p, int q)

{

if (q != 0) return GCD(q, p%q);

else return p;

}

Therefore I need the answers to b) and c)

Question 2: (a) The greatest common divisor (GCD) of two positive integers is the largest integer that divides both of them (e.g., the GCD of 102 and 68 is 34.) We know that, when p > q, the GCD of p and q is also the GCD of q and p % q. Implement accordingly the recursive function int GCD(int p, int q), making your code as concise as possible while ensuring it works in all cases. (3 marks) (b) State exactly why the following recursive function is incorrect. (1 mark) int G ( int n) return -n G (n); ) (c) State exactly why the following recursive function is incorrect. (2 marks) int H (int n) if (nreturn 0; if(n%21-0) return 1 + H ( 3*n + 1); else return H (n/2 );

Explanation / Answer

b)

Base case is not defined for G function

so, This function call will never end(infinite loop)

c)

If n is negative, Then base case of n == 1 is never reached

so, This function call will never end if n is negative (infinite loop)