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

Given nonnegative integers n, x, and y, the function A(n, x, y) is defined as fo

ID: 3540339 • Letter: G

Question

Given nonnegative integers n, x, and y, the function A(n, x, y) is defined as

follows:

if n = 0, then A(n, x, y) = x + 1

if n = 1 and y = 0, then A(n, x, y) = x

if n = 2 and y = 0, then A(n, x, y) = 0

if n = 3 and y = 0, then A(n, x, y) = 1

if n > 3 and y = 0, then A(n, x, y) = 2

otherwise, A(n, x, y) = A(n-1, A(n, x, y-1), x)

Notice that this definition is given recursively.

Assignment

Write a function A that implements the above definition according to the following

constraints:

%u2022 Use no global variables.

%u2022 Perform no input or output in the function. All I/O should be in the main program.

%u2022 The function must receive the three needed parameters from the calling program

or function. The function must return the computed value of the function A using

those parameters.

%u2022 The function must be recursive.

Write a main program that accepts user input of the n, x, and y parameters, calls the

function A, and displays the result returned by the function. Repeat until the user enters

a negative number.

Explanation / Answer

% function file

% SAVE THIS FILE AS A.m


function ans = A(n, x, y)

if n == 0

ans = x + 1;

elseif n == 1 && y == 0

ans = x;

elseif n == 2 && y == 0

ans = 0;

elseif n == 3 && y == 0

ans = 1;

elseif n > 3 && y == 0

ans = 2;

else

ans = A(n-1, A(n, x, y-1), x);

end

end


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%


% main file

% save as main.m is same place as the function A.m


while true

n=(input('Enter value for n (negative value to exit): '));

if n<0

break;

end

x=(input('Enter value for x (negative value to exit): '));

if x<0

break;

end

y=(input('Enter value for y (negative value to exit): '));

if y<0

break;

end

fprintf('The result returned by function A(%d,%d,%d) = %d ',n,x,y,A(n,x,y));

end

  

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote