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

List of all available Websheets


Viewing cpp/recursion/kettles 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
 
Write a program <code>Kettles</code> with a recursive static method
<code>sing(n)</code> which prints the famous "kettles of tea" song.
For example, when <code>n</code> is 3, calling <code>sing(3)</code>
should print out
<pre>
3 kettles of tea on the wall
3 kettles of tea
take one down, pass it around
2 kettles of tea on the wall!
2 kettles of tea on the wall
2 kettles of tea
take one down, pass it around
1 kettles of tea on the wall!
1 kettles of tea on the wall
1 kettles of tea
take one down, pass it around
no more kettles of tea on the wall!
</pre>
<div>
<i>To sing this song, first sing the first four lines, and then sing
the song for a smaller value of n (unless you are done).</i>
This is the strategy that you can turn into
a recursive method.
</div>
Note that we require your song to say <code>1 kettles</code>
instead of the grammatically correct <code>1 kettle</code>. Fixing this 
is left as a challenge for the thirsty.
Public permissions remove
Engine
Template / Reference solution
 
#include <iostream>
using namespace std;
// sings a well-known song about n kettles
void sing(int n) {
   // print three lines
\[
   cout << n << " kettles of tea on the wall" << endl;
   cout << n << " kettles of tea" << endl;
   cout << "take one down, pass it around" << endl;
]\
   if (n > 1) {
      // sing fourth line, with exclamation point!
\[
      cout << n-1 << " kettles of tea on the wall!" << endl;
]\
      // call sing recursively on remaining kettles
      sing(\[n-1]\);
   }
   else { 
      // sing the final line, with exclamation point!
\[
      cout << "no more kettles of tea on the wall!" << endl;
]\
   }
}
int main() {
   int n;
   cin >> n;
   sing(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": "1"},
   {"stdin": "9"}
]
Solution visibility 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'.