Exercise

Sequence of integers

The functions you wrote in previous exercises performed calculations then returned a single number. You can also write functions that return a vector.

The syntax for creating a vector type is to specify the type of the vector followed by the name of the variable, followed by the number of elements in the vector, in parentheses. For example, to create a numeric vector named numbers, containing 10 elements, the code would be as follows.

NumericVector numbers(10);

Instructions

100 XP
  • Complete the definition for a function, seq_cpp(), that takes two integers lo and hi and returns an IntegerVector of the numbers between them.
    • Set the return type to IntegerVector.
    • Create a new integer vector, sequence, of size n.
    • Inside the for loop, set the ith element of sequence to lo plus i.
    • Return sequence.