Problem 1
k=1
for (i in 1:13)
{
k <- k*i
}
print(k)
## [1] 6227020800
k=1
i=1
while (i <= 13)
{
k <- k*i
i <- i+1
}
print(k)
## [1] 6227020800
Problem 2
vec <- seq(10,50, 5)
print(vec)
## [1] 10 15 20 25 30 35 40 45 50
Problem 3
#library(raster)
#first.point <- "enter the first x and y (space-separated) \n"
#second.point <- "enter the second x and y (space-separated) \n"
#first.point <- as.integer(strsplit(readline(first.point), " ")[[1]])
#second.point <- as.integer(strsplit(readline(second.point), " ")[[1]])
#lines <- function(point1, point2) {
# dist <- pointDistance(point1, point2, lonlat=FALSE)
# slope <- (second.point[2]-first.point[2])/(second.point[1]-first.point[1])
# yintercept <- (second.point[2]-slope*second.point[1])
# print(point1)
#print(point2)
# print(sprintf("Distance between points: %s", dist))
# print(sprintf("Slope: %s", slope))
# print(sprintf("yintercept: %s", yintercept))
#}
#lines(first.point, second.point)
#OUTPUT
#[1] 5 10
#[1] -15 -10
#[1] "Distance between points: 28.2842712474619"
#[1] "Slope: 1"
#[1] "yintercept: 5"
first.x <- readline(prompt="Enter first x \n")
## Enter first x
first.x <- as.integer(first.x)
first.y <- readline(prompt="Enter first y \n")
## Enter first y
first.y <- as.integer(first.y)
second.x <- readline(prompt="Enter second x \n")
## Enter second x
second.x <- as.integer(second.x)
second.y <- readline(prompt="Enter second y \n")
## Enter second y
second.y <- as.integer(second.y)
lines <- function(x1,x2, y1,y2) {
dist <- sqrt (((x2-x1)^2)+(y2-y1)^2)
slope <- (y2-y1)/(x2-x1)
yintercept <- (y2-slope*x2)
print(sprintf("First point: %s %s", x1, y1))
print(sprintf("Second: %s %s", x2, y2))
print(sprintf("Distance between points: %s", dist))
print(sprintf("Slope: %s", slope))
print(sprintf("yintercept: %s", yintercept))
}
lines(first.x, second.x, first.y, second.y)
## [1] "First point: NA NA"
## [1] "Second: NA NA"
## [1] "Distance between points: NA"
## [1] "Slope: NA"
## [1] "yintercept: NA"
#OUTPUT
#[1] "First point: 5 10"
#[1] "Second: -15 -10"
#[1] "Distance between points: 28.2842712474619"
#[1] "Slope: 1"
#[1] "yintercept: 5"