Write recursive rules expbar ( R, X, Y, N ) to compute R = (2* X + Y ) N , where
ID: 3539023 • Letter: W
Question
Write recursive rules expbar(R, X, Y, N) to compute R = (2*X + Y)N, where R is used to hold the result, X, Y and N are non-negative integers, and X, Y and N cannot be 0 at the same time, because 00 is undefined. The program must print an error message if X = Y = N = 0.
You must apply the following test cases (inputs) to test your code:
X = 0, Y = 0, N = 0;
X = 0, Y = 0, N = 5;
X = 0, Y = 5, N = 0;
X = 5, Y = 0, N = 0;
X = 5, Y = 5, N = 0;
X = 5, Y = 0, N = 5;
X = 0, Y = 5, N = 5;
X = 5, Y = 5, N = 5;
Explanation / Answer
expbar(R, X, Y, N) :- ((X =:= 0, Y =:= 0, N =:= 0) -> #### Here #### write('No two variables can equal 0 at the same time'); ((N =:= 0) -> R is 2 * X + Y; N1 is N - 1, expbar(R1, X, Y, N1), R is (2 * X + Y) * R1 ) ). factbar(F, X, Y, N) :- ((N =:= 0; N =:= 1 ) -> F is expbar(F, X, Y, N); ((N > 1) -> expbar(F, X, Y, N), N1 is N-1, factbar(F1, X, Y, N1), F is N * F1 ) ).
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.