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

In the following problems do not use any mutable variables (i.e., variables decl

ID: 3886377 • Letter: I

Question

In the following problems do not use any mutable variables (i.e., variables declared with var) or any mutable predened types such as arrays, mutable sets, and so on. You may dene methods recursively this time. Do not use the if operator in Problems 1-4 below.

1. Write a Scala method mul (m:Int, n:Int):Int which, given two non-negative integers m and n, 2 returns the result of the integer multiplication of m by n. To implement mul do not use any predened arithmetic operators in Scala other than >, + and .

Explanation / Answer

def mul (m:Int, n:Int) : Int = {

while (n>0){

return m+ mul(m,n-1)

}

return 0;

}

Function mul is called in recustion and inside while loop.

So unless n becomes 0 , it will continue.

Example , m = 3 , n =4

1. return 3 + mul ( 3 , 4-1 )

2. return (3+3) , mul ( 3, 3-1 )

3. return (6+3),mul ( 3, (2-1)

4. return ( 9+3) , mul ( 3,1-1 )

here n becomes 0 and then

return ( 12+ 0)

answer is 12 .

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