fact <- 1
for (i in 1:12)
{
fact <- fact * i
print(fact)
}
## [1] 1
## [1] 2
## [1] 6
## [1] 24
## [1] 120
## [1] 720
## [1] 5040
## [1] 40320
## [1] 362880
## [1] 3628800
## [1] 39916800
## [1] 479001600
numVec <- seq (20, 50, 5)
print(numVec)
## [1] 20 25 30 35 40 45 50
I created my own source code iteratively. According to the literature, there was no way to read from user input. When necessary, I had to google the ability to implement readline method to input user strings and as.integer method to check if the user formatted number is really an integer. To test the that the application worked I used two tests to verify that the both x values were correct. ### Test 1: a = 2, b = 5, c = -25. Solution x1 = 2.5 and x2 = -5 ### Test 2: a = 1, b = 1, c = -3. Solution x1 = 1.302776, x2 = -2.302776
factorial <- function(a,b,c)
{
# A. Preliminary checks before performing quadratic equation
# 1. Check if a, b, and c integer conversions passed. If not, program cannot proceed.
if (is.na(a) || is.na(b) || is.na(c))
{
print("Cannot proceed. Value of a, b, or c are not integers.")
return
}
# 2. Check if a is 0. Cannot proceed if a is 0.
if (a == 0)
{
print("Cannot proceed. Value of a is 0. Cannot divide into 0.")
return
}
# 3. Check that b^2 - 4ac is does not have negative number
numerator2 <- (b ^ 2) - (4 * a * c)
if (numerator2 < 0)
{
print("Cannot proceed. Value b^2 - 4ac is negative.")
return
}
# B. We can now do calculations.
x1 <- (-b + sqrt(numerator2)) / (2 * a)
x2 <- (-b - sqrt(numerator2)) / (2 * a)
print(sprintf("quadratic solution for %s, %s, %s = x1=%s, x2=%s",a,b, c, x1, x2))
}
# Basic Tests
factorial(2,5,-25)
## [1] "quadratic solution for 2, 5, -25 = x1=2.5, x2=-5"
factorial(1,1,-3)
## [1] "quadratic solution for 1, 1, -3 = x1=1.30277563773199, x2=-2.30277563773199"