Not logged in. Log in with GitHub. About Websheets.

List of all available Websheets


Viewing cpp/cs103/hw-recursion/rec_sum by daveagp@gmail.com. You have unsaved changes. Log in above to create, edit, preview and publish Websheets.
PropertyValue
Description
html markup
shown to student
 
Define a recursive function <tt>int sum_up_to(int n)</tt>
that computes the sum of the first <tt>n</tt> integers without using a loop.
<br>
E.g. <tt>sum_up_to(3)</tt> is 1+2+3 which is 6.
Engine
Template / Reference solution
 
#include <iostream>
using namespace std;
int sum_up_to(int n) {
   // base case, nothing to add
   if (\[ REDACTED ]\)
      return \[ REDACTED ]\;
   else {
      // make a recursive call to n-1
      int rec_result = \[ REDACTED ]\;
      // add the missing number
      int total = \[ REDACTED ]\;
      // return the total
      return \[ REDACTED ]\;
   }
}
int main() {
   int n;
   cin >> n;
   cout << sum_up_to(n);
}
C++ test suite
json list of stdin/args tests
e.g. [{"stdin":"hi", "args":["4", "5"]},
{"stdin":"noargs"}]

to just run once with no input use [{}]
 
[
   {"stdin": "3"},
   {"stdin": "4"},
   {"stdin": "1"},
   {"stdin": "100"}
]
Forbidden substrings
json list of strings
e.g. ["for","while"]
 
["#include", "for", "while", "*"]
remove


Optional properties:

Note: problems are open-source by default (see 'Public permissions'). Assumed license for open problems is Creative Commons 4.0 Attribution-ShareAlike unless specified in 'Remarks'.