CSC121: Exercise 1
Due: Tuesday, January 23rd by 11:00pm
What to hand in
Submit one file, which must be named "e1.R
"
(without the quotation marks).
Exercises are to be done individually.
Check that file name!
Once you have submitted on MarkUs, be sure to check that you have submitted the correct versions; new or missing files will not be accepted after the due date. Spelling of filenames, including case, counts. If your file is not named exactly as required, your mark will be zero.
If you realize you have submitted an incorrectly named file, just fix the name and resubmit. There is no need to get rid of the wrongly-named file.
Watch that clock!
We will mark the newest version submitted not later than the deadline. Don't submit just once; instead, submit at least once well in advance, so that you know what you're doing, and then keep submitting new versions as you do more of the exercise. That way, if you run out of time on the last function that you just can't do, you'll still get marks for the others.
What to put in e1.R
Below you can see the content of the starter file e1_starter.R. Download this file and rename it to e1.R
.
In this file, we have provided both the function header and the docstring for all the functions that you need to have in the file e1.R
.
You need to write the function body for each of these four functions. Simply add your code below each comment that says # Add your code here.
You need to write these functions using proper R structure and style. Although we are not marking your style, it is important to have a look at at the R Style Guidelines and use them so that you create good habits.
Use intermediate variables to help describe what your function is doing. Do not put your whole function into the return statement.
# This script contains the functions for E1. # CSC121, Winter 2018 CubeVolume <- function(sideLength) { # Returns the volume of a cube with side 'sideLength'. # Precondition: 'sideLength' is greater than 0. # Add your code here } MultipleCubesVolume <- function(sideLength, numberOfCubes) { # Returns the *total* volume of 'numberOfCubes' cubes with side 'sideLength'. # (You are required to use your CubeVolume function) # Precondition: 'sideLength' is greater than 0. # Add your code here } TravelHours <- function(travelMinutes) { # Returns the number of *whole* hours travelled during 'travelMinutes' minutes. # Hint: consider using R's built-in floor() function. # Add your code here } TripDistance <- function(speed, travelDays, travelHours) { # Returns the distance travelled (in km) when travelling with # 'speed' kilometers per hour for 'travelDays' days and travelHours 'hours'. # Assume uninterrupted travel. # Add your code here }
Example Test Cases
Here are some examples of test cases in the R Console with the correct expected value for the functions: Test Cases
Important Instructions!
Remember, you are only submitting the function definitions. You do not need to submit any test cases or R Console output.
Like the name of the file itself, the names of the functions must be spelled exactly as shown. You should not change them.
Do not call print
in
your functions, and do not write any code outside of the function definitions. Doing so may cause your code to fail the tests.
Make sure to use your CubeVolume
function when writing
your MultipleCubesVolume
function.
How to check your code
You will make mistakes! Be sure to check your functions by sourcing them in RStudio, and creating a separate R script for testing your function. Make some test cases (like we did in class and lab), and make sure your function works with different arguments. Ensure you get back what you expect when you call your functions. Do not just check the example test cases, but test the functions with test cases of your own too.
If you need help
Please make use of any of the resources listed here: Getting Help
Autotesting
We will be autotesting your code for correctness after the submission deadline. An announcement will be made once autotesting results are available, likely a week after the submission deadline, as there may be some delays while we get everything set up.