This week we will dive into functions. These are the key piece for the rest of the course, and will allow you to think about real life problems. We start with one question in four versions. If you look carefully, they are all essentially the same question, so if you solve the first one, the rest should be easy.
Please write the code of the following functions:
smallest_value(x). Returns the smallest element in
largest_value(x). Returns the largest element in
place_of_smallest(x). Returns the index of the smallest element in
place_of_largest(x). Returns the index of the largest element in
Make these functions using only the commands
if() and indices. You can also do assignments, but you cannot use the words
max. In all cases the input is a vector called
You can test your function with the following codeWe will study the function
sample later, after the midterms.
<- sample(5:20, size=10, replace=TRUE) x min(x) smallest_value(x)
The two results must be the same.
The wise Shoaib Diaa Ahmed realized that I forgot to put the template for answers. Moreover, Shoaib told me about this. Good catch.
The template for answers is here.