A programming team has developed a virtual machine optimizes the usage of functi
ID: 3537023 • Letter: A
Question
A programming team has developed a virtual machine optimizes the usage of function calls to allow for maximum possible uses, particularly with recursion. The only problem is that it only uses addition and subtraction. Not surprisingly, the context where this machine can be used has requirements to multiply data.
To take full advantage of its capabilities, write a recursive function that multiplies two numbers while only using the capabilities of addition and subtraction. The function signature is:
int multiply (int a, int b)
Explanation / Answer
int multiply(int a, int b) { int answer; if(b==1){ result = a; } else{ result = a + multiply(a, b-1); } return(answer); }
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.