เริ่มต้นใช้งานเริ่มต้นใช้งานได้ฟรี

Print to the console

You can use Rprintf() to print formatted output to the console. It is similar to the sprintf() function in R, and uses the format specifiers:

  • %d to specify an integer
  • %s to specify a string

แบบฝึกหัดนี้เป็นส่วนหนึ่งของหลักสูตร

Optimizing R Code with Rcpp

ดูคอร์ส

คำแนะนำการฝึกหัด

Write a function that adds two integers (x and y) and prints the following message about it:

** <x> + <y> = <x + y>

แบบฝึกหัดเชิงโต้ตอบแบบลงมือทำ

ลองทำแบบฝึกหัดนี้โดยเติมโค้ดตัวอย่างนี้ให้สมบูรณ์

# Define the function add()
cppFunction('
  int add(int x, int y) {
    int res = x + y ;
    ___("** %d + ___ = ___\\n", x, y, res) ;
    return res ;
  }
')

# Call add() to print THE answer
add(40, 2)
แก้ไขและรันโค้ด