#HW 1: for loop 1. Write a loop that calculates 13-factorial.
fResult<-1
fInput <- 13
for (i in 2:fInput)
{
fResult<-i*fResult
i=i+1
}
returnValue(fResult)
## [1] 6227020800
#HW 1: While loop 1. Write a loop that calculates 13-factorial
fResult<-1 #answer
count<-1 #how many times loop has run
fInput <- 13 #change the factorial here
while (count<=fInput) #loop
{
fResult<-count*fResult# multipies the prevoius result by the next whole number
count=count+1 #increases the count by one each time
}
returnValue(fResult)
## [1] 6227020800
print(seq(10, 50, by = 5))
## [1] 10 15 20 25 30 35 40 45 50
#HW 3: 3. Create the function “lines” that takes two x,y points and calculates three things: the distance between those two points, the slope and the y intercept. The function should allow you to input both x and y values and give you the three answers.
lines <-function(x1,y1,x2,y2) {
distance <- sqrt ((x2-x1)^2 + (y2-y1)^2)
print(paste ("Distance: ",distance), quote=FALSE)
slope<-(y2-y1)/(x2-x1)
print(paste ("Slope: ", slope), quote=FALSE)
Yintercept <-y1-(x1*slope)
print(paste("Y intercept: ",Yintercept), quote=FALSE)
}
lines(5,8,11,15)
## [1] Distance: 9.21954445729289
## [1] Slope: 1.16666666666667
## [1] Y intercept: 2.16666666666667
Note that the echo = FALSE
parameter was added to the code chunk to prevent printing of the R code that generated the plot.