Quiz 2 Instructions

Please complete the following questions and submit a file named Quiz2.R to Gradescope for autograding.

Remember:

  • Do not use global paths in you script. Instead, use setwd() interactively in the console, but do not forget to remove or comment out this part of the code before you submit. The directory structure of your machine is not the same as the one on Gradescope’s virtual machines.
  • Do not destroy or overwrite any variables in your program. I check them only after I have run your entire program from start to finish.
  • Check to make sure you do not have any syntax errors. Code that doesn’t run will get a very bad grade.
  • Make sure to name your submission Quiz2.R

Tip: before submitting, it might help to clear all the objects from your workspace, and then source your file before you submit it. This will often uncover bugs.

Question 1.

[2 pts] Area of trapezoid

As you know, the formula for the area of a trapezoid is (base 1 + base 2) /2 \(\times\) height, as seen in the figure below:

Write R code to compute the area of a trapezoid of base1 = 10, base2 = 7, and height = 5. The computed area should be stored in an object area.

Question 2.

Consider the scores of a high school student:

  • Maths – 95
  • Physics – 88.5
  • Physics lab – 79
  • English – 92

(a). [1 pt] Use assignments to create variables maths, physics, physicslab, and english with their corresponding amounts.

(b). [2 pts] Combine the four scores from part (a) into a vector named scores. Use the names() function to label the elements of the scores vector with the names: maths, physics, physicslab, and english.

(c). [2 pts] Calculate the average of the scores and store the result in an object called avg.

(d). [4 pts] Based on the scores vector in part (b) and policy table below, create a vector named grades containing the four corresponding grades. Additionally, create an integer vector grade_values with the four grade values. Recall that integer values are of the form 1L.

Range Grade Grade Value
[90, 100] A 4
[80, 90) B 3
[70, 80) C 2
[60, 70) D 1
<60 F 0

(e). [2 pts] Suppose the credits for Maths, Physics, Physics lab, English are 0.5, 1, 0.5, 1, respectively. Create a credits object.

(f). [2 pts] Calculate the weighted GPA with

\[\text{High School GPA} = \sum (\text{grade value}*\text{credits})/ \sum \text{credits}\] Save the value to object GPA.