### Problem 1: Creating and saving R files

Create and save the file p1.R on your computer. All your work should be in p1.R. You must work with one partner. You should work collaboratively. Both partners are responsible for being able to explain the work that’s been done to the preceptor.

### Problem 2: Variables and Conditionals

#### Problem 2(a)

Write code to store the value 98 in a variable named my.score. Then, write code to output the value of my.score to the console.

#### Solution

my.score <- 98
cat(my.score)
## 98

#### Problem 2(b)

Suppose we’d like to adjust the score by multiplying it by 1.2, but making it so that the adjusted score is never above 100. So if my.score is e.g. 60, the adjusted score would be 60*1.2=72, but if my.score is 90, the adjusted score would be 100, since 108=1.290 is over 100. Write code to compute the adjusted score and store it in a variable named adj.score.

#### Solution

The most likely solution is:

adj.score <- my.score * 1.2
}

The shortest possible solution is

adj.score <- min(100, 1.2 * my.score)

Another solution would be:

adj.score <- if(my.score * 1.2 > 100){
100
}else{
my.score * 1.2
}

Note that the whole expression on the right-hand side has a value, which we assign to adj.score.

### Problem 3: Function and using functions

Write a function named compute.adj.score which computes the adjusted score, as in 2(b). Write code that uses this function to display the results of adjusting the scores 60, 70, 80, 90, and 100

get.adj.score <- function(my.score){
if (my.score * 1.2 > 100){
100
}else{
my.score * 1.2
}
}

cat(get.adj.score(60))
## 72
cat(get.adj.score(70))
## 84
cat(get.adj.score(80))
## 96
cat(get.adj.score(90))
## 100
cat(get.adj.score(100))
## 100

### Problem 4: Functions and Conditionals

#### Problem 4(a)

Write a function named disc which computes $$b^2 - 4ac$$ for the inputs a, b, and c. The first line of your solution code should look like

disc <- function(a, b, c){

Now use this function to compute the discriminant of two quadratic equations.

disc <- function(a, b, c){
b^2 - 4*a*c
}

cat(disc(2, 0, 1))   # 2x^2 + x
## -8
cat(disc(2, 0, -1))  # 2x^2 -1
## 8

#### Problem 4(b)

Write a function named num.solns which returns the number of the solutions of the quadratic equation $$ax^2 + bx + c$$.

num.solns <- function(a, b, c){
d <- disc(a, b, c)
if(d > 0){
2
}else if(d == 0){
1
}else{
0
}
}

cat(num.solns(2, 0, 1))
## 0
cat(num.solns(2, 0, -1))
## 2
cat(num.solns(1, 2, 1))
## 1

#### Problem 4(c)

Write a function qaud.roots which prints (rather than returns) the solutions (if any) of a quadratic equation.

quad.roots <- function(a, b, c){
d <- disc(a, b, c)
if(d > 0){
cat((-b-sqrt(d))/(2*a), (-b+sqrt(d))/(2*a))
}else if(d == 0){
cat(-b/(2*a))
}else{
cat("No solutions")
}
}

cat(quad.roots(2, 0, 1))
## No solutions
cat(quad.roots(2, 0, -1))
## -0.7071068 0.7071068
cat(quad.roots(1, 2, 1))
## -1