# # Unit 6 - User-functions # # Practice # # Main focus on structure. # Additional revision of previous study material. # (1) # Write the function Q1. # The function has two parameters: # x and y, both numeric values. # The function returns their average Q1 <- function(x, y) { return ((x + y) / 2) } print(Q1(5, 3)) # (2) # Write the function Q2. # The function has two parameters: # x and y, both numeric values. # The function returns their multiplication, # without using the * operator. Q2 <- function(x, y) { result <- 0 for (i in 1:y) { result <- result + x } return (result) } print(Q2(7, 6)) 5 + 5 + 5 # (3) # Write the function Q3. # The function has two parameters: # x and y, both numeric values. # The function returns the bigger of the two. Q3 <- function(x, y) { if (x > y) { return (x) } if (x < y) { return (y) } return (x) } print(Q3(8, 9)) # (4) # Write the function Q4. # The function has one parameter: # grades - a non-empty vector. # The function produces a new vector, # in which 5 is added to each of vec's grade # which is bigger than 90, and each grade # greater than 95 is rounded to 100. # The function returns the new vector. # 1 Q4 <- function(grades) { newGrades <- c() for (ind in 1:length(grades)) { curGrade <- grades[ind] if (curGrade > 90 & curGrade <= 95) { newGrades <- c(newGrades, grades[ind] + 5) } else if (curGrade > 95) { newGrades <- c(newGrades, 100) } else { newGrades <- c(newGrades, grades[ind]) } } return (newGrades) } myGrades <- c(99, 94, 75, 22) print(Q4(myGrades)) # 2 Q4 <- function(grades) { newGrades <- c() for (ind in 1:length(grades)) { curGrade <- grades[ind] if (curGrade > 90 & curGrade <= 95) { newGrades[ind] <- grades[ind] + 5 } else if (curGrade > 95) { newGrades[ind] <- 100 } else { newGrades[ind] <- grades[ind] } } return (newGrades) } myGrades <- c(99, 94, 75, 22) print(Q4(myGrades)) vec <- c() vec[1] <- 2 vec[2] <- 5 vec[3] <- 9 print(vec) # (5) # Write the function Q5. # The function has three parameters: # x and y - numeric values, # op - 1 (default), or 0. # If op equals 1 the functions # returns x + y, and if it is 0 # the function returns x - y. Q5 <- function(x, y, op = 1) { if (op == 1) { return (x + y) } return (x - y) } print(Q5(3, 4, 0)) # (6) # Write the function Q6. # The function has two parameters: # - vec - a vector # - inds - a vector of indices in vec # The function produces a new vector # including all values in vec whose indices # are listed in inds, and in their order in the latter. # The function returns the new vector. Q6 <- function(vec, inds) { result <- c() for (ind in inds) { result <- c(result, vec[ind]) } return (result) } myVec <- c(5, 4, 3, -1, 6) myInds <- c(5, 1, 3) print(Q6(myVec, myInds)) # (7) # Write the function Q7 # The function has one parameter: # - vec - a non empty vector # including more than 3 values, # The function iteratively and reandomly produces # an index in vec. # When the same index is chosen # twice, the function returns # the value in that index # 1 Q7 <- function(vec) { chosen <- c() while (TRUE) { randInd <- sample(1:length(vec), size = 1) print(randInd) if (!randInd %in% chosen) { chosen <- c(chosen, randInd) } else { return (vec[randInd]) } } } myVec <- c(5, 4, 2, 3) print(Q7(myVec)) # 2 Q7 <- function(vec) { chosen <- c() found <- TRUE while (found) { randInd <- sample(1:length(vec), size = 1) print(randInd) if (randInd %in% chosen) { found <- FALSE } else { chosen <- c(chosen, randInd) } } return (vec[randInd]) } myVec <- c(5, 4, 2, 3) print(Q7(myVec))