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

Define a procedure “Subtract” that takes parameters and returns the difference o

ID: 3813484 • Letter: D

Question

Define a procedure “Subtract” that takes parameters and returns the difference of them You can use the built-in “-“ to define your Subtract procedure. [5 points]

> (Subtract 120 50)

70

Define a recursive procedure called “IntDivide” that will compute the quotient of x divided by y. You must implement IntDivide procedure by a sequence of Subtract procedures.

You must use the Subtract procedure defined above.

You will need to account for negative values as well.

Hint: This will require a conditional and possibly the (abs x) procedure. You may not use the built-in division or quotient operators in this procedure definition. [10 points]

> (IntDivide 8 3)

2

Define a procedure “ReadForIntDivide” to read the two input values for the IntDivide procedure defined in the previous. This procedure takes no parameters and will pass an input value to the Square procedure.    [5 points]

(ReadForIntDivide)

-25

4

-6

Define a recursive procedure called “Multiply” that will compute the product of x times y. You must implement Multiply procedure by a sequence of additions. [5 points]

You can use the built-in + operation.

You will need to account for negative values as well.

> (Multiply 8 3)

24

Define a procedure (DiffDivide x y) that will compute the following expression:
x - (x/y)*y. For example, if x = 8 and y = 3, then, 8 – (8/3)*3 = 2

You must use Subtract, IntDivide, and Multiply defined in the previous questions. [5 points]

> (DiffDivide 8 3)

2

Re-implement the procedure (DiffDivide x y) and call it (DiffDivideLet x y). In this procedure, you must use let-form to bind all the procedures used in the definition: Subtract, IntDivide, and Multiply. You may name the local name whatever you’d like. [10 points]

> (DiffDivideLet 8 3)

2

> (Subtract 120 50)

70

Explanation / Answer

Note:Multiple questions asked

1. Subtract Procedure:

;function Subtract definition

(define (Subtract vara varb)(- vara varb))

;call and display the result

(display(Subtract 120 50))

;print newline

(newline)

Output:

70

2. IntDivide procedure using repeated subtraction:


(define (subtract first second)(- first second))

(define (abs ou)

;positive will return the number itself

(if(>= ou 0)

ou

;negative number, return the negative of the number

(subtract 0 ou)))

(define (return a1 a2)

(if (>= (abs a1) a2)

(+ 1 (return (subtract (abs a1) a2) a2))

0

)

)

;procedure to divide

(define (IntDivide first second)

;check the first

(if(< first 0)

;reaptdly call the subtract method

(subtract 0 (return first second))

(return first second)))

;call division methods

(display(IntDivide 4 2))

;to check negative number division

(display(IntDivide -9 2))

Output:

2

-4

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