Questions

Assignment


1. Loop

Write a loop that calculates 12-factorial.

    # Set initial value for n = 1
    n <- 1
    answer <- 1
    for(n in 1:12) {
        answer <- n * answer
    }
  
    answer
## [1] 479001600


2. Vector

Show how to create a numeric vector that contains the sequence from 20 to 50 by 5.

    # Set the start and end range with step 5
    seq(from = 20, to = 50, by = 5)
## [1] 20 25 30 35 40 45 50


3. Function

Create the function “factorial” that takes a trio of input numbers a, b, and c and solve the quadratic equation. The function should print as output the two solutions.

    # Quadratic equation
    QuadraticEquation <- function (a, b, c) {
      
      d <- b^2 - (4 * a * c)
      
      if (d < 0) {
        d <- sqrt(-d)
        e <- paste(d, 'i')
        f <- (-b) / (2 * a)
        g <- paste(f, '+/-', e)
        print(g)
      }
      else {
        f <- (sqrt(d) -b) / (2 * a)
        print(f)
        g <- (-sqrt(d) -b) / (2*a)
        print(g)
      }
      
    }
    
    QuadraticEquation(1, 5, 9)
## [1] "-2.5 +/- 3.3166247903554 i"
    QuadraticEquation (2, -8, 6)
## [1] 3
## [1] 1
    QuadraticEquation (2, -11, 7)
## [1] 4.765564
## [1] 0.7344356


Please email to: kleber.perez@live.com for any suggestion.






    ** R Bridge Week 1 Assignment - MSDS 2019 Program.