Overview

The exercises below have been written assuming that you have never programmed in C/C++ before. If you have previous experience, feel free to alter the questions to suit your background.

Exercise 1

  1. Load the Rcpp package r library("Rcpp") and run the following code r cppFunction(" double add_c(double x, double y) { double value = x + y; return value; } ")
  2. Call the function add_c and make sure it works. What happens if you pass it a vector or a character to the function?
  3. Hard: Change the function to calculate $x^y$. Hint: Google taking powers in C++.

Exercise 2

  1. Create a directory called src - this is a standard directory name for computer code. It's short for source. This is where you should keep your C++ code.
  2. Create a file called add.cpp and move the add_c function (with necessary headers) to the file.
    • Compile the file and call the add_c function.
  3. Similar to above, but add a function called subtract_c.

Exercise 3

  1. Write an R function that calculates the standard deviation - you are not allowed to use sum, mean, sd or var.
  2. Write a C function that calculates the standard deviation.
  3. Using microbenchmark, compare your C version to your R function.
    • Use the base R function sd as a baseline.

Hint: There's a chapter on RCpp Efficient R programming



jr-packages/jrBig documentation built on Jan. 1, 2020, 2:02 p.m.