# 1. (Roots.scala) Consider the following Scala code to implement fix...

## Transcribed Text

1. (Roots.scala) Consider the following Scala code to implement fixpoint concept from Scala2.html: 2. import scala.math.abs 3. 4. val tolerance 0.0001 5. 6. def isCloseEnough (x: Double, y: Double) - 7. abs (x-y) /x) < tolerance 8. 9. def fixpoint (f: Double -> Double) (firstGuess: Double) - { 10. def iterate (guess: Double) Double - { 11. val next f (guess) 12. println(next| 13. if (isCloseEnough (guess, next)) next 14. else iterate (next) 15. } 16. iterate (firstGuess) 17. } 18. 19. def sqrt(x: Double) - fixpoint (y (y + x/y)/2)(1.0) 20. 21. sqrt (2) Write functions to find the roots of the following functions by calling fixpoint with appropriate function parameters and initial values. a. X4 10 0 b. cos(x) ex = 0 The function signatures are as follows: def roota ??? def rootb ??? Include the complete solution in the file Roots.scala so that we can run it from command line. The solutions can be extracted from Fixed Point Iteration Method 22. (WordValue.scala) Write a Scala function, to compute the value of a word. The value of a word is defined as the sum of the values of the individual letters of the word with value of "a" being 1, "b" being 2 etc. Upper and lower case letters have the same value. You can assume that the word consists of just letters. Here is the signature of the function: 23. def WordValue (s: String): Int - ??? For example, wordValue("Attitude" = 100. No loops/iterations allowed. Use recursion. Submit just the code for the function and any helpers you develop. We will call your function with our test cases. 24.(HOF.scala) Define the following two functions: 25. // given f and 9, compose returns the composition of f with g. 26. def compose Int->Int, g: Int=>Int) Int=>Int - ??? 27. 28. // given f and a positive integer D, repeated returns a function that applies f 29. // to its input o times. i.e. f(f(...(f(x))...)) 30. defrepeated(f:Int->Int,n:Int):Int->Int - ??? For example, compose(x=>x* x=>x+1)(6 = 49 and repeated(x=>3*x,4)(8) = 648. Submit just the definitions of the two functions in HOF.scala We will call these with our test cases. 31. (Rational.scala) Complete the function definitions for creating and manipulating Rational Numbers. You will represent a rational number as a pair of integers in reduced form, i.c. the rational number 3/9 will be reduced to 1/3 and stored as (1,3). Also, if the rational number is positive, neither the numerator nor the denominator should be stored as negative and if the rational number is negative then you will store only the numerator as negative. Use the following templates. You should add additional test cases in Driver.scala. Submit just Rational.scala. We will use our own Driver.scala while grading. Rational.scala Driverscala 32. (Convert.scala) Complete the definitions of the following two functions: 33. def convertNum2Binary (num: Int) String - 34. 35. def (onvertFraction2Binary(num: Double) String - Here are sample calls to these functions: convertNum2Binary (100) - "1100100" convertFraction2Binary (0.375) - ".011" convertFraction2Binary (0.8) - " .11001100110011001100110" In case of repeating patterns, stop at length 23 No loops allowed. Use recursion. Submit just the code for these two functions.

## Solution Preview

These solutions may offer step-by-step problem-solving explanations or good writing examples that include modern styles of formatting and construction of bibliographies out of text citations and references. Students may use these solutions for personal skill-building and practice. Unethical use is strictly forbidden.

object Roots {
import scala.math.abs
import scala.math.pow
import scala.math.cos
import scala.math.exp

val tolerance = 0.0001

def isCloseEnough(x: Double, y: Double) =
abs((x-y)/x) < tolerance

def fixpoint(f: Double => Double)(firstGuess: Double) = {
def iterate(guess: Double): Double = {
val next = f(guess)
//println(next)
if (isCloseEnough(guess, next)) next
else iterate(next)
}
iterate(firstGuess)
}...

By purchasing this solution you'll be able to access the following files:
Solution.zip.

\$55.00
for this solution

PayPal, G Pay, ApplePay, Amazon Pay, and all major credit cards accepted.

### Find A Tutor

View available Computer Science - Other Tutors

Get College Homework Help.

Are you sure you don't want to upload any files?

Fast tutor response requires as much info as possible.