List of all available Websheets
Viewing cpp/recursion/array_max_tail by redekopp@usc.edu. You have unsaved changes. Log in above to create, edit, preview and publish Websheets.
Property | Value |
---|---|
Description html markup shown to student | Using tail recursion (i.e. where you do some work on the way down the recursion and then just return the answer back up) create a method <tt>amax()</tt> that finds the maximum element in an array of non-negative integers. <p> <strong>Return -1 if the list is empty.</strong> <p> Consider carefully what data needs to be communicated as you recurse down the array and then as you return back up the array.<br><strong>If you believe it necessary, define a helper function.</strong> |
Public permissions | |
Remarks Comments, history, license, etc. | Copied from problem cpp/recursion/array_max_head (author: redekopp@usc.edu) Copied from problem cpp/recursion/list_max (author: daveagp@gmail.com) |
Engine | |
Template / Reference solution |
using namespace std; // You can prototype a helper function here if you need it \[ int amax_helper(int* array, int len, int mymax); \show: void dummy(); // replace me if needed, else just leave me alone ]\ // This is the primary max() function that the user should call int amax(int* array, int len) { \[ return amax_helper(array, len, -1); ]\ } // You can write your helper function here if you need it \[ int amax_helper(int* array, int len, int mymax) { if(len == 0) return mymax; else { return amax_helper(array+1, len-1, max(mymax, array[0])); } } \show: void dummy(){}; // replace me if needed, else just leave me alone ]\ int main() { int len; cin >> len; int* data = new int[len]; for(int i=0; i < len; i++){ cin >> data[i]; } cout << amax(data, len); return 0; } |
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 1 0 3"}, {"stdin": "3 3 0 1"}, {"stdin": "3 1 3 0"}, {"stdin": "0 "}, {"stdin": "5 9 4 4 0 19"}, {"stdin": "5 2923 2 20 43 298"} ] |
Forbidden substrings json list of strings e.g. ["for","while"] | ["for", "while", "do"] |
Solution visibility |
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'.